5

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.

1 Answer 1

8

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;
        }
    }
}
3
  • This looks promising - I'll try it.
    – user316117
    Sep 12, 2012 at 17:20
  • It worked! Excellent. I wish there was some way on SO to give you more than +1 because you certainly deserve it for all the trouble this saves me. Thanks again!
    – user316117
    Sep 13, 2012 at 12:55
  • Note: this solution, as is, lacks DPI awareness handling: WinApi is in physical pixels, whereas WPF is in logical ones (VisualTreeHelper.GetDpi(this) obtains the ratio).
    – vines
    Jun 28, 2020 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.