/ src / modules / fancyzones / FancyZonesCLI / NativeMethods.cs
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 Windows.Win32;
 6  using Windows.Win32.Foundation;
 7  
 8  namespace FancyZonesCLI;
 9  
10  /// <summary>
11  /// Native Windows API methods for FancyZones CLI.
12  /// </summary>
13  internal static class NativeMethods
14  {
15      // Registered Windows messages for notifying FancyZones
16      private static uint wmPrivAppliedLayoutsFileUpdate;
17      private static uint wmPrivLayoutHotkeysFileUpdate;
18      private static uint wmPrivSaveEditorParameters;
19  
20      /// <summary>
21      /// Gets the Windows message ID for applied layouts file update notification.
22      /// </summary>
23      public static uint WM_PRIV_APPLIED_LAYOUTS_FILE_UPDATE => wmPrivAppliedLayoutsFileUpdate;
24  
25      /// <summary>
26      /// Gets the Windows message ID for layout hotkeys file update notification.
27      /// </summary>
28      public static uint WM_PRIV_LAYOUT_HOTKEYS_FILE_UPDATE => wmPrivLayoutHotkeysFileUpdate;
29  
30      /// <summary>
31      /// Gets the Windows message ID used to request saving editor-parameters.json.
32      /// </summary>
33      public static uint WM_PRIV_SAVE_EDITOR_PARAMETERS => wmPrivSaveEditorParameters;
34  
35      /// <summary>
36      /// Initializes the Windows messages used for FancyZones notifications.
37      /// </summary>
38      public static void InitializeWindowMessages()
39      {
40          wmPrivAppliedLayoutsFileUpdate = PInvoke.RegisterWindowMessage("{2ef2c8a7-e0d5-4f31-9ede-52aade2d284d}");
41          wmPrivLayoutHotkeysFileUpdate = PInvoke.RegisterWindowMessage("{07229b7e-4f22-4357-b136-33c289be2295}");
42          wmPrivSaveEditorParameters = PInvoke.RegisterWindowMessage("{d8f9c0e3-5d77-4e83-8a4f-7c704c2bfb4a}");
43      }
44  
45      /// <summary>
46      /// Broadcasts a notification message to FancyZones.
47      /// </summary>
48      /// <param name="message">The Windows message ID to broadcast.</param>
49      public static void NotifyFancyZones(uint message)
50      {
51          PInvoke.PostMessage(HWND.HWND_BROADCAST, message, 0, 0);
52      }
53  
54      /// <summary>
55      /// Brings the specified window to the foreground.
56      /// </summary>
57      /// <param name="hWnd">A handle to the window.</param>
58      /// <returns>True if the window was brought to the foreground.</returns>
59      public static bool SetForegroundWindow(nint hWnd)
60      {
61          return PInvoke.SetForegroundWindow(new HWND(hWnd));
62      }
63  }