我想从 Powershell 执行 WinAPI 命令,例如按下 WPF 表单上的按钮,开始按钮隐藏并显示在 powershell 5 中创建此代码
Add-Type -AssemblyName PresentationFramework
[xml]$xaml = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WinAPI"
Height="367" Width="264" Title="Hide Start button" WindowStartupLocation="CenterScreen" ResizeMode="NoResize">
<Grid>
<Button x:Name = "StartButton" Content="Start button" Margin="10,60,11,0" Height="25" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" Grid.ColumnSpan="2">
</Button>
</Grid>
</Window>
"@
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
$window = [Windows.Markup.XamlReader]::Load($reader)
$button = $Window.FindName("StartButton")
#--------------------------------------------------------------------------------
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class Win32 {
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string ClassName, string WindowName);
[DllImport("user32.dll")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string className, string windowName);
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
}
"@
$TaskbarHWnd = New-Object System.IntPtr
$StartButtonHWnd = New-Object System.IntPtr
[bool]$show = $true
$button.Add_Click({
$show = !($show)
$TaskbarHWnd = [Win32]::FindWindow("Shell_TrayWnd", $null); # get taskbar handle
write-host $TaskbarHWnd
$StartButtonHWnd = [Win32]::FindWindowEx($TaskbarHWnd, [IntPtr]::Zero, "Start", $null); # get start button handle - doesn't work
write-host $StartButtonHWnd
# hide Start button
$result = if ($show) {0} Else {5}
#if ($show) {$result = 0} Else {$result = 5}
write-host $result
[Win32]::ShowWindow($StartButtonHWnd, $result);
})
$window.ShowDialog() | Out-Null
下面的命令不起作用。
$StartButtonHWnd = [Win32]::FindWindowEx($TaskbarHWnd, [IntPtr]::Zero, "Start", $null);
$StartButtonHWnd 值 = 0。为什么 FindWindowEx 不返回句柄?第二个问题。该$show = !($show)
命令也不起作用我做错了什么?谢谢