How to Make My Windows Form App Snap to Screen Edges

How to make my Windows Form app snap to screen edges?

This worked pretty well, works on multiple monitors, observes the taskbar:

  public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private const int SnapDist = 100;
private bool DoSnap(int pos, int edge) {
int delta = pos - edge;
return delta > 0 && delta <= SnapDist;
}
protected override void OnResizeEnd(EventArgs e) {
base.OnResizeEnd(e);
Screen scn = Screen.FromPoint(this.Location);
if (DoSnap(this.Left, scn.WorkingArea.Left)) this.Left= scn.WorkingArea.Left;
if (DoSnap(this.Top, scn.WorkingArea.Top)) this.Top = scn.WorkingArea.Top;
if (DoSnap(scn.WorkingArea.Right, this.Right)) this.Left = scn.WorkingArea.Right - this.Width;
if (DoSnap(scn.WorkingArea.Bottom, this.Bottom)) this.Top = scn.WorkingArea.Bottom - this.Height;
}
}

How do I snap a borderless Form to the edges of a screen?

Aero Snap requires a window with a border, no back-door. You could emulate snapping with the code in this post.

That's still a far cry from the interactive feedback the user gets from Aero Snap. There is more than one way to get a borderless window, another way is by intercepting the WM_NCCALCSIZE message. A message that Windows sends to give an app the opportunity to override the client area size of a window. That's very easy to do, set the FormBorderStyle property back to Sizable and paste this code into your Form class:

    protected override void WndProc(ref Message m) {
const int WM_NCCALCSIZE = 0x83;
if (m.Msg == WM_NCCALCSIZE && m.WParam.ToInt32() == 1) {
m.Result = new IntPtr(0xF0); // Align client area to all borders
return;
}
base.WndProc(ref m);
}

Beware that you probably already have overridden this method to make the window sizable. Just update it with this code.

Every hack like this produces yet another problem, the client area of your window will now be too large. Larger by the size of the borders and the window caption. Fixing this is tricky, Aero lies about border sizes and you have to ensure that auto-scaling for DPI still works correctly. Set FormBorderStyle back to None and make the constructor of the Form look like this:

    public Form1() {
InitializeComponent();
var designSize = this.ClientSize;
this.FormBorderStyle = FormBorderStyle.Sizable;
this.Size = designSize;
}

Keep in mind that even though the window now has the border style flag turned on, you still won't get a drop-shadow. Hard to fix, CS_DROPSHADOW is as good as it gets.

How to automatically snap a WPF window to an edge of the screen while retaining its size?

There is no API calls you can make (as far as I've seen) to use the Windows snapping features, however you could just get the System.Windows.Forms.Screen.PrimaryScreen.WorkingArea of the screen and set your Top, Left, Height and Width Properties of your Window accordingly.

Edit: The above suggestion does require Forms, which you probably don't want. I believe the WPF equivalent is System.Windows.SystemParameters.WorkArea

WinForm : How to exclusively dock on a side of a screen

You could use this awesome solution over at code project (the author Corneliu is/was a MVP): http://www.codeproject.com/Articles/6045/Sticky-Windows-How-to-make-your-top-level-forms-to

Otherwise there is a great implementation from Hans Passant here: How to make my Windows Form app snap to screen edges?

Edit:

If you want to restrict other programs from using the space then try Arik Poznanski's c# Application Desktop Toolbars method:
http://www.codeproject.com/Articles/3728/C-does-Shell-Part-3

Disable Windows snapping form when dragged beyond screen top

I was able to solve this using a small trick. Mainly be handling the ResizeEnd event and tracing the mouse pointer location between the MouseDown event and the ResizeEnd event. The difference is then added/subtracted to/from the initial form's Y coord that was saved in the MouseDown event and then simply just setting the location of my form to that coordinate.

I am still open to other answers if anyone can provide a different one as this cause some sort of flicker because of the location difference between when Windows snaps the form and when the code forces its location.

Windows Forms Force everything half screen and put my form on top

There is an artice which covers it here - http://www.codeproject.com/KB/shell/csdoesshell3.aspx

The article contains a finished library which can be used in your own application.



Related Topics



Leave a reply



Submit