| You are here: | About>Computing & Technology>Delphi Programming> Coding Delphi Applications> Forms, Dialogs, MDI, SDI> Docking Delphi Forms to Screen Edges |
![]() | Delphi Programming |
![]() Screen Docking Delphi Form Join the DiscussionPost your views, comments, questions and doubts to this article. Docking Delphi Forms to Screen EdgesThere is no built-in feature of the Win API that allows windows to snap to the screen edge - we will have to deal with Windows messages. Delphi makes message handling easy through its use of events; an event is usually generated in response to a Windows message being sent to an application. Even though many Windows messages are handled by Delphi events, some messages are left for us to handle. For example, we know if a form was resized with the Resize event (OnResize) - Delphi handles the WM_SIZE message, but how do we know if a form has moved? The Delphi form can get the message, but initially does not do anything with it. The WM_MOVING message is sent to a window that the user is moving. By processing this message, an application can monitor the size and position of the drag rectangle and, if needed, change its size or position. The WM_WINDOWPOSCHANGING message is sent to a window whose size, position, or place in the Z order is about to change as a result of a call to the SetWindowPos function or another window-management function. Most of the time a simple message is not enough, we have to have parameters to tell us additional information. For example, the WM_MOVE message tells us that our Form has changed position but it is the LPARAM parameter that gives us the X and Y position. With the WM_WINDOWPOSCHANGING message we "only" get one parameter - the one that points to a WindowPos structure that contains information about the window's new size and position. This is how the WindowPos structure is defined: typeOur task is simple: we want a Delphi form to stick to the screen edge when the edge of the form is within a certain distance (say 20 pixels) of the edge of the screen. The WM_WindowPosChanginh messageOn a new Delphi form add a Label, one Edit control and four Check boxes. Change the name of the Edit control to edStickAt. Change the names of check boxes to chkLeft, chkTop, etc... We use the edStickAt to set the number of pixels that our form uses to snap to nearest edge if closer than edStickAt pixels and not docked.The only message we are interested in is the WM_WINDOWPOSCHANGING. The declaration is placed in the private part of the form declaration. I'll give you here the entire code for the "sticking" procedure along with the code descriptions. Note that you can prevent the form from snapping at the certain edge by unchecking the appropriate check box. SystemParametersInfo called with the SPI_GETWORKAREA as the first parameter retrieves the size of the working (desktop) area. We use it to determine the useable area of the screen minus appbars, taskbars, IE toolbars, etc. ... //interfaceNow simply run the project and move the form to any screen edge to dock it. Note: this article was written *before* the "ScreenSnap" and "SnapBuffer" were introduced as standard Delphi form properties. That's it. If you have any questions, post them on the Delphi Programming Forum! Here are some discussion ideas:
Join the DiscussionPost your views, comments, questions and doubts to this article. |
Las Vegas on a BudgetFind a BargainHotel DealsCheap EatsFree AttractionsEntertainment for Less |
All Topics | Email Article | | | ![]() |
| Advertising Info | News & Events | Work at About | SiteMap | Reprints | Help | Our Story | Be a Guide |
| User Agreement | Ethics Policy | Patent Info. | Privacy Policy | ©2008 About, Inc., A part of The New York Times Company. All rights reserved. |



