Skip to content

Commit

Permalink
Try to fix #2830
Browse files Browse the repository at this point in the history
  • Loading branch information
punker76 committed Feb 2, 2017
1 parent db10222 commit 41fd0a4
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 10 deletions.
2 changes: 2 additions & 0 deletions docs/release-notes/1.4.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
## Changes / Fixes

- Fix Flyout AutoClose sample
- Try to handle call fail of GetPhysicalCursorPos. [#2830](https://github.com/MahApps/MahApps.Metro/issues/2830)

## Closed Issues

- [#2832](https://github.com/MahApps/MahApps.Metro/issues/2832) Wrong window maximize on two monitor systems.
- [#2830](https://github.com/MahApps/MahApps.Metro/issues/2830) GetPhysicalCursorPos
14 changes: 10 additions & 4 deletions src/MahApps.Metro/MahApps.Metro.Shared/Controls/GlowWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,11 @@ private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref b
case WM.LBUTTONDOWN:
if (this.ownerHandle != IntPtr.Zero && UnsafeNativeMethods.GetWindowRect(this.ownerHandle, out rect))
{
var pt = WinApiHelper.GetRelativeMousePosition(this.handle);
NativeMethods.PostMessage(this.ownerHandle, (uint)WM.NCLBUTTONDOWN, (IntPtr)this.getHitTestValue(pt, rect), IntPtr.Zero);
Point pt;
if (WinApiHelper.TryGetRelativeMousePosition(this.handle, out pt))
{
NativeMethods.PostMessage(this.ownerHandle, (uint)WM.NCLBUTTONDOWN, (IntPtr)this.getHitTestValue(pt, rect), IntPtr.Zero);
}
}
break;
case WM.NCHITTEST:
Expand All @@ -313,8 +316,11 @@ private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref b
{
if (this.ownerHandle != IntPtr.Zero && UnsafeNativeMethods.GetWindowRect(this.ownerHandle, out rect))
{
var pt = WinApiHelper.GetRelativeMousePosition(this.handle);
cursor = this.getCursor(pt, rect);
Point pt;
if (WinApiHelper.TryGetRelativeMousePosition(this.handle, out pt))
{
cursor = this.getCursor(pt, rect);
}
}
}
if (cursor != null && cursor != this.Cursor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1246,7 +1246,7 @@ internal static void DoWindowTitleThumbMoveOnDragDelta(IMetroThumb thumb, [NotNu
// tage from DragMove internal code
window.VerifyAccess();

var cursorPos = WinApiHelper.GetPhysicalCursorPos();
//var cursorPos = WinApiHelper.GetPhysicalCursorPos();

// if the window is maximized dragging is only allowed on title bar (also if not visible)
var windowIsMaximized = window.WindowState == WindowState.Maximized;
Expand All @@ -1261,7 +1261,7 @@ internal static void DoWindowTitleThumbMoveOnDragDelta(IMetroThumb thumb, [NotNu

if (windowIsMaximized)
{
var cursorXPos = cursorPos.x;
//var cursorXPos = cursorPos.x;
EventHandler windowOnStateChanged = null;
windowOnStateChanged = (sender, args) =>
{
Expand Down
23 changes: 23 additions & 0 deletions src/MahApps.Metro/MahApps.Metro.Shared/Controls/WinApiHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,33 @@ public static System.Windows.Point GetRelativeMousePosition(IntPtr hWnd)
return new System.Windows.Point(point.x, point.y);
}

/// <summary>
/// Try to get the relative mouse position to the given handle in client coordinates.
/// </summary>
/// <param name="hWnd">The handle for this method.</param>
public static bool TryGetRelativeMousePosition(IntPtr hWnd, out System.Windows.Point point)
{
Standard.POINT pt = new Standard.POINT();
var returnValue = hWnd != IntPtr.Zero && Standard.NativeMethods.TryGetPhysicalCursorPos(out pt);
if (returnValue)
{
Standard.NativeMethods.ScreenToClient(hWnd, ref pt);
point = new System.Windows.Point(pt.x, pt.y);
}
else
{
point = new System.Windows.Point();
}
return returnValue;
}

internal static Standard.POINT GetPhysicalCursorPos()
{
try
{
// Sometimes Win32 will fail this call, such as if you are
// not running in the interactive desktop. For example,
// a secure screen saver may be running.
return Standard.NativeMethods.GetPhysicalCursorPos();
}
catch (Exception exception)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2997,11 +2997,13 @@ public static RECT GetClientRect(IntPtr hwnd)
return rc;
}

[SecurityCritical]
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
[DllImport("user32.dll", EntryPoint = "GetCursorPos", SetLastError = true)]
[DllImport("user32.dll", EntryPoint = "GetCursorPos", ExactSpelling = true, CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool _GetCursorPos(out POINT lpPoint);

[SecurityCritical]
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public static POINT GetCursorPos()
{
Expand All @@ -3010,15 +3012,33 @@ public static POINT GetCursorPos()
{
HRESULT.ThrowLastError();
}

return pt;
}

[SecurityCritical]
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public static bool TryGetCursorPos(out POINT pt)
{
var returnValue = _GetCursorPos(out pt);
// Sometimes Win32 will fail this call, such as if you are
// not running in the interactive desktop. For example,
// a secure screen saver may be running.
if (!returnValue)
{
System.Diagnostics.Debug.WriteLine("GetCursorPos failed!");
pt.x = 0;
pt.y = 0;
}
return returnValue;
}

[SecurityCritical]
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
[DllImport("user32.dll", EntryPoint = "GetPhysicalCursorPos", SetLastError = true)]
[DllImport("user32.dll", EntryPoint = "GetPhysicalCursorPos", ExactSpelling = true, CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool _GetPhysicalCursorPos(out POINT lpPoint);

[SecurityCritical]
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public static POINT GetPhysicalCursorPos()
{
Expand All @@ -3027,10 +3047,26 @@ public static POINT GetPhysicalCursorPos()
{
HRESULT.ThrowLastError();
}

return pt;
}

[SecurityCritical]
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
public static bool TryGetPhysicalCursorPos(out POINT pt)
{
var returnValue = _GetPhysicalCursorPos(out pt);
// Sometimes Win32 will fail this call, such as if you are
// not running in the interactive desktop. For example,
// a secure screen saver may be running.
if (!returnValue)
{
System.Diagnostics.Debug.WriteLine("GetPhysicalCursorPos failed!");
pt.x = 0;
pt.y = 0;
}
return returnValue;
}

[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
[DllImport("uxtheme.dll", EntryPoint = "GetCurrentThemeName", CharSet = CharSet.Unicode)]
private static extern HRESULT _GetCurrentThemeName(
Expand Down

0 comments on commit 41fd0a4

Please sign in to comment.