如何在 VBA 中获取已执行进程的窗口句柄?
在我的 VBA 应用程序中,我启动 IExplore 进程:
Shell sIE, vbMaximizedFocus
现在我需要调整创建的窗口的大小。为此,我可以使用 SetWindowPos 函数,该函数将窗口句柄作为参数之一。我没有那个句柄...
我会使用 FindWindowLike 函数(它会抛出窗口,将标题与模式进行比较,并返回具有匹配标题的窗口句柄数组),但我不能依赖窗口标题。我也不能只调整所有 IE 窗口的大小。
所以,我正在考虑使用一些东西来为我提供刚刚运行的进程的窗口句柄。壳牌不提供这个。
我有一些示例代码,如何使用 CoCreateInstance 函数在 C++ 中执行此操作:
CoCreateInstance(CLSID_InternetExplorer, NULL, CLSCTX_LOCAL_SERVER, IID_IWebBrowser2, (void**)&m_pBrowser);
if (m_pBrowser)
{
pom = buffer;
m_pBrowser->put_Visible(VARIANT_TRUE);
m_pBrowser->Navigate(pom, &(_variant_t(flaga)), &vDummy, &vDummy, &vDummy);
m_pBrowser->get_HWND((long *)&hWnd);
if (hWnd != NULL)
{
...
...
我会将其移植到 VBA,但我不太确定第四个参数应该放什么:
里德 [in] 引用用于与对象通信的接口标识符。
好吧,我不知道我应该通过的女巫界面...我什至不确定是否可以在 VBA 中使用它。
所以。有没有一种方法可以执行进程,为我提供其窗口的句柄?
In my VBA application I start IExplore process with:
Shell sIE, vbMaximizedFocus
Now I need to resize created window. For that I can use SetWindowPos function, which takes a handle to the window as one of the arguments. And I don't have that handle...
I would use FindWindowLike function (which goes threw windows, compares caption with pattern and returns array of handles of windows with matching caption), but I cann't rely on window caption. I cann't just resize all of the IE windows also.
So, I was thinking of using SOMETHING that would give me a handle of a window to the process I just ran. Shell does not provide this.
I have some example code, how to do this in C++ using CoCreateInstance function:
CoCreateInstance(CLSID_InternetExplorer, NULL, CLSCTX_LOCAL_SERVER, IID_IWebBrowser2, (void**)&m_pBrowser);
if (m_pBrowser)
{
pom = buffer;
m_pBrowser->put_Visible(VARIANT_TRUE);
m_pBrowser->Navigate(pom, &(_variant_t(flaga)), &vDummy, &vDummy, &vDummy);
m_pBrowser->get_HWND((long *)&hWnd);
if (hWnd != NULL)
{
...
...
I would've port this to VBA, but I'm not so sure, what to put for fourth parameter:
riid
[in] Reference to the identifier of the interface to be used to communicate with the object.
Well I don't know witch interface I should pass... I'm not even sure if I can use it in VBA.
So. Is there a way to execute process, which would provide me a handle to it's window?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
和蒂姆·威廉姆斯一起去。您可以通过使用 create object 来获取 IE 对象而不是使用 shell 调用来轻松完成此操作。这使得事情变得更容易,因为您可以访问该对象,并且不需要在事后尝试查找窗口句柄。
如果您正在处理多个监视器或需要对窗口进行更多控制,那么事情就会变得更加棘手。这是我之前解决这个问题的答案之一: 是否可以在当前监视器中最大化的 VBA 中启动浏览器窗口?
编辑。将窗口设置到某个位置:
To go along with Tim Williams. You can do it quite easily by using create object to get an IE object vs using a shell call. This makes it easier since you have access to the object, and don't need try to look up the window handle after the fact.
If you are dealing with multiple monitors or need more control over the window, then it gets a little more tricky. Here is one of my previous answers to address that: Is it possible to launch a browser window in VBA maximized in the current monitor?
EDIT. Set window to certain position:
在得到这个问题的明确答案之前,我“谷歌搜索”并测试了“解决方案”很长一段时间。要点是您应该确切地知道您打算用 Hwnd 做什么!
原因是,虽然 PID 保持不变,但相应的活动 Hwnd 可能会发生变化 - 例如,当窗口启动[或]最小化时。在这种情况下,我没有成功地从 PID 中找到我正在寻找的“真实”窗口(即恢复它)。请注意,窗口文本标题也可能会更改...(使用下面的测试函数进行检查)
以下函数从 PID 中检索'the' Hwnd(来自 CodeGuru,已修改):
如上所述,这不会返回任何内容如果您的窗口最小化(最终带有另一个文本标题),则很有用。因此,我建议使用部分文本标题(来自VBforum,已修改)来搜索所需的窗口:
因此,请检查在您的特定情况下哪种更适合您。您可以使用以下测试子(只需替换您的应用程序即可):
I 'googled' and tested the 'solutions' quite a long time before getting a clear answer for this issue. The essential point is that you should exactly know what you intend to do with the Hwnd !
The reason is that while the PID remains constant, the corresponding active Hwnd may change - for example when the window starts [or gets] minimized. In such case, I didn't success in finding the 'real' window I was looking for from the PID (i.e. to restore it). Note that the window text caption may change, too… (check using test function below)
The following function retrieves 'the' Hwnd from the PID (from CodeGuru, modified):
As explained above, this returns nothing useful if your window is minimized (eventually with another text caption). Hence, I recommend to search for the desired window using a partial text caption (from VBforum, modified):
Thus, please check what suits you better in your particular case. You may use the following test sub (just replace the apps by yours):