如何通过编程得到所有窗体的标题及类名称

如果我们想通过编程来检测某个窗口是否存在,我们通常会使用 Windows API 函数中的 FindWindow(),而且,通常我们又不知道要获得窗口的类名称,所以我们经常性的忽略第一个参数,而是只使用窗口标题来检测窗口,这里就出现了一个问题:若该窗口没有标题或有变化标题(如IE)则我们就需要使用类名称进行查询了。当然,我们可以通过 VS 中提供的 spy++ 这样的工具来获得桌面窗口的类名称,但对于没有安装 VC++ 的朋友想必就要借助第三方软件了,与其这样为什么不自己动手呢?其实,对于整个枚举过程,完全可以基于 EnumWindows() 函数,而我们需要做的,只是给这个函数定义一个布尔类型的回调函数(这里我们命名为 EnumFunc()),并给它赋予两个要求的长整参数 hwnd 和 lParam,最后将自己需要的代码内容写入函数内部。具体代码如下:
如果您使用 VC++ 来编程,那么您可以参考以下代码:

#include "stdio.h"
#include "windows.h"
#include "string.h
bool CALLBACK EnumFunc(HWND hwnd, LPARAM lParam)
{
    char sString[2001],sClassName[2001];

    int hFunc=GetClassName(hwnd,(LPTSTR)sClassName,2000);
    int hFunc2=GetWindowText(hwnd,(LPTSTR)sString,2000);

    if(hFunc!=0)
    {
        if(hFunc2==0)
            strcpy(sString,"(无)");
        printf("句柄:%ld 标题:%s 类:%s  \n",hwnd,sString,sClassName);
    }
    return TRUE;
}

void main(int argc, char* argv[])
{

    printf("%s ","取得所有窗口的标题和类名称");
    if(EnumWindows((WNDENUMPROC)EnumFunc,0)==0)
        printf("%s ","枚举出现错误");
}");
}

随机日志

发表评论

0 评论.

Leave a Reply



[ Ctrl + Enter ]

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word