简体   繁体   English

防止通过鼠标拖动子窗体

[英]Prevent a Child form from being dragged by means of the mouse

I have the WndProc method that prevents dragging the main form.我有防止拖动主窗体的 WndProc 方法。 I would like to prevent dragging a child form, created within the Form_Main constructor:我想防止拖动在 Form_Main 构造函数中创建的子表单:

Form form1 = new Form();

The method to prevent from dragging the main form is:防止拖动主窗体的方法是:

/// <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);
    }

Please help.请帮忙。 Thank yoiu in advance.提前谢谢你。

Here is the way (In my idea) and i'm not sure that it's the best way:这是方法(在我的想法中),我不确定这是最好的方法:

Make a class with name LockedForm:制作一个名为 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);
        }
 }

And inherit your forms from this class, just like this:并从此 class 继承您的 forms,就像这样:

public partial class Frm_Main : LockedForm
{
        public Frm_Main()
        {
            InitializeComponent();
        }       
} 

And

Form form1 = new LockedForm();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM