【问题标题】:Winform form border issue in windows 10Windows 10 中的 Winform 表单边框问题
【发布时间】:2017-08-28 05:41:51
【问题描述】:

我是 WinForms 新手,因此需要您就我在 Window 10 Pro 环境中部署 Winform 应用程序时遇到的这个问题提供专家建议。我看到将 FormBorderStyle 设置为 SizableToolWindow(或 FixedToolWindow 的对话框)不会在窗口的所有侧面绘制边框,除了顶部。

FormBorderStyle 设置为 SizableToolWindow 时的边框问题

当 FormBorderStyle 设置为 FixedSingle 时会看到边框

示例完整代码如下:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form form = new Form();
            form.FormBorderStyle = FormBorderStyle.FixedSingle;
            form.ShowDialog();
        }
    }

是否有可以覆盖此行为的解决方案,可能仅适用于 Windows 10?

编辑:我观察到,当我将表单的 ControlBox 属性设置为 false 时,客户端站点仅显示并且具有完整的边框,但标题栏不可见。

【问题讨论】:

  • 如果将鼠标指针放在边缘,当 FormBorderStyle 设置为 SizableToolWindow 时,它将显示调整大小光标
  • 工具窗口不应该是这样的吗?
  • 嗨 EpicKip,工具箱顶部不应有最小化和最大化按钮,但活动窗口边框应位于所有三个边(左、右和下)
  • 我认为这是 Win10 的事情,你无能为力。
  • 我遇到了这样的问题。我发现用鼠标调整窗口大小会使其消失并重新出现,具体取决于大小。为了解决这个问题,我更改了表单的高度(Form.ClientSize)。可能在其他计算机上它有时仍然不会显示。

标签: c# winforms


【解决方案1】:

好吧,我会说行为和渲染取决于操作系统,我认为您的问题没有真正的答案。

但是,您可以创建自定义表单/窗口,并将其用作工具窗口或您想要的任何方式。

首先,您需要将 FormBorderStyle 设置为 None

this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

然后你可以重写 OnPaint 方法并像下面的代码那样在那里绘制你的边框

protected override void OnPaint(PaintEventArgs e)
        {
            Rectangle borderRectangle = new Rectangle(1, 1, ClientRectangle.Width - 2, ClientRectangle.Height - 2);

            e.Graphics.DrawRectangle(Pens.Blue, borderRectangle);
            base.OnPaint(e);
        }

结果将如下图所示:

请注意,您必须处理其他事情,例如让此表单能够移动,确保添加自定义关闭按钮等。

完成此操作后,您可以将此表单用作基类并从该表单继承您未来的表单类。

该自定义表单的完整代码为:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace CustomForm
{
    public partial class CustomBorderForm : Form
    {
        public CustomBorderForm()
        {
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Rectangle borderRectangle = new Rectangle(1, 1, ClientRectangle.Width - 2, ClientRectangle.Height - 2);

            e.Graphics.DrawRectangle(Pens.Blue, borderRectangle);
            base.OnPaint(e);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

我希望这会有所帮助。

【讨论】:

  • 似乎是唯一合乎逻辑的方法。感谢您的回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-13
  • 1970-01-01
  • 1970-01-01
  • 2017-02-01
  • 2012-11-22
  • 2015-10-20
  • 1970-01-01
相关资源
最近更新 更多