Introduction
This code allows you to restrict a dialog form's movement area. I used VS2008, and the target framework is 2.0, but it works fine if changed to 3.0 or 3.5
Background
This idea came from a post on the C# message board. The poster's boss complained that dialog forms weren't contained within the MDI application that was being developed for him.
My first thought was 'what's his problem?', but after a little more thought, I kind of agreed with him. We develop MDI applications so we can have multiple windows within one defined environment. Why shouldn't any dialog be confined in there too?
The first two obvious things were to handle the Move
or LocationChanged
events, but both of these occur after the form has moved, so the form 'jumps' back - not very elegant. The solution comes from overriding WndProc
and catching a few Windows messages. A little bit of Marshalling, and that's it.
Actually, restricting the movement of the form was reasonably simple, but was slightly complicated because even though the form stopped, the mouse cursor continued to move.
Using the code
All the code is commented, and should be self explanatory.
In Form1
, the relevant Form
that the dialog is to be restricted to is passed to the Form2
(the restricted dialog) constructor. I have provided three different examples in the source/demo. There's also a fourth which creates a child form (Form3
), which in turn calls Form2
on button click.
Form2
is where the real stuff takes place. Three Windows messages are overridden: WM_NCLBUTTONDOWN
, WM_ENTERSIZEMOVE
, and WM_MOVING
.
WM_NCLBUTTONDOWN
occurs when the title bar (Non Client area) is Left clicked. Here, I have set a rectangle appropriate for the cursor, based on the limits supplied, the size, and the location of the dialog and the current position of the cursor.
When WM_ENTERSIZEMOVE
occurs, I simply clip the cursor to the rectangle set above.
WM_MOVING
is repeatedly sent when the form is moved. Each time this message is received, the Left, Top, Right, and Bottom coordinates are checked against the passed rectangle, and if outside the limits, Marshalling is used to disallow the move.
Usage
Probably, the easiest way to use this is to save Form2
as a template, so it can then be easily added to your project as many times as you need.
Credits
Thanks to Luc Pattyn who helped me find WndProc
initially, and Harvey Saayman who posted the initial problem!
Bugs
Doesn't respond exactly as expected if moving the form with the arrow keys (I'll continue to work on this).
History
- 19 Feb 2008 v1.00.
- 22 Feb 2008 v.1.01: Added an MDI child example to cover Harvey's problem, in the messages below. I moved a lot of the code from the calling form to the dialog form (makes more sense for it to be in there) and added options for close button disable and border width allowances.
- 25 Feb 2008 v1.02: Added option to enable, disable, or hide the form's system context menu close item (along with disabling the close button), and disabled closing by Alt+F4 if above is disabled or hidden.