How to get the location value of WPF window when dragged after "Show window content while dragging" is turned off ?

Niranjan Kumar Gopalan 61 Reputation points
2021-07-15T10:51:33.927+00:00

Hi ,

I have been trying to get the location of the WPF window when the "Show window content while dragging" is turned off as shown below. The LocationChanged event gets invoked only when the mouse is up after stopped dragging the window.

114999-image.png

So I have used the IntPtr solution to get the LocationChange method on dragging the WPF window with the following code, but the location of window does not update while dragging. Please provide a suitable solution for this query.

 public partial class CustomWindow : Window  
    {  
        private const int WM_MOVING = 0x0216;  
        private HwndSource _hwndSrc;  
        private HwndSourceHook _hwndSrcHook;  
        private IntPtr hWnd;  
  
        public CustomWindow()  
        {  
            InitializeComponent();  
  
            this.Loaded += CustomWindow_Loaded;  
            this.Unloaded += CustomWindow_Unloaded;  
            this.LocationChanged += CustomWindow_LocationChanged;  
        }  
  
        private void CustomWindow_Unloaded(object sender, RoutedEventArgs e)  
        {  
            _hwndSrc.RemoveHook(_hwndSrcHook);  
            _hwndSrc.Dispose();  
            _hwndSrc = null;  
        }  
  
        private void CustomWindow_Loaded(object sender, RoutedEventArgs e)  
        {  
            _hwndSrc = HwndSource.FromDependencyObject(this) as HwndSource;  
            _hwndSrcHook = FilterMessage;  
            _hwndSrc.AddHook(_hwndSrcHook);  
            hWnd = new WindowInteropHelper(this).Handle;  
        }  
  
        private IntPtr FilterMessage(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)  
        {  
            switch (msg)  
            {  
                case WM_MOVING:  
                    OnLocationChange();  
                    break;  
            }  
  
            return IntPtr.Zero;  
        }  
  
        private void OnLocationChange()  
        {  
            Rect windowRect = new Rect(this.Left, this.Top, this.Width, this.Height);  
        }  
  
        private void CustomWindow_LocationChanged(object sender, EventArgs e)  
        {  
             
        }  
    }  
Developer technologies | Windows Presentation Foundation
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2021-07-15T13:13:34.553+00:00

    Try this:

    [StructLayout(LayoutKind.Sequential)]
    struct RECT
    {
       public Int32 left;
       public Int32 top;
       public Int32 right;
       public Int32 bottom;
    }
    
    . . .
    
    case WM_MOVING:
       RECT r = Marshal.PtrToStructure<RECT>( lParam );
       Rect rect = new Rect( r.left, r.top, r.right - r.left, r.bottom - r.top );
       // . . .
       break;
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Niranjan Kumar Gopalan 61 Reputation points
    2021-08-18T05:37:57.597+00:00

    Hi @Viorel ,

    Thanks for your answer it works perfectly.

    Can you please guide me on how to find the mouse position relative to an UIElement under same scenario ? I have tried using the Mouse.GetPosition(UIelement) but it returns the point values when mouse is pressed and mouse is released , but not on dragging.

    Regards,
    Niranjan Kumar

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.