|
This sample uses native subclassing to monitor WM_WINDOWPOSCHANGING notifications, and demonstrates how simple it is to "snap" a dialog to the edge of the screen as the user drags it around. You've probably seen this effect in numerous little utilities like WinAmp. It's so subtle, many users wouldn't even notice it, but it definitely adds a touch of polish to nearly any application.
CSnapDialog is implementated as a drop-in ready class which hooks any given form in your app and completely handles all the "messy" details. You set it up, quite simply, like this:
Private m_Snap As CSnapDialog Private Sub Form_Load() ' Setting up is a snap! Set m_Snap = New CSnapDialog m_Snap.hWnd = Me.hWnd m_Snap.SnapGap = 20 '(optional) End SubThe optional SnapGap property (default: 15) sets the "snap tolerance" in pixels. Anytime your user drags the form within this many pixels of the edge of any monitor on their system, CSnapDialog snaps the form to the nearest edge or corner. No teardown is required, as that's completely encapsulated within the class.
This sample, or the one from which it originally derived, was published (or at least peripherally mentioned) in the following article(s):
- Take Control of Window Movements, Classic VB Corner, VSM Online, April 2008
- Subclassing the XP Way, Classic VB Corner, VSM Online, July 2009
This sample uses the following API calls:
Module Library Function CSnapDialog.cls kernel32
user32RtlMoveMemory
GetMonitorInfo
GetSystemMetrics
IsWindow
MonitorFromWindow
SystemParametersInfoMHookMe.bas kernel32
user32OutputDebugString
RtlMoveMemory
CallWindowProc
GetProp
GetWindowLong
SetProp
SetWindowLongDon't see what you're looking for? Here's a complete API cross-reference.
Please, enjoy and learn from this sample. Include its code within your own projects, if you wish. But, in order to insure only the most recent code is available to all, I ask that you don't share the sample by any form of mass distribution. Download SnapDialog.zip, 12Kb, Last Updated: Friday, March 28, 2008
The following resources may also be of interest:
- HookMe - Native subclassing technique for VB5/6. Extremely clean.
- HookXP - Demonstrates native Windows subclassing method with no risk of improper teardown. Very flexible!
- Monitors - Provides a collection of objects representing each monitor on the system, and a module of functions for monitor tricks.