Click here to Skip to main content
15,861,172 members
Articles / Desktop Programming / Win32
Article

Movement Restricted Dialog Form

Rate me:
Please Sign up or sign in to vote.
4.39/5 (11 votes)
25 Feb 2008CPOL3 min read 71.7K   2.2K   26   13
How to restrict a dialog form's movement area. Useful for MDI applications.

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.

License

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


Written By
CEO Dave Meadowcroft
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralVery Nice Pin
I Believe In GOD6-Jun-09 11:03
I Believe In GOD6-Jun-09 11:03 
GeneralRe: Very Nice Pin
DaveyM697-Jun-09 3:59
professionalDaveyM697-Jun-09 3:59 
QuestionMoving a form without border Pin
algunname1-Apr-08 3:42
algunname1-Apr-08 3:42 
GeneralRe: Moving a form without border Pin
DaveyM693-Apr-08 9:44
professionalDaveyM693-Apr-08 9:44 
GeneralRe: Moving a form without border Pin
algunname20-Apr-08 13:32
algunname20-Apr-08 13:32 
Questionbug? Pin
Harvey Saayman19-Feb-08 22:04
Harvey Saayman19-Feb-08 22:04 
AnswerRe: bug? Pin
DaveyM6919-Feb-08 23:26
professionalDaveyM6919-Feb-08 23:26 
GeneralRe: bug? Pin
Harvey Saayman19-Feb-08 23:31
Harvey Saayman19-Feb-08 23:31 
AnswerRe: bug? Pin
DaveyM6919-Feb-08 23:50
professionalDaveyM6919-Feb-08 23:50 
GeneralRe: bug? Pin
Harvey Saayman19-Feb-08 23:55
Harvey Saayman19-Feb-08 23:55 
GeneralRe: bug? Pin
DaveyM6922-Feb-08 4:51
professionalDaveyM6922-Feb-08 4:51 
Generalthanx Pin
Harvey Saayman19-Feb-08 19:14
Harvey Saayman19-Feb-08 19:14 
GeneralThanks for Sharing Pin
Jeffrey Walton19-Feb-08 16:44
Jeffrey Walton19-Feb-08 16: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.