首页 > 解决方案 > 为什么在切换 FormBorderStyle 属性后 WinForms 表单会移动(左 -7、-8)?

问题描述

我正在开发一个 WinForms 应用程序,它具有通过单击 F3 按钮隐藏和显示标题栏的功能。

一切正常,但我有多个显示器,当我切换标题栏的可见性(窗口最大化)时,我看到窗口向左移动了 -7、-8。

代码如下:

Form1.cs

using System.Windows.Forms;

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

        private void OnKeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F3)
            {
                FormBorderStyle = FormBorderStyle == FormBorderStyle.None ? 
                    FormBorderStyle.Sizable : FormBorderStyle.None;
            }
        }
    }
}

Form1.Designer.cs

using System.Diagnostics;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    partial class Form1
    {
        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }

            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(800, 450);
            this.Text = "Form1";

            WindowState = FormWindowState.Maximized;
            FormBorderStyle = FormBorderStyle.None;

            this.KeyUp += OnKeyUp;
        }
    }
}

我尝试调试外部代码,发现 WinAPI 会返回这种转变。

显示器:戴尔 U2412M。比例:100%。分辨率:1920x1200。

标签: c#winformswinapilocation

解决方案


推荐阅读