gpt4 book ai didi

SetWindowPos doesn't work with VSCode windows(SetWindowPos不适用于VSCode窗口)

转载 作者:bug小助手 更新时间:2023-10-24 21:37:33 37 4
gpt4 key购买 nike



I'm trying to move the VSCode window using SetWindowPos (in PowerShell, however the same should apply to C++ or C# PInvoke) and I cannot get it to work.

我正在尝试使用SetWindowPos来移动VSCode窗口(在PowerShell中,同样的方法应该适用于C++或C#PInvoke),但我无法使其工作。


Even more frustratingly NirSoft's GUIPropView utility does manage to move (using the Center Selected Windows option) the VSCode window somehow. (And of course there's no source code available for that program...)

更令人沮丧的是,NirSoft的GUIPropView实用程序确实设法移动(使用居中选定窗口选项)VSCode窗口。(当然,该程序没有可用的源代码……)


I'm able to get some kind of handle using the following PowerShell code:

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


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

This yields the handle as when using FindWindow (supplying the window class and title of the VSCode instance).

这会产生与使用FindWindow时一样的句柄(提供窗口类和VSCode实例的标题)。


Using this handle for SetWindowPos does absolutely nothing apart from making SetWindowPos return True (meaning it at least didn't fail completely, I suppose?).

对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 also gave me two other handles (shown in a bottom panel in the program), of which I have no idea where they came from, and one of those allows me to resize and reposition the rendered content, but not the whole window itself, producing a result like this:
weirdness

GUIPropView还给了我另外两个句柄(显示在程序的底部面板中),我不知道它们来自哪里,其中一个允许我调整呈现内容的大小和位置,但不是整个窗口本身,从而产生如下结果:


Is there something obvious I'm missing? Should I obtain the window handle differently? What's going on?

我是不是明显漏掉了什么?我应该以不同的方式获取窗口句柄吗?到底怎么回事?


更多回答

Visual Studio code is based on Electron which itself is base on Chromium, so the app has a complex windowing structure, you're probably not getting the good window handle. Otherwise you should UI Automation (learn.microsoft.com/en-us/windows/win32/winauto/…) and the TransformPattern (learn.microsoft.com/en-us/windows/win32/api/uiautomationclient/…) to move a window you don't own, it works fine with VS Code. Not sure what's the easiest way to do it in PowerShell though.

Visual Studio的代码是基于Electron的,而Electron本身也是基于Chromium的,所以这个应用程序有一个复杂的窗口结构,你可能得不到好的窗口句柄。否则,您应该使用UI自动化(learn.microsoft.com/en-us/windows/win32/winauto/…)和TransformPattern(learn.microsoft.com/en-us/windows/win32/api/uiautomationclient/…)要移动您不拥有的窗口,它可以很好地与VS代码配合使用。不过,我不确定在PowerShell中做这件事最简单的方法是什么。

MainWindowHandle is made up by the .NET Framework. It suggests the concept of a "main window," which does not exist in the OS. The implementation is based on heuristics (i.e., guessing) and seemingly ran out of luck in the case of Visual Studio Code.

MainWindowHandle由.NET框架组成。它提出了“主窗口”的概念,而这在操作系统中并不存在。该实现是基于启发式的(即,猜测),在使用Visual Studio代码的情况下似乎没有运气了。

@SimonMourier Yeah I figured I was targetting the wrong handle already, but that makes me really confused as to how GUIPropView is somehow able to target the right handle. I suppose if I could iterate over all window handles and setting their position I'll eventually end up moving the right one, right? Or would that introduce even more issues? UI Automation looks really complicated, do you happen to know about an example program for moving windows or something in that direction?

@SimonMourier是的,我认为我已经瞄准了错误的句柄,但这让我真的很困惑GUIPropView如何能够瞄准正确的句柄。我想,如果我可以迭代所有窗口句柄并设置它们的位置,我最终会移动正确的一个,对吗?或者这会带来更多的问题?UI Automation看起来真的很复杂,您是否知道用于在该方向上移动窗口或其他东西的示例程序?

The problem is you take the first code.exe process, while since it's chromimum undecovers, it spawns a lot of these. Here is a simple C# + UIAutomation code that works: pastebin.com/raw/RvVHyb8Y

问题是您采用的是第一个code.exe进程,而由于它是chromium undecovers,所以它会产生很多这样的进程。下面是一个简单的C#+UIAutomation代码:pastebin.com/raw/RvVHyb8Y

@SimonMourier Ah I figured it out, apparently VSCode doesn't like to be moved while maximized (I've tested this with other applications such as Firefox and they didn't have any issues with that). Your UIAutomation code retrieves the window handle the exact same way I do in PowerShell - (Get-Process code).MainWindowHandle | where { $_ -ne 0 } does the same as (Get-Process code | where { $_.MainWindowHandle -ne 0 }).MainWindowHandle or your C# code Process.GetProcessesByName("code").FirstOrDefault(p => p.MainWindowHandle != IntPtr.Zero).

@SimonMourier啊,我明白了,显然VSCode不喜欢在最大化的时候被移动(我已经用Firefox等其他应用程序测试了这一点,他们没有任何问题)。您的UIAutomation代码检索窗口句柄的方式与我在PowerShell中所做的完全相同-(Get-Process代码)。MainWindowHandle|Where{$_-ne 0}与(Get-Process代码|Where{$_.MainWindowHandle-ne 0})相同。MainWindowHandle或C#代码Process.GetProcessesByName(“code”).FirstOrDefault(p=>p.MainWindowHandle!=IntPtr.Zero)。

优秀答案推荐

Turns out the code I had for finding the handle to the VSCode window was actually fine. The issue was trying to move/resize the window while it was still maximized. (Note that other applications I tested this with, such as Firefox, do not have any issues with that.)

原来,我用来查找VSCode窗口句柄的代码实际上是正确的。问题是在窗口仍处于最大化状态时尝试移动窗口/调整窗口大小。(请注意,我测试的其他应用程序,如Firefox,在这方面没有任何问题。)


To unmaximize/unminimize/unarrange the window, we can use ShowWindow with SWP_RESTORE.

要取消最大化/取消最小化/取消排列窗口,可以使用带有SWP_RESTORE的ShowWindow。


The full script is as follows:

全文如下:


$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);

更多回答

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com