Monday, July 27, 2009

Restrict .Net Window Forms resizing

How to restrict the user from moving or resizing the .NET Windows
Forms?



There are some situations that we might want to prevent the user from
resizing(Maximizing, minimizing or double clicking the title bar etc) the
VB.Net Windows forms.



For example, we might not want to resize the form when the user double
click on the title bar of the Windows Forms.



This will be accomplished by overriding the color="#008000">WndProc Method.



WndProc Method – A method which handles the
windows messages (Maximizing the window, minimizing the window etc) posted
by the Windows Forms.



Based on the windows messages, we need to write the handler to handle
the messages.



The following VB.NET Code Snippet explains how to prevent



1) Maximization or Restoration of Form (When click on the Title bar of
the Form)



2) Maximize button click event



3) Minimize button click event



4) Moving of Form



5) Resizing the Form (Double click on Title bar of the Window)





Protected Overrides Sub WndProc(ByRef WndMsg As
Message)



'various windows message codes



Const WM_NCLBUTTONDOWN As Integer = 161



Const WM_NCLBUTTONDBLCLK As Int32 = &HA3 'When
User Clicks on the Title Bar



Const WM_SYSCOMMAND As Integer = 274 'Windows
Message



Const HTCAPTION As Integer = 2 'Title bar of
Form



Const SC_MOVE As Integer = 61456 'When the Form is
moved



Const SC_MINIMIZE As System.Int32 =
&HF020& 'When a Form is Minimized



Const SC_MAXIMIZE As System.Int32 =
&HF030& 'When a Form is Maximized



Const SC_RESTORE As System.Int32 = 61728 'When a
Form is Restored



If (WndMsg.Msg = WM_NCLBUTTONDBLCLK) And
(m.WParam.ToInt32() = HTCAPTION) Then 'Handle the Title Bar Click event as
well, this would be either Restore or Maximized



Return



End If



If (WndMsg.Msg = WM_SYSCOMMAND) And
(WndMsg.WParam.ToInt32() = SC_MAXIMIZE) Then



Return



End If



If (WndMsg.Msg = WM_SYSCOMMAND) And
(WndMsg.WParam.ToInt32() = SC_MINIMIZE) Then



Return



End If



If (WndMsg.Msg = WM_SYSCOMMAND) And
(WndMsg.WParam.ToInt32() = SC_MOVE) Then



Return



End If



If (WndMsg.Msg = WM_SYSCOMMAND) And
(WndMsg.WParam.ToInt32() = SC_RESTORE) Then



Return



End If



If (WndMsg.Msg = WM_NCLBUTTONDOWN) And
(WndMsg.WParam.ToInt32() = HTCAPTION) Then



Return



End If



MyBase.WndProc(m)



End Sub





color="#400000">USAGE:



Place the above code snippet in the form’s
Coding area where we want to do the above activities
.

No comments:

Post a Comment