【发布时间】:2021-02-02 01:58:07
【问题描述】:
我一直在努力让 PowerShell 通过 Pinvoke 控制 SetWindowCompositionAttribute(通过 PowerShell 向 Windows 任务栏和其他窗口添加 Windows 10 Acrylic Blur 效果)。
我很乐意发送简单的参数,例如 HWND 和相应的值(如下面的两个示例所示)。但是,我不确定如何增强下面的 PowerShell 代码,以便它也可以处理打包在结构中的发送参数。请参阅 SetWindowCompositionAttribute。
我在 Delphi 和 AutoIt(而不是 PowerShell)中看到了执行此操作的代码示例。不幸的是,我无法弄清楚它们。
无论如何,下面是我的工作 PowerShell 代码和使用示例,它们演示了与 Windows API 的基本交互。我希望有人可以帮助我增强此代码(通过几个示例)来控制SetWindowCompositionAttribute 提供的各种功能。
最终,我希望能够指定 hWnd、Windows 类名和/或我希望添加模糊的组件的窗口标题名称;如果可能,请指定模糊/透明度的量。
工作功能和示例用法:
$script:nativeMethods = @();
function Register-NativeMethod([string]$dll, [string]$methodSignature) {
$script:nativeMethods += [PSCustomObject]@{ Dll = $dll; Signature = $methodSignature; }
}
function Add-NativeMethods() {
$nativeMethodsCode = $script:nativeMethods | % { "
[DllImport(`"$($_.Dll)`")]
public static extern $($_.Signature);
" }
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class NativeMethods {
$nativeMethodsCode
}
"@
}
#Build class and registers them:
Add-NativeMethods
#Example 1:
Register-NativeMethod "user32.dll" "bool SetForegroundWindow(IntPtr hWnd)"
[NativeMethods]::SetForegroundWindow((Get-Process -name notepad).MainWindowHandle)
#Example 2:
Register-NativeMethod "user32.dll" "bool ShowWindow(IntPtr hWnd, int nCmdShow)"
[NativeMethods]::ShowWindow((Get-Process -name notepad).MainWindowHandle, 0)
【问题讨论】:
-
如果提供了正确的结构定义,p/invokeit 肯定是可行的。但是,您是否考虑过使用DwmSetWindowAttribute?似乎在做同样的事情,但采用“简单”输入值
-
谢谢。知道我在哪里看到了如何使用它为窗口启用 Win10 Acrylic Blur 效果的示例吗?最终,我想这样做.. 使用 Powershell。
-
不是这个问题的答案,但是如果你知道如何在 C# 中做某事,那么你也可以在 PowerShell 中轻松完成,看看@987654326 @查看如何在powershell中运行C#代码。
标签: c# powershell pinvoke