Skip to content

Commit a14c458

Browse files
committed
Fix display on monitor with mouse and move all NaticeMethods to NativeMethods.cs
1 parent 22dc870 commit a14c458

File tree

3 files changed

+25
-28
lines changed

3 files changed

+25
-28
lines changed

src/modules/ShortcutGuideV2/ShortcutGuide.Ui/DpiHelper.cs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
// The Microsoft Corporation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System;
6-
using System.Runtime.InteropServices;
7-
85
namespace ShortcutGuide
96
{
107
// This class is rewritten from C++ to C# from the measure tool project
@@ -25,7 +22,7 @@ public static float GetDPIScaleForWindow(int hwnd)
2522

2623
public static long GetScreenDPIForWindow(int hwnd, ref int dpi)
2724
{
28-
var targetMonitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
25+
var targetMonitor = NativeMethods.MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
2926
return GetScreenDPIForMonitor(targetMonitor.ToInt32(), ref dpi);
3027
}
3128

@@ -34,19 +31,13 @@ public static long GetScreenDPIForMonitor(int targetMonitor, ref int dpi)
3431
if (targetMonitor != 0)
3532
{
3633
int dummy = 0;
37-
return GetDpiForMonitor(targetMonitor, MDT_EFFECTIVE_DPI, ref dpi, ref dummy);
34+
return NativeMethods.GetDpiForMonitor(targetMonitor, MDT_EFFECTIVE_DPI, ref dpi, ref dummy);
3835
}
3936
else
4037
{
4138
dpi = DEFAULT_DPI;
4239
return 0x80004005L;
4340
}
4441
}
45-
46-
[LibraryImport("User32.dll")]
47-
private static partial IntPtr MonitorFromWindow(int hwnd, int dwFlags);
48-
49-
[LibraryImport("Shcore.dll")]
50-
private static partial long GetDpiForMonitor(int hmonitor, int dpiType, ref int dpiX, ref int dpiY);
5142
}
5243
}

src/modules/ShortcutGuideV2/ShortcutGuide.Ui/NativeMethods.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.Runtime.InteropServices;
7+
using Windows.Graphics;
78

89
internal static partial class NativeMethods
910
{
@@ -29,6 +30,27 @@ internal static partial class NativeMethods
2930
[LibraryImport("user32.dll", StringMarshalling = StringMarshalling.Utf16)]
3031
internal static partial int FindWindowExA(int hwndParent, int hwndChildAfter, in string lpClassName, in string? lpWindowName);
3132

33+
[LibraryImport("User32.dll")]
34+
internal static partial IntPtr MonitorFromWindow(int hwnd, int dwFlags);
35+
36+
[LibraryImport("Shcore.dll")]
37+
internal static partial long GetDpiForMonitor(int hmonitor, int dpiType, ref int dpiX, ref int dpiY);
38+
39+
[LibraryImport("user32.dll")]
40+
[return: MarshalAs(UnmanagedType.Bool)]
41+
public static partial bool GetCursorPos(out POINT lpPoint);
42+
43+
public struct POINT
44+
{
45+
public int X;
46+
public int Y;
47+
48+
public static implicit operator PointInt32(POINT point)
49+
{
50+
return new PointInt32(point.X, point.Y);
51+
}
52+
}
53+
3254
internal const int GWL_STYLE = -16;
3355
internal const int WS_CAPTION = 0x00C00000;
3456
}

src/modules/ShortcutGuideV2/ShortcutGuide.Ui/ShortcutGuideXAML/MainWindow.xaml.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Collections.Generic;
77
using System.Globalization;
88
using System.IO;
9-
using System.Runtime.InteropServices;
109
using System.Text.Json;
1110
using Microsoft.PowerToys.Settings.UI.Library;
1211
using Microsoft.UI;
@@ -78,7 +77,7 @@ private void OnLauched(object sender, WindowActivatedEventArgs e)
7877
_appWindow = AppWindow.GetFromWindowId(windowId);
7978

8079
GetCursorPos(out POINT lpPoint);
81-
_appWindow.Move(lpPoint);
80+
_appWindow.Move(lpPoint with { Y = lpPoint.Y - ((int)Height / 2), X = lpPoint.X - ((int)Width / 2) });
8281

8382
float dpiScale = DpiHelper.GetDPIScaleForWindow((int)hwnd);
8483

@@ -108,21 +107,6 @@ public void WindowSelectionChanged(object sender, SelectorBarSelectionChangedEve
108107

109108
private bool _setPosition;
110109

111-
[LibraryImport("user32.dll")]
112-
[return: MarshalAs(UnmanagedType.Bool)]
113-
public static partial bool GetCursorPos(out POINT lpPoint);
114-
115-
public struct POINT
116-
{
117-
public int X;
118-
public int Y;
119-
120-
public static implicit operator PointInt32(POINT point)
121-
{
122-
return new PointInt32(point.X, point.Y);
123-
}
124-
}
125-
126110
public void CloseButton_Clicked(object sender, RoutedEventArgs e)
127111
{
128112
Environment.Exit(0);

0 commit comments

Comments
 (0)