C#设置MDI子窗体不可移动
时间: 2023-11-01 09:54:02 浏览: 55
要设置 C# MDI 子窗体不可移动,可以使用以下代码:
```csharp
private void Form2_Load(object sender, EventArgs e)
{
this.ControlBox = false; // 隐藏控制按钮
this.FormBorderStyle = FormBorderStyle.None; // 设置无边框样式
}
private const int WM_NCLBUTTONDOWN = 0xA1;
private const int HT_CAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
private void Form2_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
```
这里的代码实现了两个功能:
1. 隐藏控制按钮,即最大化、最小化和关闭按钮。
2. 通过重写 Form 的 MouseDown 事件,实现鼠标拖拽窗口的功能。这里只实现了鼠标左键拖拽窗口的功能,右键和中键不会触发拖拽。
这样就可以实现 C# MDI 子窗体不可移动的效果了。