SetWindowPos 不适用于 VSCode 窗口

问题描述 投票:0回答:1

我尝试使用

SetWindowPos
移动 VSCode 窗口(在 PowerShell 中,但这同样适用于 C++ 或 C# PInvoke),但我无法让它工作。

更令人沮丧的是,NirSoft 的 GUIPropView 实用程序确实设法以某种方式移动(使用 Center Selected Windows 选项)VSCode 窗口。 (当然该程序没有可用的源代码......)

我可以使用以下 PowerShell 代码获取某种句柄:

$hwnd = (Get-Process code).MainWindowHandle | where { $_ -ne 0 }

这会产生使用

FindWindow
时的句柄(提供 VSCode 实例的窗口类和标题)。

SetWindowPos
使用此句柄除了使
SetWindowPos
返回
True
之外绝对没有任何作用(我想这意味着它至少没有完全失败?)。

$user32 = Add-Type -MemberDefinition @' [DllImport("user32.dll", SetLastError = true)] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, UInt32 uFlags); '@ -Name user32 -PassThru # just some test coordinates and sizes $user32::SetWindowPos($hwnd, 0, 100, 100, 500, 500, 0)
GUIPropView 还给了我另外两个句柄(显示在程序的底部面板中),我不知道它们来自哪里,其中之一允许我调整渲染内容的大小和位置,但不能调整整个窗口本身,产生这样的结果:

我是否遗漏了一些明显的东西?我应该以不同的方式获取窗口句柄吗?这是怎么回事?

visual-studio-code winapi electron user32 setwindowpos
1个回答
0
投票
事实证明,我用于查找 VSCode 窗口句柄的代码实际上没问题。问题是在窗口仍处于最大化状态时尝试移动/调整窗口大小。 (请注意,我测试过的其他应用程序,例如 Firefox,没有任何问题。)

要取消最大化/取消最小化/取消排列窗口,我们可以使用

ShowWindow

SWP_RESTORE

完整脚本如下:

$user32 = Add-Type -MemberDefinition @' [DllImport("user32.dll", SetLastError = true)] public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, UInt32 uFlags); [DllImport("user32.dll")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); '@ -Name user32 -PassThru $hwnd = (Get-Process code).MainWindowHandle | Where-Object { $_ -ne 0 } $user32::ShowWindow($hwnd, 0x0009 <# SW_RESTORE #>); $user32::SetWindowPos($hwnd, 0, 100, 100, 500, 500, 0);
    
© www.soinside.com 2019 - 2024. All rights reserved.