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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…