本文介绍了Windows 10 中的 Winform 窗体边框问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to WinForms, so need your expert advice on this issue that I am facing when I deploy my Winform Application in Window 10 Pro environment. I see that the dialog form which has FormBorderStyle set to SizableToolWindow(or FixedToolWindow for that matter) doesnt paint the Borders on all side on the window except on top.

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?

Is there a solution which can override this behavior maybe just for windows 10?

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

I observed that when I set the ControlBox Property of the Form to false the client site is only shown and has the complete border but then the Caption bar is not visible.

推荐答案

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

Well, I would say the behavior and the rendering depends on the operating system and I think there is no real answer to your question.

但是,您可以创建自定义表单/窗口,并且可以将其用作工具窗口或随意使用.

However, you can create custom form/window and you can use it as a tool window or however you want.

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

First, you would need to set the FormBorderStyle to None

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

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

Then you can override the OnPaint method and draw your border there like the code below

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);
        }

结果如下图所示:

请注意,您必须注意其他事项,例如可以移动此表单,确保添加自定义关闭按钮等.

Please note that you will have to take care of other things like giving the ability to this form to be moved around, make sure you add custom close button etc.

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

Once you have this done, you can use this form as a base class and inherit your future form classes from that one.

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

The complete code of that custom form would be:

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();
        }
    }
}

我希望这会有所帮助.

这篇关于Windows 10 中的 Winform 窗体边框问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 04:21