Live Chat Icon For mobile
Live Chat Icon

How do I prevent a user from moving a form at run time?

Platform: WinForms| Category: Form

The following code snippet (posted in the Windows Forms FAQ forums) shows how you can prevent a user from moving a form at run time:

protected override void WndProc(ref Message m)
const int WM_NCLBUTTONDOWN = 161;
const int WM_SYSCOMMAND = 274;
const int HTCAPTION = 2; 
const int SC_MOVE = 61456;

if((m.Msg == WM_SYSCOMMAND) && (m.WParam.ToInt32() == SC_MOVE))

if((m.Msg == WM_NCLBUTTONDOWN) && (m.WParam.ToInt32() == HTCAPTION))

base.WndProc (ref m);

Protected Overrides  Sub WndProc(ByRef m As Message)
const Integer WM_NCLBUTTONDOWN = 161
const Integer WM_SYSCOMMAND = 274
const Integer HTCAPTION = 2 
const Integer SC_MOVE = 61456
If (m.Msg = WM_SYSCOMMAND) &&(m.WParam.ToInt32() = SC_MOVE) Then
End If
If (m.Msg = WM_NCLBUTTONDOWN) &&(m.WParam.ToInt32() = HTCAPTION) Then
End If
MyBase.WndProc( m)
End Sub

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.