user2102327
user2102327

Reputation: 109

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

I have the WndProc method that prevents dragging the main form. I would like to prevent dragging a child form, created within the Form_Main constructor:

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.

Upvotes: 0

Views: 34

Answers (1)

Ilia Afzali
Ilia Afzali

Reputation: 429

Here is the way (In my idea) and i'm not sure that it's the best way :

Make a class with name LockedForm :

 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 :

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

And

Form form1 = new LockedForm();

Upvotes: 1

Related Questions