瀏覽代碼

Separate all windows function into a separate class

Equbuxu 2 年之前
父節點
當前提交
eb8d714656

+ 10 - 56
src/PixiEditor/Helpers/GlobalMouseHook.cs

@@ -15,15 +15,9 @@ public delegate void MouseUpEventHandler(object sender, Point p, MouseButton but
 [ExcludeFromCodeCoverage]
 internal static class GlobalMouseHook
 {
-    private const int WH_MOUSE_LL = 14;
-    private const int WM_LBUTTONUP = 0x0202;
-    private const int WM_MBUTTONUP = 0x0208;
-    private const int WM_RBUTTONUP = 0x0205;
-
     private static int mouseHookHandle;
-    private static HookProc mouseDelegate;
+    private static Win32.HookProc mouseDelegate;
 
-    private delegate int HookProc(int nCode, int wParam, IntPtr lParam);
 
     public static event MouseUpEventHandler OnMouseUp
     {
@@ -56,7 +50,7 @@ internal static class GlobalMouseHook
     {
         if (mouseHookHandle != 0)
         {
-            int result = UnhookWindowsHookEx(mouseHookHandle);
+            int result = Win32.UnhookWindowsHookEx(mouseHookHandle);
             mouseHookHandle = 0;
             mouseDelegate = null;
             if (result == 0)
@@ -72,10 +66,10 @@ internal static class GlobalMouseHook
         if (mouseHookHandle == 0)
         {
             mouseDelegate = MouseHookProc;
-            mouseHookHandle = SetWindowsHookEx(
-                WH_MOUSE_LL,
+            mouseHookHandle = Win32.SetWindowsHookEx(
+                Win32.WH_MOUSE_LL,
                 mouseDelegate,
-                GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName),
+                Win32.GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName),
                 0);
             if (mouseHookHandle == 0)
             {
@@ -89,60 +83,20 @@ internal static class GlobalMouseHook
     {
         if (nCode >= 0)
         {
-            MSLLHOOKSTRUCT mouseHookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
-            if (wParam == WM_LBUTTONUP || wParam == WM_MBUTTONUP || wParam == WM_RBUTTONUP)
+            Win32.MSLLHOOKSTRUCT mouseHookStruct = (Win32.MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(Win32.MSLLHOOKSTRUCT));
+            if (wParam == Win32.WM_LBUTTONUP || wParam == Win32.WM_MBUTTONUP || wParam == Win32.WM_RBUTTONUP)
             {
                 if (MouseUp != null)
                 {
 
-                    MouseButton button = wParam == WM_LBUTTONUP ? MouseButton.Left
-                        : wParam == WM_MBUTTONUP ? MouseButton.Middle : MouseButton.Right;
+                    MouseButton button = wParam == Win32.WM_LBUTTONUP ? MouseButton.Left
+                        : wParam == Win32.WM_MBUTTONUP ? MouseButton.Middle : MouseButton.Right;
                     Dispatcher.CurrentDispatcher.BeginInvoke(() =>
                         MouseUp.Invoke(null, new Point(mouseHookStruct.Pt.X, mouseHookStruct.Pt.Y), button));
                 }
             }
         }
 
-        return CallNextHookEx(mouseHookHandle, nCode, wParam, lParam);
-    }
-
-    [DllImport(
-        "user32.dll",
-        CharSet = CharSet.Auto,
-        CallingConvention = CallingConvention.StdCall,
-        SetLastError = true)]
-    private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, int dwThreadId);
-
-    [DllImport(
-        "user32.dll",
-        CharSet = CharSet.Auto,
-        CallingConvention = CallingConvention.StdCall,
-        SetLastError = true)]
-    private static extern int UnhookWindowsHookEx(int idHook);
-
-    [DllImport(
-        "user32.dll",
-        CharSet = CharSet.Auto,
-        CallingConvention = CallingConvention.StdCall)]
-    private static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);
-
-    [DllImport("kernel32.dll")]
-    private static extern IntPtr GetModuleHandle(string name);
-
-    [StructLayout(LayoutKind.Sequential)]
-    private struct POINT
-    {
-        public int X;
-        public int Y;
-    }
-
-    [StructLayout(LayoutKind.Sequential)]
-    private struct MSLLHOOKSTRUCT
-    {
-        public POINT Pt;
-        public uint MouseData;
-        public uint Flags;
-        public uint Time;
-        public IntPtr DwExtraInfo;
+        return Win32.CallNextHookEx(mouseHookHandle, nCode, wParam, lParam);
     }
 }

+ 2 - 41
src/PixiEditor/Helpers/InputKeyHelpers.cs

@@ -28,10 +28,10 @@ internal static class InputKeyHelpers
         int virtualKey = KeyInterop.VirtualKeyFromKey(key);
         byte[] keyboardState = new byte[256];
 
-        uint scanCode = MapVirtualKeyExW((uint)virtualKey, MapType.MAPVK_VK_TO_VSC, culture.KeyboardLayoutId);
+        uint scanCode = Win32.MapVirtualKeyExW((uint)virtualKey, Win32.MapType.MAPVK_VK_TO_VSC, culture.KeyboardLayoutId);
         StringBuilder stringBuilder = new(3);
 
-        int result = ToUnicode((uint)virtualKey, scanCode, keyboardState, stringBuilder, stringBuilder.Capacity, 0);
+        int result = Win32.ToUnicode((uint)virtualKey, scanCode, keyboardState, stringBuilder, stringBuilder.Capacity, 0);
 
         string stringResult;
 
@@ -44,43 +44,4 @@ internal static class InputKeyHelpers
 
         return stringResult;
     }
-
-    private enum MapType : uint
-    {
-        /// <summary>
-        /// The uCode parameter is a virtual-key code and is translated into a scan code. If it is a virtual-key code that does not distinguish between left- and right-hand keys, the left-hand scan code is returned. If there is no translation, the function returns 0.
-        /// </summary>
-        MAPVK_VK_TO_VSC = 0x0,
-
-        /// <summary>
-        /// The uCode parameter is a scan code and is translated into a virtual-key code that does not distinguish between left- and right-hand keys. If there is no translation, the function returns 0.
-        /// </summary>
-        MAPVK_VSC_TO_VK = 0x1,
-
-        /// <summary>
-        /// The uCode parameter is a virtual-key code and is translated into an unshifted character value in the low order word of the return value. Dead keys (diacritics) are indicated by setting the top bit of the return value. If there is no translation, the function returns 0.
-        /// </summary>
-        MAPVK_VK_TO_CHAR = 0x2,
-
-        /// <summary>
-        /// The uCode parameter is a scan code and is translated into a virtual-key code that distinguishes between left- and right-hand keys. If there is no translation, the function returns 0.
-        /// </summary>
-        MAPVK_VSC_TO_VK_EX = 0x3,
-    }
-
-    [DllImport("user32.dll")]
-    private static extern int ToUnicode(
-        uint wVirtKey,
-        uint wScanCode,
-        byte[] lpKeyState,
-        [Out, MarshalAs(UnmanagedType.LPWStr, SizeParamIndex = 4)]
-        StringBuilder pwszBuff,
-        int cchBuff,
-        uint wFlags);
-
-    [DllImport("user32.dll")]
-    private static extern bool GetKeyboardState(byte[] lpKeyState);
-
-    [DllImport("user32.dll")]
-    private static extern uint MapVirtualKeyExW(uint uCode, MapType uMapType, int hkl);
 }

+ 147 - 0
src/PixiEditor/Helpers/Win32.cs

@@ -0,0 +1,147 @@
+using System.Runtime.InteropServices;
+using System.Text;
+
+namespace PixiEditor.Helpers;
+internal class Win32
+{
+    public const int WM_GETMINMAXINFO = 0x0024;
+    public const uint MONITOR_DEFAULTTONEAREST = 0x00000002;
+    public const int WH_MOUSE_LL = 14;
+    public const int WM_LBUTTONUP = 0x0202;
+    public const int WM_MBUTTONUP = 0x0208;
+    public const int WM_RBUTTONUP = 0x0205;
+
+    public delegate int HookProc(int nCode, int wParam, IntPtr lParam);
+
+
+    [StructLayout(LayoutKind.Sequential)]
+    public struct MSLLHOOKSTRUCT
+    {
+        public POINT Pt;
+        public uint MouseData;
+        public uint Flags;
+        public uint Time;
+        public IntPtr DwExtraInfo;
+    }
+
+    public enum MapType : uint
+    {
+        /// <summary>
+        /// The uCode parameter is a virtual-key code and is translated into a scan code. If it is a virtual-key code that does not distinguish between left- and right-hand keys, the left-hand scan code is returned. If there is no translation, the function returns 0.
+        /// </summary>
+        MAPVK_VK_TO_VSC = 0x0,
+
+        /// <summary>
+        /// The uCode parameter is a scan code and is translated into a virtual-key code that does not distinguish between left- and right-hand keys. If there is no translation, the function returns 0.
+        /// </summary>
+        MAPVK_VSC_TO_VK = 0x1,
+
+        /// <summary>
+        /// The uCode parameter is a virtual-key code and is translated into an unshifted character value in the low order word of the return value. Dead keys (diacritics) are indicated by setting the top bit of the return value. If there is no translation, the function returns 0.
+        /// </summary>
+        MAPVK_VK_TO_CHAR = 0x2,
+
+        /// <summary>
+        /// The uCode parameter is a scan code and is translated into a virtual-key code that distinguishes between left- and right-hand keys. If there is no translation, the function returns 0.
+        /// </summary>
+        MAPVK_VSC_TO_VK_EX = 0x3,
+    }
+
+    [Serializable]
+    [StructLayout(LayoutKind.Sequential)]
+    public struct RECT
+    {
+        public int Left;
+        public int Top;
+        public int Right;
+        public int Bottom;
+
+        public RECT(int left, int top, int right, int bottom)
+        {
+            this.Left = left;
+            this.Top = top;
+            this.Right = right;
+            this.Bottom = bottom;
+        }
+    }
+
+    [StructLayout(LayoutKind.Sequential)]
+    public struct MONITORINFO
+    {
+        public int cbSize;
+        public RECT rcMonitor;
+        public RECT rcWork;
+        public uint dwFlags;
+    }
+
+    [Serializable]
+    [StructLayout(LayoutKind.Sequential)]
+    public struct POINT
+    {
+        public int X;
+        public int Y;
+
+        public POINT(int x, int y)
+        {
+            this.X = x;
+            this.Y = y;
+        }
+    }
+
+    [StructLayout(LayoutKind.Sequential)]
+    public struct MINMAXINFO
+    {
+        public POINT ptReserved;
+        public POINT ptMaxSize;
+        public POINT ptMaxPosition;
+        public POINT ptMinTrackSize;
+        public POINT ptMaxTrackSize;
+    }
+
+
+    [DllImport("user32.dll")]
+    public static extern IntPtr MonitorFromWindow(IntPtr handle, uint flags);
+
+    [DllImport("user32.dll")]
+    public static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFO lpmi);
+
+    [DllImport("user32.dll")]
+    public static extern int ToUnicode(
+        uint wVirtKey,
+        uint wScanCode,
+        byte[] lpKeyState,
+        [Out, MarshalAs(UnmanagedType.LPWStr, SizeParamIndex = 4)]
+        StringBuilder pwszBuff,
+        int cchBuff,
+        uint wFlags);
+
+    [DllImport("user32.dll")]
+    public static extern bool GetKeyboardState(byte[] lpKeyState);
+
+    [DllImport("user32.dll")]
+    public static extern uint MapVirtualKeyExW(uint uCode, MapType uMapType, int hkl);
+
+    [DllImport(
+        "user32.dll",
+        CharSet = CharSet.Auto,
+        CallingConvention = CallingConvention.StdCall,
+        SetLastError = true)]
+    public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, int dwThreadId);
+
+    [DllImport(
+        "user32.dll",
+        CharSet = CharSet.Auto,
+        CallingConvention = CallingConvention.StdCall,
+        SetLastError = true)]
+    public static extern int UnhookWindowsHookEx(int idHook);
+
+    [DllImport(
+        "user32.dll",
+        CharSet = CharSet.Auto,
+        CallingConvention = CallingConvention.StdCall)]
+    public static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);
+
+    [DllImport("kernel32.dll")]
+    public static extern IntPtr GetModuleHandle(string name);
+}
+

+ 9 - 70
src/PixiEditor/Helpers/WindowSizeHelper.cs

@@ -9,22 +9,22 @@ static class WindowSizeHelper
     {
         // All windows messages (msg) can be found here
         // https://docs.microsoft.com/de-de/windows/win32/winmsg/window-notifications
-        if (msg == WM_GETMINMAXINFO)
+        if (msg == Win32.WM_GETMINMAXINFO)
         {
             // We need to tell the system what our size should be when maximized. Otherwise it will
             // cover the whole screen, including the task bar.
-            MINMAXINFO mmi = (MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(MINMAXINFO));
+            Win32.MINMAXINFO mmi = (Win32.MINMAXINFO)Marshal.PtrToStructure(lParam, typeof(Win32.MINMAXINFO));
 
             // Adjust the maximized size and position to fit the work area of the correct monitor
-            IntPtr monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
+            IntPtr monitor = Win32.MonitorFromWindow(hwnd, Win32.MONITOR_DEFAULTTONEAREST);
 
             if (monitor != IntPtr.Zero)
             {
-                MONITORINFO monitorInfo = default;
-                monitorInfo.cbSize = Marshal.SizeOf(typeof(MONITORINFO));
-                GetMonitorInfo(monitor, ref monitorInfo);
-                RECT rcWorkArea = monitorInfo.rcWork;
-                RECT rcMonitorArea = monitorInfo.rcMonitor;
+                Win32.MONITORINFO monitorInfo = default;
+                monitorInfo.cbSize = Marshal.SizeOf(typeof(Win32.MONITORINFO));
+                Win32.GetMonitorInfo(monitor, ref monitorInfo);
+                Win32.RECT rcWorkArea = monitorInfo.rcWork;
+                Win32.RECT rcMonitorArea = monitorInfo.rcMonitor;
                 mmi.ptMaxPosition.X = Math.Abs(rcWorkArea.Left - rcMonitorArea.Left);
                 mmi.ptMaxPosition.Y = Math.Abs(rcWorkArea.Top - rcMonitorArea.Top);
                 mmi.ptMaxSize.X = Math.Abs(rcWorkArea.Right - rcWorkArea.Left);
@@ -36,65 +36,4 @@ static class WindowSizeHelper
 
         return IntPtr.Zero;
     }
-
-    private const int WM_GETMINMAXINFO = 0x0024;
-
-    private const uint MONITOR_DEFAULTTONEAREST = 0x00000002;
-
-    [DllImport("user32.dll")]
-    private static extern IntPtr MonitorFromWindow(IntPtr handle, uint flags);
-
-    [DllImport("user32.dll")]
-    private static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFO lpmi);
-
-    [Serializable]
-    [StructLayout(LayoutKind.Sequential)]
-    private struct RECT
-    {
-        public int Left;
-        public int Top;
-        public int Right;
-        public int Bottom;
-
-        public RECT(int left, int top, int right, int bottom)
-        {
-            this.Left = left;
-            this.Top = top;
-            this.Right = right;
-            this.Bottom = bottom;
-        }
-    }
-
-    [StructLayout(LayoutKind.Sequential)]
-    private struct MONITORINFO
-    {
-        public int cbSize;
-        public RECT rcMonitor;
-        public RECT rcWork;
-        public uint dwFlags;
-    }
-
-    [Serializable]
-    [StructLayout(LayoutKind.Sequential)]
-    private struct POINT
-    {
-        public int X;
-        public int Y;
-
-        public POINT(int x, int y)
-        {
-            this.X = x;
-            this.Y = y;
-        }
-    }
-
-    [StructLayout(LayoutKind.Sequential)]
-    private struct MINMAXINFO
-    {
-        public POINT ptReserved;
-        public POINT ptMaxSize;
-        public POINT ptMaxPosition;
-        public POINT ptMinTrackSize;
-        public POINT ptMaxTrackSize;
-    }
-}
+}