c ++ WTL/ATL如何从当前进程中查找窗口句柄? [英] c++ WTL/ATL How to find a window handle from the current process?

查看:339
本文介绍了c ++ WTL/ATL如何从当前进程中查找窗口句柄?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个库函数(对链接到的应用程序一无所知)有时会打开一个窗口.

如果在打开库窗口的同时关闭了应用程序的主窗口,那么即使关闭了库函数寡妇,该过程也将进入一种奇怪的状态,即不会退出.

如果库函数由主窗口拥有",则一切均关闭.但是,通常,库函数不知道打开了哪些窗口.

标准的Windows函数似乎是在所有窗口中搜索所有进程,或者它们需要一个窗口句柄才能找到另一个窗口句柄.

如何找到我的函数正在运行的进程的任何顶级窗口?
================================================== ====================
解决方案,笨拙但经过测试,可以工作.

静态布尔回调MyEnumThreadWndProc(HWND hwnd,LPARAM param){
HWND * oW = reinterpret_cast< hwnd *> (param);
(* oW)= hwnd;
返回false;
}

...
HWND oWind = 0;
EnumThreadWindows(GetCurrentThreadId(),& MyEnumThreadWndProc,reinterpret_cast< lparam>(& oWind));

I have a library function (that does not know anything about the application its linked to) that sometimes opens a window.

If the application''s main window is closed while the library window is open, the process gets into a weird state that wont exit, even after the library function widow is closed.

If the library function is "owned" by the main window, everything closed fine. However, in general the library function has no idea what windows are open.

The standard windows functions appear to either search all windows for all processes, or they require a window handle to find another window handle.

How do I find ANY top level window of the process my function is running in?
=======================================================================
Solution, clunky but tested, works.

static BOOL CALLBACK MyEnumThreadWndProc( HWND hwnd, LPARAM param ){
HWND *oW = reinterpret_cast<hwnd*> (param);
(*oW) = hwnd;
return false;
}

...
HWND oWind=0;
EnumThreadWindows(GetCurrentThreadId(), &MyEnumThreadWndProc, reinterpret_cast<lparam>(&oWind) );

推荐答案

您可以使用系统调用 EnumWindows 查找所有顶级窗口.您的应用程序关闭所有其自己的窗口后,不应再保留其他窗口.如果EnumWindows检测到任何东西,则可能是您的函数创建的另一个窗口.

如果您知道该窗口的窗口类或窗口名称,则可以进一步验证是否找到了正确的窗口.

如果您有一个具有多个UI线程的多线程应用程序,则可以使用 EnumThreadWindows 枚举属于特定线程的所有顶级窗口.
You can use the system call EnumWindows to find all of your top-level windows. After your application has closed all its own windows, no other window should remain. If EnumWindows detects anything, then it''s probably the additional window that your function has created.

If you know the window class or window name of that window, you could do a further verification that you have found the right window.

If you have a multi-threaded application with multiple UI threads you can use EnumThreadWindows to enumerate all top-level windows belonging to a particular thread.


能够从导出的函数调用AfxGetMainWnd()并从调用导出的函数的线程获取顶层窗口(如果没有与该线程关联的窗口,则可能返回NULL).
请参阅文档:
http://msdn.microsoft.com/zh-CN/library/waas15s1%28v = vs.80%29.aspx [ ^ ]

或者...您可以使用AfxGetApp(),然后调用CWinThread::GetMainWnd()(记忆体CWinApp 源自CWinThread).
参考:
http://msdn.microsoft.com/zh-CN/library/5k9f064x%28v = vs.80%29.aspx [ ^ ]
http://msdn.microsoft.com/zh-CN/library/sazyysab%28v = VS.80%29.aspx [ ^ ]
You should be able to call AfxGetMainWnd() from the exported function and get the top-level window from the thread calling the exported function (may return NULL if there''s no window associated with the thread).

See documentation:
http://msdn.microsoft.com/en-US/library/waas15s1%28v=vs.80%29.aspx[^]

Or... you can use AfxGetApp(), then call CWinThread::GetMainWnd() (rememeber CWinApp is derived from CWinThread).
Reference:
http://msdn.microsoft.com/en-US/library/5k9f064x%28v=vs.80%29.aspx[^]
http://msdn.microsoft.com/en-US/library/sazyysab%28v=VS.80%29.aspx[^]


这篇关于c ++ WTL/ATL如何从当前进程中查找窗口句柄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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