FindWindow如何找到一个窗口EnumChildWindows不? [英] How come FindWindow finds a window that EnumChildWindows doesn't?

查看:131
本文介绍了FindWindow如何找到一个窗口EnumChildWindows不?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一个类名为CLIPBRDWNDCLASS的窗口(可以在办公应用和其他应用程序中找到)。



如果我使用FindWindow或FindWindowEx我找到第一个HWND有这个类,但我想所有该类的窗口,所以我决定使用递归EnumChildWindows枚举所有窗口,并找到我想要的窗口:

  // --------------------------- -------------------------------------------------- -  
BOOL CALLBACK enum_wnd_proc(HWND h,LPARAM lp)
{
char cls [1024] = {0};
:: GetClassNameA(h,cls,1024);

if(std :: string(cls)==CLIPBRDWNDCLASS)
{
// match!
}

:: EnumChildWindows(h,enum_wnd_proc,NULL);

return TRUE;
}
// --------------------------------------- ----------------------------------------
int _tmain(int argc, _TCHAR * argv [])
{
:: EnumWindows(enum_wnd_proc,NULL);
return 0;
}
// --------------------------------------- ----------------------------------------



这是这个窗口不会被EnumWindows返回,只有FindWindow。




解决方案

原因 EnumWindows 不工作是你正在寻找的窗口是仅消息窗口



FindWindowEx 可在两种情况下找到它们:


  1. 如果 hwndParent hwndChildAfter 都为NULL。

  2. 如果您指定HWND_MESSAGE作为父窗口。

此代码将为您找到所有相关窗口此处的解决方案的版本):

  HWND hWindow = FindWindowExA(HWND_MESSAGE,NULL,CLIPBRDWNDCLASS,NULL); 
while(hWindow)
{
//用窗口做这里的事情...

//查找下一个窗口
hWindow = FindWindowExA(HWND_MESSAGE, hWindow,CLIPBRDWNDCLASS,NULL);
}

另请注意,与上面链接中写的不同, GetParent()对于只有消息的窗口返回 HWND_MESSAGE (至少不是我的测试) p>

I'm looking for a window that its class name is "CLIPBRDWNDCLASS" (it can be found in office apps and other applications).

If I use FindWindow or FindWindowEx I find the first HWND that has this class, but I want all the windows with that class, so I decided to use recursive EnumChildWindows to enumerate all windows and find the window I want:

//-------------------------------------------------------------------------------
BOOL CALLBACK enum_wnd_proc(HWND h, LPARAM lp)
{
    char cls[1024] = {0};
    ::GetClassNameA(h, cls, 1024);

    if(std::string(cls) == "CLIPBRDWNDCLASS")
    {
        // match!
    }

    ::EnumChildWindows(h, enum_wnd_proc, NULL);

    return TRUE;
}
//-------------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
    ::EnumWindows(enum_wnd_proc, NULL); 
    return 0;
}
//-------------------------------------------------------------------------------

The this is that this window does not return by the EnumWindows, only by FindWindow.

Does anyone can tell why it doesn't work ???

解决方案

The reason EnumWindows doesn't work is that the window you are looking for is a message only window.

FindWindowEx can find them in two cases:

  1. If both hwndParent and hwndChildAfter are NULL.
  2. If you specify 'HWND_MESSAGE' as your parent window.

This code will find all the relevant windows for you (a modified version of a solution from here):

HWND hWindow = FindWindowExA(HWND_MESSAGE, NULL, "CLIPBRDWNDCLASS", NULL);
while (hWindow )
{
    // Do something here with window...

    // Find next window
    hWindow = FindWindowExA(HWND_MESSAGE, hWindow , "CLIPBRDWNDCLASS", NULL);
}

Also note that unlike what's written in the above link, GetParent() for message only windows does not return HWND_MESSAGE (at least not for my tests).

这篇关于FindWindow如何找到一个窗口EnumChildWindows不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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