Skip to content

Commit

Permalink
Port DragMove logic from WPF (#14186)
Browse files Browse the repository at this point in the history
* Port DragMove logic from WPF to fix missing PointerReleased event on drag out.

* remove WindowState.Normal check
  • Loading branch information
jmacato authored and maxkatz6 committed Jan 17, 2024
1 parent d0267c8 commit 366a935
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1180,7 +1180,12 @@ public static extern IntPtr CreateWindowEx(

[DllImport("user32.dll", EntryPoint = "DefWindowProcW")]
public static extern IntPtr DefWindowProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);


public const int SC_MOUSEMOVE = 0xf012;

[DllImport("user32.dll", CharSet = CharSet.Unicode, EntryPoint = "SendMessageW")]
public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

[DllImport("user32.dll", EntryPoint = "DispatchMessageW")]
public static extern IntPtr DispatchMessage(ref MSG lpmsg);

Expand Down
18 changes: 16 additions & 2 deletions src/Windows/Avalonia.Win32/WindowImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,22 @@ public void SetParent(IWindowImpl? parent)
public void BeginMoveDrag(PointerPressedEventArgs e)
{
e.Pointer.Capture(null);
DefWindowProc(_hwnd, (int)WindowsMessage.WM_NCLBUTTONDOWN,
new IntPtr((int)HitTestValues.HTCAPTION), IntPtr.Zero);

// Mouse.LeftButton actually reflects the primary button user is using.
// So we don't need to check whether the button has been swapped here.
if (e.Pointer.IsPrimary)
{
// SendMessage's return value is dependent on the message send. WM_SYSCOMMAND
// and WM_LBUTTONUP return value just signify whether the WndProc handled the
// message or not, so they are not interesting

SendMessage(_hwnd, (int)WindowsMessage.WM_SYSCOMMAND, (IntPtr)SC_MOUSEMOVE, IntPtr.Zero);
SendMessage(_hwnd, (int)WindowsMessage.WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);
}
else
{
throw new InvalidOperationException("BeginMoveDrag Failed");
}
}

public void BeginResizeDrag(WindowEdge edge, PointerPressedEventArgs e)
Expand Down

0 comments on commit 366a935

Please sign in to comment.