Home>Article>Operation and Maintenance> Win32 SDK Basics (6) Detailed explanation of the window class search process and related APIs

Win32 SDK Basics (6) Detailed explanation of the window class search process and related APIs

黄舟
黄舟 Original
2017-06-06 09:44:40 2332browse

1. The search process of window classes

In the previous article, we introduced the three window classes of thewindowssystem - system window class, Global window class and local window class. Each window class has a different scope in the operating system, and the window class name registered in each scope cannot be repeated. When creating a window, it is often searched according to the window class name in the system, global, and local scopes. The search process is summarized as follows:

(1)The operating system searches based on the incoming window class name. Now search in the local window class. If found, execute step2. If not found, execute step3.

(2)Compare the local window class with theHINSTANCEvariablepassed in when creating the window. If they are found to be equal, it means that the created and registered windows are in the same module, and the created window is returned. If not equal, continue to step3.

(3)Search in the global window class of the application. If found, perform step4. If not found, perform step5steps.

(4) Use the found window class information to create a window and return.

(5) Search in the system window class. If it is found, create the window. If it is not found, the window creation fails.

2. Register the window classAPIRegisterClassand RegisterClassEx

RegisterClass and RegisterClassExcan be used to register window classes. Their two prototypes are as follows:

ATOM WINAPI RegisterClass( _In_ const WNDCLASS *lpWndClass ); ATOM WINAPI RegisterClassEx( _In_ const WNDCLASSEX *lpwcx );

It can be seen from theAPIprototype that the two prototypes are as follows: The difference mainly lies in the parameters received, which is the window class we need to register. The two window classes are declared as follows:

typedef struct tagWNDCLASS { UINT style; WNDPROC lpfnWndProc; int cbClsExtra; int cbWndExtra; HINSTANCE hInstance; HICON hIcon; HCURSOR hCursor; HBRUSH hbrBackground; LPCTSTR lpszMenuName; LPCTSTR lpszClassName; } WNDCLASS, *PWNDCLASS;
typedef struct tagWNDCLASSEX { UINT cbSize; UINT style; WNDPROC lpfnWndProc; int cbClsExtra; int cbWndExtra; HINSTANCE hInstance; HICON hIcon; HCURSOR hCursor; HBRUSH hbrBackground; LPCTSTR lpszMenuName; LPCTSTR lpszClassName; HICON hIconSm; } WNDCLASSEX, *PWNDCLASSEX;

As you can see from the above code, the main difference between the two window classesstructureis thatWNDCLASSEX contains The structure size of the cbSize window and the small icon handle of the hIconSmwindow. Please refer toMSDNfor the meaning of other parameters.

3.Get registered window informationGetClassInfo

GetClassInfo() APIcanGet registered The window information, itsfunctionprototype is as follows:

BOOL WINAPI GetClassInfo( _In_opt_ HINSTANCE hInstance, _In_ LPCTSTR lpClassName, _Out_ LPWNDCLASS lpWndClass );

hInstance—— is to set the search scope, if set toNULL, will be searched from three scopes: system, global and local.

lpClassName - is the name of the window class to be found.

lpWndClass - The address of the passed inWndClassstructure variable, used to receive window class information.

Let’s find the window information of theButtonclass created above:

WNDCLASS wc; if (GetClassInfo(NULL, "Button", &wc) == false) MessageBox(NULL,"GetClassInfo Faile",NULL,NULL);

Let’s set a breakpoint , check the obtainedButtonwindow class information:

4.Uninstall the window classUnregisterClass

We can unregister the registered window class through UnregisterClass. Its prototype is as follows:

BOOL WINAPI UnregisterClass( _In_ LPCTSTR lpClassName, _In_opt_ HINSTANCE hInstance );

lpClassName - is the name of the window class to be uninstalled.

hInstance—— is to set the search scope. If it is set toNULL, it will search from three scopes: system, global and local.

The following code uninstalls the registeredButtonwindow class:

if (UnregisterClass("Button",NULL) == false) MessageBox(NULL, "UnregisterClass Faile", NULL, NULL);

The above is the detailed content of Win32 SDK Basics (6) Detailed explanation of the window class search process and related APIs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn