本帖最后由 5i365 于 2022-2-18 12:12 编辑
回复 15# went
使用了@idwma 大侠这个贴子的代码, 取得屏幕宽度, 现在代码少了些
http://www.bathome.net/redirect. ... 1487&pid=251249- #Windows API
- $code=@'
- using System.Runtime.InteropServices;
- public struct RECT{
- public uint left;
- public uint top;
- public uint right;
- public uint bottom;
- }
- public static class WinApi{
- [DllImport("user32.dll")]
- public static extern bool SetWindowPos(uint hWnd,uint hAfter,uint x,uint y,uint cx,uint cy,uint flags);
- [DllImport("user32.dll")]
- public static extern bool GetWindowRect(uint hwnd, ref RECT rect);
- }
- '@
- Add-Type -TypeDefinition $code
-
- #获取记事本窗口句柄
- $hwnd = (Get-Process 'notepad')[0].MainWindowHandle
- #获取窗口信息
- $rect = New-Object 'RECT'
- [void][WinApi]::GetWindowRect([int]$hwnd,[ref]$rect)
- $screen_w = [Windows.Forms.Screen]::PrimaryScreen.WorkingArea.Width
- #计算水平居中坐标
- $x = ($screen_w - ($rect.right - $rect.left))/2
- #设置记事本水平居中
- [WinApi]::SetWindowPos([int]$hwnd,$null,$x,$rect.top,0,0,1)
复制代码
|