Handling System Commands in C#
Recently, at my work, I had to trap few system commands in C#. I had to disable the movement of Form and few other basic operations such as minimize, maximize, close while the buttons for them still kept visible.
Doing this is very simple. All you need to do is ‘override’ the ‘WndProc’ method inside the Form class.
A sample override is shown below. This checks for System Command corresponding to Form’s movement (i.e SC_MOVE) and if found, returns from the method without calling the base’s WndProc method. As a result, SC_MOVE is pre filtered out.
protected override void WndProc(ref Message message)
{
const int WM_SYSCOMMAND = 0x0112;
switch(message.Msg)
{
case WM_SYSCOMMAND:
int command = message.WParam.ToInt32() & 0xfff0;
//Disable the movement of the Form
if (command == (int)SystemCommands.SC_MOVE)
return;
}
base.WndProc(ref message);
}
Above code uses an enumeration called ‘SystemCommands’. This enumeration is not inbuilt. You will have to define it youself.
A sample of this enumeration can be found at following location.
http://msdn.microsoft.com/en-us/library/ms646360(VS.85).aspx
Following sample is the same as the one in above location.
public enum SystemCommands
{
SC_SIZE = 0xF000,
SC_MOVE = 0xF010,
///
/// Sent when form minimizes
///
SC_MINIMIZE = 0xF020,
///
/// Sent when form maximizes
///
SC_MAXIMIZE = 0xF030,
///
/// Sent when form maximizes because of doubcle click on caption
///
SC_MAXIMIZE2 = 0xF032,
SC_NEXTWINDOW = 0xF040,
SC_PREVWINDOW = 0xF050,
///
/// Sent when form maximizes because of doubcle click on caption
///
SC_CLOSE = 0xF060,
SC_VSCROLL = 0xF070,
SC_HSCROLL = 0xF080,
SC_MOUSEMENU = 0xF090,
SC_KEYMENU = 0xF100,
SC_ARRANGE = 0xF110,
///
/// Sent when form is maximized from the taskbar
///
SC_RESTORE = 0xF120,
///
/// Sent when form maximizes because of doubcle click on caption
///
SC_RESTORE2 = 0xF122,
SC_TASKLIST = 0xF130,
SC_SCREENSAVE = 0xF140,
SC_HOTKEY = 0xF150,
SC_DEFAULT = 0xF160,
SC_MONITORPOWER = 0xF170,
SC_CONTEXTHELP = 0xF180,
SC_SEPARATOR = 0xF00F
}
Filed under: .NET, C Sharp | Leave a Comment
Tags: Disable Form Movement, SC_MOVE, System Commands
No Responses Yet to “Handling System Commands in C#”