本帖最后由 went 于 2022-11-25 10:48 编辑
回复 3# 小白龙
Rect是自定义的结构体
不想用结构体也可以直接传数组参数 | cls | | | | $code=@' | | using System.Runtime.InteropServices; | | public static class WinApi{ | | [DllImport("user32.dll")] | | public static extern bool SetWindowPos(uint hwnd,uint hpart,uint x,uint y,uint cx,uint cy,uint flags); | | [DllImport("user32.dll")] | | public static extern bool GetWindowRect(uint hwnd,uint[] arr); | | [DllImport("user32.dll")] | | public static extern bool ShowWindow(uint hwnd,uint show); | | } | | '@ | | Add-Type -TypeDefinition $code | | Add-Type -AssemblyName system.Windows.Forms | | Add-Type -AssemblyName system.Drawing | | | | | | $Form = New-Object windows.Forms.Form | | $Form.Size = New-Object drawing.Size(200, 120) | | | | $Form.Text = "Test of Form" | | $Form.MaximizeBox = $false | | $Form.MinimizeBox = $false | | $Form.Font = New-Object drawing.Font("Times New Roman", 10, [Drawing.FontStyle]::Bold) | | | | $Form.BackColor = "LightBlue" | | $Form.FormBorderStyle = "FixedSingle" | | | | $hwnd = (Get-Process notepad -ErrorAction SilentlyContinue| Select-Object -First 1).MainWindowHandle | | if($hwnd -eq $null){ | | $Form.StartPosition = 'CenterScreen' | | } else { | | $arr = New-Object 'int32[]'(4) | | [void][WinApi]::ShowWindow([int32]$hwnd,1) | | [void][WinApi]::GetWindowRect([int32]$hwnd,$arr) | | $x = $arr[0] + ($arr[2] - $arr[0] - $Form.Width)/2 | | $y = $arr[1] + ($arr[3] - $arr[1] - $Form.Height)/2 | | [void][WinApi]::SetWindowPos([int32]$Form.Handle,$null,$x,$y,0,0,1) | | } | | | | $Form.ShowDialog()COPY |
|