use cswin32 native

This commit is contained in:
Leilei Zhang
2025-12-08 18:26:46 +08:00
parent 35242575f5
commit 1e21254c6a
4 changed files with 25 additions and 16 deletions

View File

@@ -11,6 +11,7 @@
<OutputType>Exe</OutputType>
<Platforms>x64;ARM64</Platforms>
<PublishAot>true</PublishAot>
<DisableRuntimeMarshalling>true</DisableRuntimeMarshalling>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<OutputPath>..\..\..\..\$(Platform)\$(Configuration)</OutputPath>
@@ -20,6 +21,7 @@
<ItemGroup>
<PackageReference Include="System.Text.Json" />
<PackageReference Include="Microsoft.Windows.CsWin32" PrivateAssets="all" />
</ItemGroup>
<!-- Force using WindowsDesktop runtime to ensure consistent dll versions with other projects -->

View File

@@ -2,8 +2,8 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Runtime.InteropServices;
using Windows.Win32;
using Windows.Win32.Foundation;
namespace FancyZonesCLI;
@@ -12,8 +12,6 @@ namespace FancyZonesCLI;
/// </summary>
internal static class NativeMethods
{
public static readonly IntPtr HWND_BROADCAST = new IntPtr(0xffff);
// Registered Windows messages for notifying FancyZones
private static uint wmPrivAppliedLayoutsFileUpdate;
private static uint wmPrivLayoutHotkeysFileUpdate;
@@ -33,8 +31,8 @@ internal static class NativeMethods
/// </summary>
public static void InitializeWindowMessages()
{
wmPrivAppliedLayoutsFileUpdate = RegisterWindowMessage("{2ef2c8a7-e0d5-4f31-9ede-52aade2d284d}");
wmPrivLayoutHotkeysFileUpdate = RegisterWindowMessage("{07229b7e-4f22-4357-b136-33c289be2295}");
wmPrivAppliedLayoutsFileUpdate = PInvoke.RegisterWindowMessage("{2ef2c8a7-e0d5-4f31-9ede-52aade2d284d}");
wmPrivLayoutHotkeysFileUpdate = PInvoke.RegisterWindowMessage("{07229b7e-4f22-4357-b136-33c289be2295}");
}
/// <summary>
@@ -43,16 +41,16 @@ internal static class NativeMethods
/// <param name="message">The Windows message ID to broadcast.</param>
public static void NotifyFancyZones(uint message)
{
PostMessage(HWND_BROADCAST, message, IntPtr.Zero, IntPtr.Zero);
PInvoke.PostMessage(HWND.HWND_BROADCAST, message, 0, 0);
}
[DllImport("user32.dll", SetLastError = true)]
public static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern uint RegisterWindowMessage(string lpString);
/// <summary>
/// Brings the specified window to the foreground.
/// </summary>
/// <param name="hWnd">A handle to the window.</param>
/// <returns>True if the window was brought to the foreground.</returns>
public static bool SetForegroundWindow(nint hWnd)
{
return PInvoke.SetForegroundWindow(new HWND(hWnd));
}
}

View File

@@ -0,0 +1,5 @@
{
"$schema": "https://aka.ms/CsWin32.schema.json",
"emitSingleFile": true,
"allowMarshaling": false
}

View File

@@ -0,0 +1,4 @@
PostMessage
SetForegroundWindow
RegisterWindowMessage
HWND_BROADCAST