Skip to content

Commit

Permalink
P/Invoke&C++ -> C#/Win32
Browse files Browse the repository at this point in the history
  • Loading branch information
AndromedaMelody committed May 29, 2023
1 parent f193dd2 commit 90d74ab
Show file tree
Hide file tree
Showing 30 changed files with 411 additions and 626 deletions.
23 changes: 2 additions & 21 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>

<PropertyGroup Condition="'$(MSBuildProjectExtension)' == '.csproj'">
<PropertyGroup>
<TargetPlatformVersion>10.0.22621.0</TargetPlatformVersion>
<TargetPlatformMinVersion>7.0</TargetPlatformMinVersion>
<TargetFrameworks>netframework4.7.2;netcoreapp7.0-windows$(TargetPlatformVersion)</TargetFrameworks>
Expand All @@ -9,27 +9,8 @@
<Version>2.12.2.0</Version>
</PropertyGroup>

<ItemGroup Condition="'$(MSBuildProjectExtension)' == '.vcxproj'">
<ProjectCapability Include="PackageReferences" />
</ItemGroup>
<PropertyGroup Condition="'$(MSBuildProjectExtension)' == '.vcxproj'">
<AssetTargetFallback>$(AssetTargetFallback);native</AssetTargetFallback>
<TargetFrameworkMoniker>native,Version=v0.0</TargetFrameworkMoniker>
<NuGetTargetMoniker>native,Version=v0.0</NuGetTargetMoniker>
<RuntimeIdentifiers>win;win-x86;win-x64</RuntimeIdentifiers>
</PropertyGroup>

<PropertyGroup Condition="'$(MSBuildProjectExtension)' == '.csproj' and $(TargetFramework.StartsWith('netcoreapp'))">
<IncludeWindowsSDKRefFrameworkReferences>false</IncludeWindowsSDKRefFrameworkReferences>
</PropertyGroup>

<ItemGroup Condition="'$(MSBuildProjectExtension)' == '.csproj' and $(TargetFramework.StartsWith('netframework'))">
<ItemGroup Condition="$(TargetFramework.StartsWith('netframework'))">
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" />
</ItemGroup>

<ItemGroup Condition="'$(MSBuildProjectExtension)' == '.vcxproj'">
<PackageReference Include="VC-LTL" Version="5.0.5" />
<PackageReference Include="Microsoft.Windows.SDK.CPP.$(PlatformShortName)" Version="10.0.22621.*" />
</ItemGroup>

</Project>
30 changes: 30 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Condition="'$(MSBuildProjectExtension)' == '.csproj'">

<ItemGroup Condition="'$(UseWin32)' == 'true'">
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.2-beta" PrivateAssets="all" />
<PackageReference Include="Microsoft.Windows.SDK.Win32Metadata" Version="52.0.65-preview" PrivateAssets="all" />
<None Remove="NativeMethods.txt" Condition="Exists('NativeMethods.txt')" />
<AdditionalFiles Update="NativeMethods.txt" Link="Properties\NativeMethods.txt" Condition="Exists('NativeMethods.txt')" />
<AdditionalFiles Include="Properties\NativeMethods.txt" Condition="Exists('Properties\NativeMethods.txt')" />
<None Remove="NativeMethods.json" Condition="Exists('NativeMethods.json')" />
<AdditionalFiles Include="$(MSBuildThisFileDirectory)\NativeMethods.json" Link="Properties\NativeMethods.json" Condition="Exists('$(MSBuildThisFileDirectory)\NativeMethods.json')" />
</ItemGroup>

<Choose>
<When Condition="'$(TargetFrameworkIdentifier)' == '.NETCoreApp' and '$(_TargetFrameworkVersionWithoutV)' &gt;= '5.0'">
<PropertyGroup>
<IncludeWindowsSDKRefFrameworkReferences Condition="'$(UseWinRT)' != 'true'">false</IncludeWindowsSDKRefFrameworkReferences>
<IncludeWindowsSDKRefFrameworkReferences Condition="'$(UseWinRT)' == 'true'">true</IncludeWindowsSDKRefFrameworkReferences>
</PropertyGroup>
<ItemGroup Condition="'$(UseWinRT)' == 'true'">
<PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.0.2" PrivateAssets="all" />
</ItemGroup>
</When>
<Otherwise>
<ItemGroup Condition="'$(UseWinRT)' == 'true'">
<PackageReference Include="Microsoft.Windows.SDK.Contracts" Version="$([System.Version]::Parse('$(TargetPlatformVersion)').ToString(3)).*" PrivateAssets="all" />
</ItemGroup>
</Otherwise>
</Choose>

</Project>
66 changes: 21 additions & 45 deletions KeyboardMouseHookLibrary/FindWindowInfo.cs
Original file line number Diff line number Diff line change
@@ -1,62 +1,37 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Windows.Win32;
using Windows.Win32.Foundation;

namespace KeyboardMouseHookLibrary
{
public class FindWindowInfo
{
[DllImport("user32.dll", EntryPoint = "WindowFromPoint")]//指定坐标处窗体句柄
public static extern int WindowFromPoint(
int xPoint,
int yPoint
);

[DllImport("user32.dll", EntryPoint = "GetWindowText")]
public static extern int GetWindowText(
int hWnd,
StringBuilder lpString,
int nMaxCount
);

[DllImport("user32.dll", EntryPoint = "GetClassName")]
public static extern int GetClassName(
int hWnd,
StringBuilder lpString,
int nMaxCont
);

[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(
IntPtr hwnd,
out int ID
);

/// <summary>
/// 获取指定坐标处窗口的句柄
/// </summary>
/// <param name="mouseX"></param>
/// <param name="mouseY"></param>
/// <returns></returns>
public static int GetWindowHWND(int mouseX, int mouseY)
public static IntPtr GetWindowHWND(Point point)
{

int hwnd = WindowFromPoint(mouseX, mouseY);
return hwnd;
return PInvoke.WindowFromPoint(point);
}

/// <summary>
/// 根据HWND获得窗口标题
/// </summary>
/// <param name="hwnd"></param>
/// <returns></returns>
public static string GetWindowName(int hwnd)
public static unsafe string GetWindowName(IntPtr hwnd)
{
StringBuilder name = new StringBuilder(256);
GetWindowText(hwnd, name, 256);
Span<char> name = stackalloc char[PInvoke.GetWindowTextLength((HWND)hwnd) + 1];
fixed(char* pName = name)
{
PInvoke.GetWindowText((HWND)hwnd, pName, name.Length);
}
return name.ToString();
}

Expand All @@ -65,10 +40,13 @@ public static string GetWindowName(int hwnd)
/// </summary>
/// <param name="hwnd"></param>
/// <returns></returns>
public static string GetWindowClassName(int hwnd)
public static unsafe string GetWindowClassName(IntPtr hwnd)
{
StringBuilder name = new StringBuilder(256);
GetClassName(hwnd, name, 256);
Span<char> name = stackalloc char[256];
fixed(char* pName = name)
{
name = name.Slice(0, PInvoke.GetClassName((HWND)hwnd, pName, 256) + 1);
}
return name.ToString();
}

Expand All @@ -77,13 +55,11 @@ public static string GetWindowClassName(int hwnd)
/// </summary>
/// <param name="hwnd"></param>
/// <returns></returns>
public static int GetProcessIDByHWND(int hwnd)
public static unsafe uint GetProcessIDByHWND(IntPtr hWnd)
{
int oo;
IntPtr ip = new IntPtr(hwnd);
GetWindowThreadProcessId(ip, out oo);
return oo;
uint result;
PInvoke.GetWindowThreadProcessId((HWND)hWnd, &result);
return result;
}

}
}
Loading

0 comments on commit 90d74ab

Please sign in to comment.