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> <OutputType>Exe</OutputType>
<Platforms>x64;ARM64</Platforms> <Platforms>x64;ARM64</Platforms>
<PublishAot>true</PublishAot> <PublishAot>true</PublishAot>
<DisableRuntimeMarshalling>true</DisableRuntimeMarshalling>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath> <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath> <AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<OutputPath>..\..\..\..\$(Platform)\$(Configuration)</OutputPath> <OutputPath>..\..\..\..\$(Platform)\$(Configuration)</OutputPath>
@@ -20,6 +21,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="System.Text.Json" /> <PackageReference Include="System.Text.Json" />
<PackageReference Include="Microsoft.Windows.CsWin32" PrivateAssets="all" />
</ItemGroup> </ItemGroup>
<!-- Force using WindowsDesktop runtime to ensure consistent dll versions with other projects --> <!-- 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. // The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
using System; using Windows.Win32;
using System.Runtime.InteropServices; using Windows.Win32.Foundation;
namespace FancyZonesCLI; namespace FancyZonesCLI;
@@ -12,8 +12,6 @@ namespace FancyZonesCLI;
/// </summary> /// </summary>
internal static class NativeMethods internal static class NativeMethods
{ {
public static readonly IntPtr HWND_BROADCAST = new IntPtr(0xffff);
// Registered Windows messages for notifying FancyZones // Registered Windows messages for notifying FancyZones
private static uint wmPrivAppliedLayoutsFileUpdate; private static uint wmPrivAppliedLayoutsFileUpdate;
private static uint wmPrivLayoutHotkeysFileUpdate; private static uint wmPrivLayoutHotkeysFileUpdate;
@@ -33,8 +31,8 @@ internal static class NativeMethods
/// </summary> /// </summary>
public static void InitializeWindowMessages() public static void InitializeWindowMessages()
{ {
wmPrivAppliedLayoutsFileUpdate = RegisterWindowMessage("{2ef2c8a7-e0d5-4f31-9ede-52aade2d284d}"); wmPrivAppliedLayoutsFileUpdate = PInvoke.RegisterWindowMessage("{2ef2c8a7-e0d5-4f31-9ede-52aade2d284d}");
wmPrivLayoutHotkeysFileUpdate = RegisterWindowMessage("{07229b7e-4f22-4357-b136-33c289be2295}"); wmPrivLayoutHotkeysFileUpdate = PInvoke.RegisterWindowMessage("{07229b7e-4f22-4357-b136-33c289be2295}");
} }
/// <summary> /// <summary>
@@ -43,16 +41,16 @@ internal static class NativeMethods
/// <param name="message">The Windows message ID to broadcast.</param> /// <param name="message">The Windows message ID to broadcast.</param>
public static void NotifyFancyZones(uint message) 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)] /// <summary>
public static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); /// Brings the specified window to the foreground.
/// </summary>
[DllImport("user32.dll")] /// <param name="hWnd">A handle to the window.</param>
[return: MarshalAs(UnmanagedType.Bool)] /// <returns>True if the window was brought to the foreground.</returns>
public static extern bool SetForegroundWindow(IntPtr hWnd); public static bool SetForegroundWindow(nint hWnd)
{
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)] return PInvoke.SetForegroundWindow(new HWND(hWnd));
public static extern uint RegisterWindowMessage(string lpString); }
} }

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