Click here to Skip to main content
13,355,335 members (70,158 online)
Click here to Skip to main content
Add your own
alternative version


26 bookmarked
Posted 19 Feb 2008

Movement Restricted Dialog Form

, 25 Feb 2008
Rate this:
Please Sign up or sign in to vote.
How to restrict a dialog form's movement area. Useful for MDI applications.


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


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.</li />


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.


Thanks to Luc Pattyn who helped me find WndProc initially, and Harvey Saayman who posted the initial problem!


Doesn't respond exactly as expected if moving the form with the arrow keys (I'll continue to work on this).


  • 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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

CEO Dave Meadowcroft
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralVery Nice Pin
Stark DaFixzer6-Jun-09 12:03
memberStark DaFixzer6-Jun-09 12:03 
GeneralRe: Very Nice Pin
DaveyM697-Jun-09 4:59
mvpDaveyM697-Jun-09 4:59 
QuestionMoving a form without border Pin
algunname1-Apr-08 4:42
memberalgunname1-Apr-08 4:42 
GeneralRe: Moving a form without border Pin
DaveyM693-Apr-08 10:44
memberDaveyM693-Apr-08 10:44 
GeneralRe: Moving a form without border Pin
algunname20-Apr-08 14:32
memberalgunname20-Apr-08 14:32 
Questionbug? Pin
HarveySaayman19-Feb-08 23:04
memberHarveySaayman19-Feb-08 23:04 
AnswerRe: bug? Pin
DaveyM6920-Feb-08 0:26
memberDaveyM6920-Feb-08 0:26 
GeneralRe: bug? Pin
HarveySaayman20-Feb-08 0:31
memberHarveySaayman20-Feb-08 0:31 
AnswerRe: bug? Pin
DaveyM6920-Feb-08 0:50
memberDaveyM6920-Feb-08 0:50 
GeneralRe: bug? Pin
HarveySaayman20-Feb-08 0:55
memberHarveySaayman20-Feb-08 0:55 
GeneralRe: bug? Pin
DaveyM6922-Feb-08 5:51
memberDaveyM6922-Feb-08 5:51 
Generalthanx Pin
HarveySaayman19-Feb-08 20:14
memberHarveySaayman19-Feb-08 20:14 
GeneralThanks for Sharing Pin
Jeffrey Walton19-Feb-08 17:44
mvpJeffrey Walton19-Feb-08 17:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 25 Feb 2008
Article Copyright 2008 by DaveyM69
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid