'Winform remain unexpected painted color on other screens after snap to main screen's corner
I am creating a simple borderless winform witch remain window snapping feature.
But when I snap winform to the corner of some screen.
It still remain some unexpected painted color on other screens as in example Picture1.
I have attached Form1 in c# code here for your reviewing.
Your help or any suggesting would be appreciated if I am wrong or missing somethings.
If another way to archive this, please do not hesitate to let me know.
Thank you in advance.
Picture1:
namespace Test.FormApp
{
public partial class Form1 : Form
{
private const int WM_NCCALCSIZE = 0x0083;
private const int WM_NCHITTEST = 0x0084;
private const int HTCAPTION = 2;
public Form1()
{
InitializeComponent();
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.DoubleBuffered = true;
}
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_NCCALCSIZE:
if (m.WParam != IntPtr.Zero)
{
return; // Leave out when WParam != 0 to eliminate Form1 borders but remain Window snapping feature.
}
break;
case WM_NCHITTEST:
if (m.Result == IntPtr.Zero)
{
m.Result = (IntPtr)HTCAPTION; // Just to move Form1 to demonstrate the problem in Picture1.
return;
}
break;
}
base.WndProc(ref m);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
using (var gp = new GraphicsPath())
{
// Painting Form1 rounded rectangle borders to investigate the problem in Picture1.
var rect = new Rectangle(0, 0, this.Width, this.Height);
gp.AddRectangle(rect);
rect.Inflate(-20, -20);
gp.AddRectangle(rect);
e.Graphics.FillPath(Brushes.Green, gp);
}
}
}
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(682, 456);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

