Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

wpf

As the application starts, I'd like my WPF window to automatically snap to the right edge of the screen. Is there a way to do that? I also want to be able to retain its dimensions. So, unlike the snapping behavior that happens when you drag a window to the edge of the screen, causing the window to resize to either a portion of the screen or full screen, I want my window to simply snap to the edge at a certain location by default or if dragged by the user to a specific location afterwards, without resizing. I still want to retain the ability of the user to drag the window away from the edge.

Is there anything like that already implemented or would I have to create my own behavior schema? I tried numerous search keyword combinations, but couldn't find anything similar to what I'm doing. Some of the searches included disabling snapping behavior or providing snapping behavior, but nothing in the way I described above.

EDIT:

I haven't been able to find a ready solution, so I wrote my own. This solution is based on BenVlodgi's suggestions, so I thank him for helping me out. This is a very rough implementation and still requires a lot of polishing and better code techniques, but it works and it's a good base for anyone wanting to try this. It's incredibly simple and works very well with WPF. The only limitation of this implementation is that I haven't tried getting it to work with two screens yet, but it's incredibly simple (I'm just not going to have time for it and I don't need that functionality at this point). So, here's the code and I hope that it helps someone out there:

public partial class MainWindow : Window
{
    // Get the working area of the screen.  It excludes any dockings or toolbars, which
    // is exactly what we want.
    private System.Drawing.Rectangle screen = 
                                 System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;

    // This will be the flag for the automatic positioning.
    private bool dragging = false;

    //  The usual initialization routine
    public MainWindow()
    {
        InitializeComponent();
    }

    // Wait until window is lodaded, but prior to being rendered to set position.  This 
    // is done because prior to being loaded you'll get NaN for this.Height and 0 for
    // this.ActualHeight.
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        // Sets the initial position.
        SetInitialWindowPosition();
        // Sets the monitoring timer loop.
        InitializeWindowPositionMonitoring();
    }

    // Allows the window to be dragged where the are no other controls present.
    // PreviewMouseButton could be used, but then you have to do more work to ensure that if
    // you're pressing down on a button, it executes its routine if dragging was not performed.
    private void Window_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        // Set the dragging flag to true, so that position would not be reset automatically.
        if (e.ChangedButton == System.Windows.Input.MouseButton.Left)
        {
            dragging = true;
            this.DragMove();
        }
    }

    // Similar to MouseDown.  We're setting dragging flag to false to allow automatic 
    // positioning.
    private void Window_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        if (e.ChangedButton == System.Windows.Input.MouseButton.Left)
        {
            dragging = false;
        }
    }

    // Sets the initial position of the window.  I made mine static for now, but later it can
    // be modified to be whatever the user chooses in the settings.
    private void SetInitialWindowPosition()
    {
        this.Left = screen.Width - this.Width;
        this.Top = screen.Height / 2 - this.Height / 2;
    }

    // Setup the monitoring routine that automatically positions the window based on its location
    // relative to the working area.
    private void InitializeWindowPositionMonitoring()
    {
        var timer = new System.Windows.Threading.DispatcherTimer();
        timer.Tick += delegate
        {
            // Check if window is being dragged (assuming that mouse down on any portion of the
            // window is connected to dragging).  This is a fairly safe assumption and held
            // true thus far.  Even if you're not performing any dragging, then the position
            // doesn't change and nothing gets reset.  You can add an extra check to see if
            // position has changed, but there is no significant performance gain.
            // Correct me if I'm wrong, but this is just O(n) execution, where n is the number of
            // ticks the mouse has been held down on that window.
            if (!dragging)
            {
                // Checking the left side of the window.
                if (this.Left > screen.Width - this.Width)
                {
                    this.Left = screen.Width - this.Width;
                }
                else if (this.Left < 0)
                {
                    this.Left = 0;
                }

                // Checking the top of the window.
                if (this.Top > screen.Height - this.Height)
                {
                    this.Top = screen.Height - this.Height;
                }
                else if (this.Top < 0)
                {
                    this.Top = 0;
                }
            }
        };

        // Adjust this based on performance and preference.  I set mine to 10 milliseconds.
        timer.Interval = new TimeSpan(0, 0, 0, 0, 10);
        timer.Start();
    }
}

Make sure that your window has the following:

MouseDown="Window_MouseDown"
MouseUp="Window_MouseUp"
WindowStartupLocation="Manual" 
Loaded="Window_Loaded"

Also, this doesn't work well with Windows native components of the window, such as the top bar, so I disable the style and create my own (which is actually good for me, since I don't want the windows style for this):

WindowStyle="None"
like image 470
B.K. Avatar asked Mar 03 '14 21:03

B.K.


1 Answers

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

like image 187
BenVlodgi Avatar answered Oct 03 '22 03:10

BenVlodgi