C# 窗口拖动尺寸大小鼠标闪动,拖动不方便
时间: 2023-08-02 10:16:22 浏览: 144
C#实现Winform鼠标拖动窗口大小时设定窗口最小尺寸的方法
这种情况可能是因为窗口的边框样式设置不当所导致的。你可以尝试在窗口加载时,通过代码将窗口的边框样式设置为None,然后自己实现窗口的拖动与缩放功能。
具体实现可以参考以下代码:
```csharp
public partial class MainForm : Form
{
private const int WM_NCHITTEST = 0x0084;
private const int HTLEFT = 10;
private const int HTRIGHT = 11;
private const int HTTOP = 12;
private const int HTTOPLEFT = 13;
private const int HTTOPRIGHT = 14;
private const int HTBOTTOM = 15;
private const int HTBOTTOMLEFT = 16;
private const int HTBOTTOMRIGHT = 17;
private const int BORDER_WIDTH = 5;
public MainForm()
{
InitializeComponent();
// 设置窗口的边框样式为None
this.FormBorderStyle = FormBorderStyle.None;
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_NCHITTEST)
{
Point point = this.PointToClient(new Point(m.LParam.ToInt32() & 0xffff, m.LParam.ToInt32() >> 16));
if (point.X <= BORDER_WIDTH && point.Y <= BORDER_WIDTH)
{
m.Result = (IntPtr)HTTOPLEFT;
}
else if (point.X >= this.ClientSize.Width - BORDER_WIDTH && point.Y <= BORDER_WIDTH)
{
m.Result = (IntPtr)HTTOPRIGHT;
}
else if (point.X <= BORDER_WIDTH && point.Y >= this.ClientSize.Height - BORDER_WIDTH)
{
m.Result = (IntPtr)HTBOTTOMLEFT;
}
else if (point.X >= this.ClientSize.Width - BORDER_WIDTH && point.Y >= this.ClientSize.Height - BORDER_WIDTH)
{
m.Result = (IntPtr)HTBOTTOMRIGHT;
}
else if (point.X <= BORDER_WIDTH)
{
m.Result = (IntPtr)HTLEFT;
}
else if (point.X >= this.ClientSize.Width - BORDER_WIDTH)
{
m.Result = (IntPtr)HTRIGHT;
}
else if (point.Y <= BORDER_WIDTH)
{
m.Result = (IntPtr)HTTOP;
}
else if (point.Y >= this.ClientSize.Height - BORDER_WIDTH)
{
m.Result = (IntPtr)HTBOTTOM;
}
}
}
}
```
这段代码实现了窗口的边框拖动功能,当鼠标移动到窗口边缘时,鼠标的样式会改变,然后通过重写窗口的WndProc方法,实现窗口的拖动与缩放功能。
阅读全文