c# - Windows Forms Move Borderless Window
问题描述
i have a Windows Forms application which should be borderless (even without the titlebar) and be resizeable and moveable.
so far, i've set the BorderStyle to 'none' which removes all the borders and let my program look pretty.
Now i've added invisble borders with the following:
private const int cGrip = 16; // Grip size
private const int cCaption = 50; // Caption bar height;
protected override void OnPaint(PaintEventArgs e)
{
Rectangle rc = new Rectangle(this.ClientSize.Width - cGrip, this.ClientSize.Height - cGrip, cGrip, cGrip);
ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor, rc);
rc = new Rectangle(0, 0, this.ClientSize.Width, cCaption);
//e.Graphics.FillRectangle(Brushes.DarkBlue, rc);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x84)
{ // Trap WM_NCHITTEST
Point pos = new Point(m.LParam.ToInt32());
pos = this.PointToClient(pos);
if (pos.Y < cCaption)
{
m.Result = (IntPtr)2; // HTCAPTION
return;
}
if (pos.X >= this.ClientSize.Width - cGrip && pos.Y >= this.ClientSize.Height - cGrip)
{
m.Result = (IntPtr)17; // HTBOTTOMRIGHT
return;
}
}
base.WndProc(ref m);
}
The blue rectangle is rendered via the OnPaint() method and shows the field where the user is able to move the window while holding the left mouse button.
My problem is, that this rectangle is below my label. Does anyone know how to get the rectangle in front of the label?
Another way would be disabling the label which than turns dark grey. You would solve my problem if i could change the color of my disabled label.
解决方案
Leave the Label enabled and make it so that dragging the label causes the form to be dragged as well:
public const int HT_CAPTION = 0x2;
public const int WM_NCLBUTTONDOWN = 0xA1;
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
private void label1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
推荐阅读
- java - 以编程方式在 Maven 中心获取捆绑包
- mysql - 如何获取我以前知道的 2 行中常见的相同聊天 ID
- javascript - 具有多个孩子的基准继承
- matlab - 通过平均将点云转换为体素
- typo3 - 在 Typo3 版本 9.5 中一次添加多个页面
- vb.net - COM Visible Assembly - 无法通过其公开的接口方法访问的对象方法
- laravel - 会话未在 laravel 中持续存在
- stata - 使用oprobit时导出组合表
- azure-devops - 如何比较两个 Azure(发布)管道?
- php - 如何使用“|”将文本区域中的值插入数据库 人物?