[英]Prevent a Child form from being dragged by means of the mouse
我有防止拖動主窗體的 WndProc 方法。 我想防止拖動在 Form_Main 構造函數中創建的子表單:
Form form1 = new Form();
防止拖動主窗體的方法是:
/// <summary>
/// Prevents Form_Main and any of the controls from being dragged by means of the mouse.
/// </summary>
/// <param name="messsage"></param>
protected override void WndProc(ref Message message)
{
int WM_NCLBUTTONDOWN = 0xA1;
int WM_SYSCOMMAND = 0x112;
int HTCAPTION = 0x02;
int SC_MOVE = 0xF010;
if (message.Msg == WM_SYSCOMMAND && message.WParam.ToInt32() == SC_MOVE)
{
return;
}
if (message.Msg == WM_NCLBUTTONDOWN && message.WParam.ToInt32() == HTCAPTION)
{
return;
}
base.WndProc(ref message);
}
請幫忙。 提前謝謝你。
這是方法(在我的想法中),我不確定這是最好的方法:
制作一個名為 LockedForm 的 class:
public class LockedForm : Form
{
protected override void WndProc(ref Message message)
{
int WM_NCLBUTTONDOWN = 0xA1;
int WM_SYSCOMMAND = 0x112;
int HTCAPTION = 0x02;
int SC_MOVE = 0xF010;
if (message.Msg == WM_SYSCOMMAND && message.WParam.ToInt32() == SC_MOVE)
{
return;
}
if (message.Msg == WM_NCLBUTTONDOWN && message.WParam.ToInt32() == HTCAPTION)
{
return;
}
base.WndProc(ref message);
}
}
並從此 class 繼承您的 forms,就像這樣:
public partial class Frm_Main : LockedForm
{
public Frm_Main()
{
InitializeComponent();
}
}
和
Form form1 = new LockedForm();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.