Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8559797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:08:11+00:00 2026-06-11T16:08:11+00:00

I have an app where I create a Window in WPF that the user

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:08:12+00:00Added an answer on June 11, 2026 at 4:08 pm

    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;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm developing an application (wpf) that have 3 windows. in the main window user
I have to create app that provides online radio streaming (icecast), preferably .ogg format.
I have an app that allows you to create Home shortcuts to a specific
I have an iPhone app and would like to create iPad version of it.
I have a flex app which allows user to create some content. this content
I am totally new to WPF, I have created a simple WPF app that
I have a WPF TaskBar like application that i am developing for fun and
In a WPF 4 app, I have a very big user control full of
I have a WPF app that creates some text files in its own install
I have a WPF app that I'm testing on a Samsung XE700T1A tablet. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.