Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intercept a move event in a WPF Window before the move happens on the screen?

Tags:

wpf

I have an app where I create a Window in WPF that the user can drag around. I'd like to limit it so it doesn't go over the parent window's main menu. If I handle the WindowLocationChanged event it's fired after the change has already taken place so the only thing I can do is force the window back to some other position, which creates bad visual effects.

Is there any event I can handle before the move has actually occurred so I can incercept the window and force it to stay within certain limits? Some commercial WPF libraries support an OnMoving event which gets fired before the move actually happens on the screen. Is there anything like that in native WPF (staying within the managed code environment, if possible)?

Alternatively, is there any way to use dependency properties to set max or min x,y values? (N.B., I'm moving it, not resizing it)

Thanks in advance.

like image 461
user316117 Avatar asked Sep 11 '12 18:09

user316117


1 Answers

This example is a demo to show how to prevent the Window from being moved below it's starting X/Y position...it should give you a starting point to what you want to do.

Note, it's possible when resizing to alter the top and left position which is why the WM_SIZING message is being handled too to ensure the resize doesn't do that.

If you don't need that then just take out the WM_SIZING case.

    <Window x:Class="WpfApplication12.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
        <Grid>

        </Grid>
    </Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Interop;
using System.Runtime.InteropServices;

namespace WpfApplication12
{
    [StructLayout(LayoutKind.Sequential)]
    public struct WIN32Rectangle
    {
        public int Left;
        public int Top;
        public int Right;
        public int Bottom;
    }

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        const int WM_SIZING = 0x0214;
        const int WM_MOVING = 0x0216;

        private Point InitialWindowLocation;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            WindowInteropHelper helper = new WindowInteropHelper(this);
            HwndSource.FromHwnd(helper.Handle).AddHook(HwndMessageHook);

            InitialWindowLocation = new Point(this.Left, this.Top);
        }

        private IntPtr HwndMessageHook(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam, ref bool bHandled)
        {
            switch (msg)
            {
               // You may prefer to check the specific size action being done
               //
               //case WM_SIZING:
               //     {
               //         case (wParam)
               //         {
               //             case WMSZ_LEFT:
               //             {

               //             }
               //             break;

               //             case WSSZ_TOP:
               //             {
               //             }
               //             break;

               //             case WSSZ_TOPLEFT:
               //             {
               //             }
               //             break;
               //         }
               //     }
               //     break;

                case WM_SIZING:
                case WM_MOVING:
                    {
                        WIN32Rectangle rectangle = (WIN32Rectangle)Marshal.PtrToStructure(lParam, typeof(WIN32Rectangle));

                        if (rectangle.Left < this.InitialWindowLocation.X)
                        {
                            rectangle.Left = (int)this.InitialWindowLocation.X;
                            rectangle.Right = (int)this.Left + (int)this.Width;

                            bHandled = true;
                        }

                        if (rectangle.Top < this.InitialWindowLocation.Y)
                        {
                            rectangle.Top = (int)this.InitialWindowLocation.Y;
                            rectangle.Bottom = (int)this.Top + (int)this.Height;

                            bHandled = true;
                        }

                        if (bHandled)
                        {
                            Marshal.StructureToPtr(rectangle, lParam, true);
                        }
                    }
                    break;

            }
            return IntPtr.Zero;
        }
    }
}
like image 86
CSmith Avatar answered Sep 24 '22 04:09

CSmith