Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
207 views
in Technique[技术] by (71.8m points)

c# - Why a WinForms form is shifted (left -7, -8) after toggling FormBorderStyle property?

I'm developing a WinForms application with a functionality of hiding and showing a title bar by clicking on F3 button.

Everything works fine, but I have multiple monitors and when I toggle the visibility of the title bar (window is maximized) I see the shift of the window to the left by -7, -8.

The code is below:

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

I've tried to debug external code and found that WinAPI returns this shift.

Monitors: Dell U2412M. Scale: 100%. Resolution: 1920x1200.

question from:https://stackoverflow.com/questions/65842078/why-a-winforms-form-is-shifted-left-7-8-after-toggling-formborderstyle-prop

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...