Win32使用GetClassInfoEx获取窗口类信息

#include 
#include "resource.h"
 
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
 
HINSTANCE hInst;
TCHAR szClassName[] = TEXT("getinfoDemo1");
 
int WINAPI
WinMain (HINSTANCE hThisInstance,
         HINSTANCE hPrevInstance,
         LPSTR lpszArgument,
         int nFunsterStil)
{
    HWND hwnd;
    MSG messages;
    WNDCLASSEX wincl;
 
	hInst = hThisInstance;
	
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;
    wincl.style = CS_DBLCLKS;
    wincl.cbSize = sizeof (WNDCLASSEX);
 
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = MAKEINTRESOURCE (IDC_GETINFO1);
    wincl.cbClsExtra = 0;
    wincl.cbWndExtra = 0;
    wincl.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
 
    if (!RegisterClassEx (&wincl))
        return 0;
 
    hwnd = CreateWindowEx (
           0,
           szClassName,
           TEXT("getinfoDemo1"),
           WS_OVERLAPPEDWINDOW,
           CW_USEDEFAULT,
           CW_USEDEFAULT,
           400,
           400,
           HWND_DESKTOP,
           NULL,
           hThisInstance,
           NULL
           );
 
    ShowWindow (hwnd, nFunsterStil);
 
    while (GetMessage (&messages, NULL, 0, 0))
    {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }
 
    return messages.wParam;
}
 
LRESULT CALLBACK
WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc;
	RECT rt;
	int ret;
	HWND hWndDlg = NULL;
	WNDCLASSEX cls1;
	char szBuffer[100];
				
    switch (message)
    {
			case WM_COMMAND:
    		switch (LOWORD(wParam))
    		{
   			case IDM_GetClassInfo:
   				cls1.cbSize=sizeof(WNDCLASSEX);
   				hdc=GetDC(hwnd);
   				ret=GetClassInfoEx(hInst,"AAAA",&cls1);
   				wsprintf(szBuffer, "调用是否成功:%d",ret);
				TextOut(hdc,100,10,szBuffer,lstrlen(szBuffer));
				wsprintf(szBuffer, "结构体大小:%d",cls1.cbSize);
				TextOut(hdc,100,30,szBuffer,lstrlen(szBuffer));
				wsprintf(szBuffer, "窗口类样式:%d",cls1.style);
				TextOut(hdc,100,50,szBuffer,lstrlen(szBuffer));
				wsprintf(szBuffer, "窗口处理函数:0X%X",cls1.lpfnWndProc);
				TextOut(hdc,100,70,szBuffer,lstrlen(szBuffer));
				wsprintf(szBuffer, "窗口类的额外信息:%d",cls1.cbClsExtra);
				TextOut(hdc,100,90,szBuffer,lstrlen(szBuffer));
				wsprintf(szBuffer, "窗口实例的额外信息:%d",cls1.cbWndExtra);
				TextOut(hdc,100,110,szBuffer,lstrlen(szBuffer));
				wsprintf(szBuffer, "本模块的实例句柄:0X%X",cls1.hInstance);
				TextOut(hdc,100,130,szBuffer,lstrlen(szBuffer));
				wsprintf(szBuffer, "窗口类图标:0X%X",cls1.hIcon);
				TextOut(hdc,100,150,szBuffer,lstrlen(szBuffer));
				wsprintf(szBuffer, "窗口类鼠标:0X%X",cls1.hCursor);
				TextOut(hdc,100,170,szBuffer,lstrlen(szBuffer));
				wsprintf(szBuffer, "窗口类的背景刷:0X%X",cls1.hbrBackground);
				TextOut(hdc,100,190,szBuffer,lstrlen(szBuffer));
				wsprintf(szBuffer, "窗口类的菜单名:%s",cls1.lpszMenuName);
				TextOut(hdc,100,210,szBuffer,lstrlen(szBuffer));
				wsprintf(szBuffer, "窗口类名字:%s",cls1.lpszClassName);
				TextOut(hdc,100,230,szBuffer,lstrlen(szBuffer));
				wsprintf(szBuffer, "窗口类小图标:%s",cls1.hIconSm);
				TextOut(hdc,100,250,szBuffer,lstrlen(szBuffer));				
				
   				break;
		    case IDM_ABOUT:
				MessageBox (hwnd, TEXT ("getinfoDemo1 v1.0\nCopyright (C) 2020\n by bo"),
                        TEXT ("getinfoDemo1"), MB_OK | MB_ICONINFORMATION);
				break;
			case IDM_EXIT:
				DestroyWindow(hwnd);
				break;
			default:
				return DefWindowProc(hwnd, message, wParam, lParam);	    		
		    }
    		break;
    	case WM_PAINT:
			hdc = BeginPaint(hwnd, &ps);					
			GetClientRect(hwnd, &rt);			
			
			EndPaint(hwnd, &ps);
			break;
        case WM_DESTROY:
            PostQuitMessage (0);
            break;
        default:
            return DefWindowProc (hwnd, message, wParam, lParam);
    }
 
    return 0;
}

GetClassInfoEx函数,第一个参数是程序实例句柄,第二个参数是要获取信息的窗口类名,第三个参数是WNDCLASSEX结构体地址;调用失败,返回零;调用成功填充WNDCLASSEX结构体;

使用前先

    cls1.cbSize=sizeof(WNDCLASSEX);

获取结构体大小,填充.cbSize成员;

输出返回结构体内容,即窗口类信息;

对于是句柄的成员,格式化为十六进制输出;字符串类型成员格式化为 %s;lpfnWndProc成员则输出窗口处理函数地址;

看不同类名返回的信息;

ret=GetClassInfoEx(hInst,"Edit",&cls1); 程序中没有Edit,系统预定义的类名也可以获取;

Win32使用GetClassInfoEx获取窗口类信息_第1张图片

ret=GetClassInfoEx(hInst,"Button",&cls1);

Win32使用GetClassInfoEx获取窗口类信息_第2张图片

ret=GetClassInfoEx(hInst,"getinfoDemo1",&cls1); 本程序类名,输出一部分后挂了;有时间再看;

Win32使用GetClassInfoEx获取窗口类信息_第3张图片

ret=GetClassInfoEx(hInst,"AAAA",&cls1); 随便写个类名;

Win32使用GetClassInfoEx获取窗口类信息_第4张图片

菜单;

Win32使用GetClassInfoEx获取窗口类信息_第5张图片

工程;

Win32使用GetClassInfoEx获取窗口类信息_第6张图片

Win32使用GetClassInfoEx获取窗口类信息_第7张图片

资源文件,头文件;win7, CFree 5.0;GetClassInfoEx函数和WNDCLASSEX结构体参考Windows API 帮助;

#include "resource.h"
#include 

/////////////////////////////////////////////////////////////////////////////
//
// Menu
//

IDC_GETINFO1 MENU 
BEGIN
    POPUP "&File"
    BEGIN
    	MENUITEM "获取窗口类信息",                	IDM_GetClassInfo
        MENUITEM "E&xit",                	IDM_EXIT
    END
    POPUP "&Help"
    BEGIN
        MENUITEM "&About ...",           	IDM_ABOUT
    END
END
#define 	IDM_EXIT		10001
#define 	IDM_ABOUT		10002

#define 	IDC_GETINFO1		10101
#define 	IDD_ABOUTBOX	10102
#define     IDM_GetClassInfo    40001

 

 

 

你可能感兴趣的:(VC++,WNDCLASSEX,GetClassInfoEx,窗口类,Edit,lpfnWndProc)