Q: Keep Your Window
Visible
How can I prevent a user from moving a window off the screen? I'd like
to allow users to move a window around the desktop at will, but not
allow them to move any part of the window off the desktop. The entire
window must be viewable at all times.
A:
Hopefully, this is a client specification and not a method to keep some
sort of popup advertisement perpetually in someone's face. (Online, a
<g> would probably follow, but irritating users is rarely funny.)
Assuming a legitimate need, the simple answer is that you'd want to hook
your form's message stream and respond to incoming WM_MOVING messages
(see Additional Resources). If you don't have a favorite drop-in
subclassing module, I'd urge you to grab HookMe.zip
from my Web site or this column's sample
code.
Windows sends WM_MOVING messages to a window immediately prior to the
user getting any feedback. These messages are accompanied by a pointer
to a RECT structure in lParam that contains the drag rectangle
coordinates Windows displays to the user. You're only given a pointer,
so you need to copy the data at this address to a RECT structure
declared within your hook procedure (see Listing
1).
At this point, you're free to examine the RECT coordinates and even
modify them to suit your needs. In this case, you'd want to ensure that
none of the edges go past the edge of the screen, and if they do,
correct them to remain onscreen. After any necessary modifications, copy
the updated structure back to the same address passed in lParam and tell
Windows you've handled the message by returning True for the function
result. —K.E.P.
About the Author
Karl E. Peterson is a GIS analyst with a regional
transportation-planning agency and serves as a member of the Visual
Studio Magazine Technical Review and Editorial Advisory Boards.
Online, he's a Microsoft MVP and a section leader on several VSM
forums. Find more of Karl's VB samples at www.mvps.org/vb.
|