Click here to Skip to main content
Click here to Skip to main content

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.

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)

About the Author

DaveyM69
CEO Dave Meadowcroft
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
GeneralVery Nice PinmemberStark DaFixzer6-Jun-09 11:03 
GeneralRe: Very Nice PinmvpDaveyM697-Jun-09 3:59 
QuestionMoving a form without border Pinmemberalgunname1-Apr-08 3:42 
GeneralRe: Moving a form without border PinmemberDaveyM693-Apr-08 9:44 
GeneralRe: Moving a form without border Pinmemberalgunname20-Apr-08 13:32 
Questionbug? PinmemberHarveySaayman19-Feb-08 22:04 
AnswerRe: bug? PinmemberDaveyM6919-Feb-08 23:26 
GeneralRe: bug? PinmemberHarveySaayman19-Feb-08 23:31 
AnswerRe: bug? PinmemberDaveyM6919-Feb-08 23:50 
GeneralRe: bug? PinmemberHarveySaayman19-Feb-08 23:55 
GeneralRe: bug? PinmemberDaveyM6922-Feb-08 4:51 
Generalthanx PinmemberHarveySaayman19-Feb-08 19:14 
GeneralThanks for Sharing PinmvpJeffrey Walton19-Feb-08 16:44 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 25 Feb 2008
Article Copyright 2008 by DaveyM69
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid