NativeMethods.cs
1 // Copyright (c) Microsoft Corporation 2 // The Microsoft Corporation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System; 6 using System.Runtime.InteropServices; 7 using System.Text; 8 9 namespace Microsoft.PowerToys.Settings.UI.Helpers 10 { 11 public static class NativeMethods 12 { 13 private const int WS_POPUP = 1 << 31; // 0x80000000 14 internal const int GWL_STYLE = -16; 15 internal const int WS_CAPTION = 0x00C00000; 16 internal const int SPI_GETDESKWALLPAPER = 0x0073; 17 internal const int SW_SHOWNORMAL = 1; 18 internal const int SW_SHOWMAXIMIZED = 3; 19 internal const int SW_HIDE = 0; 20 internal const int WM_COMMAND = 0x0111; // https://learn.microsoft.com/en-us/windows/win32/menurc/wm-command 21 22 [DllImport("user32.dll")] 23 internal static extern IntPtr GetActiveWindow(); 24 25 [DllImport("user32.dll")] 26 internal static extern bool SetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl); 27 28 [DllImport("user32.dll")] 29 internal static extern bool GetWindowPlacement(IntPtr hWnd, out WINDOWPLACEMENT lpwndpl); 30 31 [DllImport("user32.dll")] 32 internal static extern uint SendInput(uint nInputs, NativeKeyboardHelper.INPUT[] pInputs, int cbSize); 33 34 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)] 35 internal static extern short GetAsyncKeyState(int vKey); 36 37 [DllImport("user32.dll", SetLastError = true)] 38 internal static extern int GetWindowLong(IntPtr hWnd, int nIndex); 39 40 [DllImport("user32.dll")] 41 internal static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); 42 43 [DllImport("shell32.dll")] 44 internal static extern IntPtr SHBrowseForFolderW(ref ShellGetFolder.BrowseInformation browseInfo); 45 46 [DllImport("shell32.dll")] 47 internal static extern int SHGetPathFromIDListW(IntPtr pidl, IntPtr pszPath); 48 49 [DllImport("Comdlg32.dll", CharSet = CharSet.Unicode)] 50 internal static extern bool GetOpenFileName([In, Out] OpenFileName openFileName); 51 52 [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)] 53 internal static extern IntPtr FindWindow(string lpClassName, string lpWindowName); 54 55 [DllImport("user32.dll")] 56 internal static extern IntPtr SendMessage(IntPtr hWnd, IntPtr msg, UIntPtr wParam, UIntPtr lParam); 57 58 [DllImport("comdlg32.dll", CharSet = CharSet.Auto, EntryPoint = "ChooseFont", SetLastError = true)] 59 internal static extern bool ChooseFont(IntPtr lpChooseFont); 60 61 [DllImport("comdlg32.dll", SetLastError = true)] 62 internal static extern int CommDlgExtendedError(); 63 64 #pragma warning disable CA1401 // P/Invokes should not be visible 65 [DllImport("user32.dll")] 66 public static extern bool ShowWindow(System.IntPtr hWnd, int nCmdShow); 67 68 [DllImport("user32.dll")] 69 public static extern int GetDpiForWindow(System.IntPtr hWnd); 70 71 [DllImport("user32.dll")] 72 public static extern bool IsWindowVisible(IntPtr hWnd); 73 74 [DllImport("user32.dll")] 75 public static extern bool AllowSetForegroundWindow(int dwProcessId); 76 77 [System.Runtime.InteropServices.DllImport("User32.dll")] 78 public static extern bool SetForegroundWindow(IntPtr handle); 79 80 [DllImport("kernel32.dll", CharSet = CharSet.Unicode)] 81 public static extern IntPtr LoadLibrary(string dllToLoad); 82 83 [DllImport("kernel32.dll", CharSet = CharSet.Unicode)] 84 public static extern bool FreeLibrary(IntPtr hModule); 85 86 #pragma warning restore CA1401 // P/Invokes should not be visible 87 88 [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)] 89 [return: MarshalAs(UnmanagedType.Bool)] 90 91 internal static extern bool SystemParametersInfo(int uiAction, int uiParam, StringBuilder pvParam, int fWinIni); 92 93 public static void SetPopupStyle(IntPtr hwnd) 94 { 95 _ = SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) | WS_POPUP); 96 } 97 } 98 }