Windows 7 "AeroSnap" feature
I apologize up front if this has been asked before. I searched for a thread already discussing this and failed. Most likely a PEBKAC error. :P But the last time I posted here for help, I got exactly what I was looking for so, figured I'd try again.
Basically, I want the projects I'm writing in Visual Basic Express 2008 to have the window snap functionality available in Windows 7. For those that haven't seen it, it's when a user drags the window to the edge of the screen, it automatically snaps to that edge and fills half the screen.
Any help here?
Thank you in advance!
Re: Windows 7 "AeroSnap" feature
You can handle the Move event of the form. In that event, check whether the form is near any of the bounds of any screen. You can get the bounds of the working area of all screens by looping through Screen.AllScreens. Not too sure about the syntax but it's something like
Code:
For Each s As Screen In Screen.AllScreens
Dim rect As Rectangle = s.WorkingArea
' use rect...
Next
The WorkingArea is the screen minus the taskbar and other toolbars. Use the Bounds property if you want the entire screen, including the taskbar.
You'd check each side individually.
Let's take the top as an example. The form is near the top if its Y position (Location.Y) is between the Y position of the screen minus some 'boundary' value, and the Y position of the screen plus some 'boundary' value.
In code:
Code:
If Me.Location.Y > s.WorkingArea.Y - boundary _
AndAlso Me.Location.Y < s.WorkingArea.Y + boundary Then
' snap to top
End If
Here's a 'picture' to clarify:
Code:
------------ s.WorkingArea.Y - boundary
============ s.WorkingArea.Y
------------ s.WorkingArea.Y + boundary
The form top must be between the dashed lines (while the double dashed line represents the top of the screen), so it must be more than s.WorkingArea.Y - boundary and at the same time less than s.WorkingArea.Y + boundary.
Note that the Y position is zero at the top, and increases to the bottom, so more than ___ means lower than ___ for your eyes.
(boundary is something like 25 pixels, dunno. You could measure it in Windows 7 or you could just take a value you like)
Once you know that the form is in this area, you can snap it. Snapping is as easy as setting the bounds of the form. For the top, you'll want to set the Location to the Location of the screen, and the Size to the Size of the (working area of the) screen. For the left/right sides (dunno about bottom, there isn't any docking there is there?), instead of juts matching the Size, you only set the Height to the Height of the (working area of the) screen, and set the Width to some fixed value (or maybe half the screen width, whatever you like best).
Re: Windows 7 "AeroSnap" feature
Actually Nick, it's a little bit different than that. When you're dragging a window, the snap zones only cause the window to fill the left half, the right half, or be maximized (the top of the desktop) when the mouse cursor enters the zone while dragging (which the zone is something like 2 or 3 pixels, pretty narrow) and even then it shows a glass preview of what the window will do and doesn't actually do it until the mouse button is released.
So he'll need to check the cursor's position only while dragging the window in his app, as for when it takes effect (either immediately or when the mouse button's released) it's up to the OP to decide.
Another thing I should mention is, in Windows 7 when you resize the window (vertically) when the cursor hits the zone at the top, the window (again does the glass preview until the mouse button's released) will be the same height as the desktop. Likewise if you're resizing grabbed from the bottom edge of the window and you hit the zone just above the taskbar.
Also, Windows 7 does this automatically, even for .Net apps so the code he writes to mimic this only has to run if the user is running something older than Win7. I don't know if Server 2008 RC2 has it or not as I haven't used it yet.
Re: Windows 7 "AeroSnap" feature
Yes, I didn't think of the glass preview, but I don't see why you'd want to add that anyway. It would only be useful in Vista, as XP can't show glass anyway (or perhaps you could use a transparent form instead?), and Windows 7 already has the feature as you mention.
From reading the first post I thought the question was basically how to make the form snap, and whether or not the OP wants to implement the behavior as accurately as possible is questionable. If he does, I'm sure he'll ask that next :p
And yeah, you're right that the boundary is very small in Windows 7. I was thinking of normal docking, in which case the boundary is usually much greater. In fact, I wouldn't be surprised if there was no boundary at all, and that the snap only happens when you actually hit the border instead of just hovering beneath it. I'm not on Win7 now so I can't test it, but I could test it later tonight when I get home.
Re: Windows 7 "AeroSnap" feature
Having gotten snagged (snapped) by accident a couple times by it (until I figured it out)... it's when the cursor hits the edge of the desktop while dragging a window... It is a very narrow area, but it's enough. A cool feature I wish I had in XP at work.
-tg
Re: Windows 7 "AeroSnap" feature
tg there is an app (addin) for XP called AeroSnap..... I use it on my XP systems works well.
Re: Windows 7 "AeroSnap" feature
Considering the lockdown on these things... I kind of doubt I'd be able to get it to install.
-tg