by Slaystation (1 Submission)
Category: Custom Controls/Forms/Menus
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating:
(3 Votes)
Snapping a form to the edge of the screen is easy enough in VB... that is, if you don't mind subclassing. If you'd rather not have to worry about subclassing and the pitfalls involved then these solutions may work for you.
The implementation issue with VB is that it provides no mechanism for determining when you've clicked on the caption bar. The two examples presented in this article show how you can overcome this limitation in a couple of ways without resorting to subclassing.
The first example, "Snap with Caption using Timer", shows how you can use a standard VB Timer control to determine when the mouse is requesting a form to move. The form's Moveable property is set to False at design-time and all movements are handled by our code. This allows us to take full control over positioning and snapping behavior.
The second example, "Custom Snap Areas", demonstrates how snapping can be done without a Timer, so long as you don't mind having a captionless form. This example also demonstrates setting up custom snap areas independent of the screen size.
Thanks for reading and I hope you find my code useful.
Download Create custom snap areas for your VB forms (Updated) (6 KB)