如何为所有opend窗口获取(hwnd)句柄窗口 [英] How to get (hwnd)handle window for all opend windows

查看:86
本文介绍了如何为所有opend窗口获取(hwnd)句柄窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





以下代码返回Excel的唯一ID。



Hi,

The below code returns unique Id for the Excel.

[DllImportAttribute("User32.dll")]
        private static extern int FindWindow(String ClassName, String WindowName);
int hWnd = FindWindow(null,"Micosoft Excel");







但我打开了一个excel文件名Book1.xls,下面的代码返回0.






But i have opened one excel file name "Book1.xls" the below code return 0.

<pre lang="cs">[DllImportAttribute(&quot;User32.dll&quot;)]
        private static extern int FindWindow(String ClassName, String WindowName);
int hWnd = FindWindow(null,""Book1"");</pre>







你能不能请帮我获取打开文件的唯一ID。



谢谢,




can you please help me to get unique id of my opened file.

Thanks,

推荐答案

如果你真的想要要获取Windows会话中的所有窗口,您可以首先检索桌面的HWMD,然后获取其所有子项。请参阅:

http ://msdn.microsoft.com/en-us/library/windows/desktop/ms633504%28v=vs.85%29.aspx [ ^ ],

< a href =http://msdn.microsoft.com/en-us/library/windows/desktop/ms633515%28v=vs.85%29.aspx> http://msdn.microsoft.com/en-us /library/windows/desktop/ms633515%28v=vs.85%29.aspx [ ^ ]。



然而,我''我不确定你是否需要那个。相反,您可以获取所有正在执行的进程,然后尝试获取每个执行进程的主窗口句柄(它可能有也可能没有,并且此尝试可能会抛出异常,所以要小心)。方法如下:

http://msdn.microsoft.com/en- us / library / 1f3ys1f9.aspx [ ^ ],

http://msdn.microsoft.com/ en-us / library / x8b2hzk8.aspx [ ^ ],

http://msdn.microsoft。 com / en-us / library / z3w4xdc9.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.mainwindowhandle.aspx [ ^ ](这里注意可以抛出的异常)。



最后,我不确定整个想法是否非常有用。您无法100%保证您找到的流程或窗口正是您真正需要的流程或窗口。对于某些不相关的应用程序,进程和窗口标题可能会意外地相同。此外,您访问一个单独的进程是非常有限的,因为这些进程是完全隔离的。有可能,你需要使用Office自动化或其他东西。



-SA
If you really want to get all windows in the Windows session, you can first retrieve the HWMD of desktop and than get all its children. Please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633504%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms633515%28v=vs.85%29.aspx[^].

However, I''m not sure you need exactly that. Instead, you can get all the executing processes and then try to get a main window handle of each (which it may or may not have, and this attempt can throw an exception, so be careful). Here is how:
http://msdn.microsoft.com/en-us/library/1f3ys1f9.aspx[^],
http://msdn.microsoft.com/en-us/library/x8b2hzk8.aspx[^],
http://msdn.microsoft.com/en-us/library/z3w4xdc9.aspx[^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.mainwindowhandle.aspx[^] (here, pay attention for the exceptions which can be thrown).

And finally, I''m not sure the whole idea is very useful. You cannot get 100% guarantee that a process or a window you find is exactly the one you really need. Both process and window title can accidentally be the same for some unrelated applications. Besides, you access to a separate process is quite limited, because the processes are well isolated. Chances are, you would need to use Office automation, or something else.

—SA


这篇关于如何为所有opend窗口获取(hwnd)句柄窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆