Skip to content

Instantly share code, notes, and snippets.

@corberan
Last active April 25, 2022 06:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corberan/0282965d2e982002ab1c742517085b11 to your computer and use it in GitHub Desktop.
Save corberan/0282965d2e982002ab1c742517085b11 to your computer and use it in GitHub Desktop.
powershell 和 winapi 的交互,获取窗口大小及边框大小
using namespace System;
using namespace System.Diagnostics;
using namespace System.Runtime.InteropServices;
param (
[Parameter(Mandatory = $True)]
[string]$hWndHexStr
)
Add-Type -TypeDefinition @"
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Explicit, Size = 16)]
public struct RECT
{
[FieldOffset(0)]
public int left;
[FieldOffset(4)]
public int top;
[FieldOffset(8)]
public int right;
[FieldOffset(12)]
public int bottom;
}
[StructLayout(LayoutKind.Sequential)]
public struct WINDOWINFO
{
public uint cbSize;
public RECT rcWindow;
public RECT rcClient;
public uint dwStyle;
public uint dwExStyle;
public uint dwWindowStatus;
public uint cxWindowBorders;
public uint cyWindowBorders;
public ushort atomWindowType;
public ushort wCreatorVersion;
}
public static class Kernel32
{
[DllImport("kernel32.dll", EntryPoint="GetLastError")]
public static extern uint GetLastError();
}
public static class User32
{
[DllImport("user32.dll", EntryPoint="GetWindowInfo", SetLastError=true)]
public static extern bool GetWindowInfo(
IntPtr hWnd,
ref WINDOWINFO pwi);
}
public static class Dwm
{
[DllImport("dwmapi.dll", EntryPoint="DwmGetWindowAttribute")]
public static extern int DwmGetWindowAttribute(
IntPtr hWnd,
uint dwAttribute,
out RECT pvAttribute, // 参见 http://pinvoke.net/default.aspx/dwmapi.DwmGetWindowAttribute,是分了 RECT 和 bool 两种参数的,而不是 IntPtr
uint cbAttribute);
}
"@
function Get-Pretty-Print {
param (
[RECT]$rect
)
return "($($rect.left), $($rect.top)) - ($($rect.right), $($rect.bottom)) $($rect.right-$rect.left)x$($rect.bottom-$rect.top)"
}
$hWnd = [Convert]::ToInt64($hWndHexStr, 16)
$WindowInfo = New-Object WINDOWINFO
$WindowInfo.cbSize = [Marshal]::SizeOf([System.Type] [WINDOWINFO])
$ret = [User32]::GetWindowInfo($hWnd, [ref] $WindowInfo)
if (!$ret) {
Write-Output "$((New-Object System.ComponentModel.Win32Exception([int][Kernel32]::GetLastError())).Message)"
}
else {
$rcWindow = Get-Pretty-Print -rect $WindowInfo.rcWindow
$rcClient = Get-Pretty-Print -rect $WindowInfo.rcClient
Write-Output "window: $($rcWindow)"
Write-Output "client: $($rcClient)"
Write-Output "border: [width: $($WindowInfo.cxWindowBorders), height: $($WindowInfo.cyWindowBorders)]"
}
New-Variable -Name DWMWA_EXTENDED_FRAME_BOUNDS -Option Constant -Value 0x09
New-Variable -Name S_OK -Option Constant -Value 0
$dwmRect = New-Object RECT
$dwmRectSize = [Marshal]::SizeOf([System.Type] [RECT])
$ret = [Dwm]::DwmGetWindowAttribute($hWnd, $DWMWA_EXTENDED_FRAME_BOUNDS, [ref] $dwmRect, $dwmRectSize)
if ($ret -eq $S_OK) {
$rcDwmWindow = Get-Pretty-Print -rect $dwmRect
Write-Output "dwm window: $($rcDwmWindow)"
}
else {
Write-Output "Error code: $($ret.ToString('X'))`r"
Write-Output "Check reference https://docs.microsoft.com/en-us/windows/desktop/seccrypto/common-hresult-values"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment