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:402396browse

1. The search process of window classes

In the previous article, we introduced the three window classes of the windows system - 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 step 2. If not found, execute step 3.

(2)Compare the local window class with the HINSTANCE variable passed 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 step 3.

(3)Search in the global window class of the application. If found, perform step 4. If not found, perform step 5 steps.

(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 classAPI RegisterClass and RegisterClassEx

RegisterClass and RegisterClassEx can 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 the API prototype 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 classes structure is that WNDCLASSEX contains The structure size of the cbSize window and the small icon handle of the hIconSm window. Please refer to MSDN for the meaning of other parameters.

3. Get registered window informationGetClassInfo

GetClassInfo() APIcanGet registered The window information, its function prototype 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 to NULL, 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 in WndClass structure variable, used to receive window class information.

    Let’s find the window information of the Button class created above:

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

        Let’s set a breakpoint , check the obtained Button window 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 to NULL, it will search from three scopes: system, global and local.

The following code uninstalls the registered Button window 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