Home  >  Article  >  Operation and Maintenance  >  Win32 SDK Basics (5) A brief introduction to the window class

Win32 SDK Basics (5) A brief introduction to the window class

黄舟
黄舟Original
2017-06-06 09:42:552562browse

1. Introduction

In the previous article, we created a window from scratch. One of the most important steps is to register the window class, such as the following code:

//注册窗口类
BOOL Register(LPSTR lpClassName, WNDPROC wndProc)
{
    WNDCLASSEX wce = { 0 };
    wce.cbSize = sizeof(wce);
    wce.cbClsExtra = 0;
    wce.cbWndExtra = 0;
    wce.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wce.hCursor = NULL;
    wce.hIcon = NULL;
    wce.hIconSm = NULL;
    wce.hInstance = g_hInstance;
    wce.lpfnWndProc = wndProc;
    wce.lpszClassName = lpClassName;
    wce.lpszMenuName = NULL;
    wce.style = CS_HREDRAW | CS_VREDRAW;
    ATOM nAtom = RegisterClassEx(&wce);
    if (nAtom == 0)
        return FALSE;
    return true;
}

Any window under Windows must be specified in the system before creation Registration, when we use CreateWindowEx to create a window, the name of the second parameter is the window class name. This name should be unique within the scope visible to our code. This article mainly discusses the issues of window classes under Windows.

2. Window Classification

All visible elements in Windows basically belong to a window, regardless of its shape No matter how, it is round, square, or even irregular. All these windows belong to a certain window class. Generally speaking, Windows window classes are divided into three types:
(1) System window class
(2) Global window class
(3) Local window Class
Below, I will discuss the classification of window classes in Windows respectively.

2.1 System window class

A button and an edit box, all of which we use in the windows operating system The controls visible in are actually a window. When we install the operating system, Windows will register a large number of system-level window classes within the operating system. When we develop, we can create these windows directly based on the window class names. To explain this problem, we introduce the following code on top of the code in the previous article.

HWND CreateMain(LPSTR lpClassName, LPSTR lpWndName)
{
	HWND hWnd = CreateWindowEx(0, lpClassName, lpWndName,
		WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, g_hInstance, NULL);
	return hWnd;
}
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
	_In_opt_ HINSTANCE hPrevInstance,
	_In_ LPWSTR    lpCmdLine,
	_In_ int       nCmdShow)
{
	// TODO: Place code here.

	g_hInstance = hInstance;
	
	HWND hWnd = CreateMain("Button", "window");
	Display(hWnd);
	Message();
	return 0;
}

CreateMain is a function we defined to create a window. It receives two parameters, the first is the registered window class name, and the second is the title of the window. Unlike the code in the previous article, we have omitted the step of registering a custom window. Instead, in the WinMain function, CreateMain is called to create a window with the window class name "Button". I think you already know what we're going to do, right? Yes, it is to generate a Button button. All the controls we commonly use have been registered as system window classes by the operating system, and we can use them directly. Please see the results of the following program:


# You can also try to create an edit box such as , drop-down boxes and other system-level windows.

2.2 Global window class

The global window class refers to the window class that can be used in the global scope of the application after registration. For example, we can register the global window class in the dll, then all programs that introduce the dll can use this class. When registering a window class for global use, we only need to add the CS_GLOBALCLASS attribute to the style member of wec's structure when registering, as follows:

	wce.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;

In order to verify this problem, we add a new dll project, and then define a window registration function RegisterWindow() as follows:

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
	case WM_DESTROY:
		PostQuitMessage(0);//可以使GetMessage返回0
		break;
	default:
		break;
	}
	return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

 BOOL RegisterWindow()
{
	WNDCLASSEX wce = { 0 };
	wce.cbSize = sizeof(wce);
	wce.cbClsExtra = 0;
	wce.cbWndExtra = 0;
	wce.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wce.hCursor = NULL;
	wce.hIcon = NULL;
	wce.hIconSm = NULL;
	wce.hInstance = NULL;
	wce.lpfnWndProc = WndProc;
	wce.lpszClassName = "DllMain";
	wce.lpszMenuName = NULL;
	wce.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
	ATOM nAtom = RegisterClassEx(&wce);
	if (nAtom == 0)
		return FALSE;
	return true;
}

The name of the window class registered in RegisterWindow() is

"DllMain". We call this registration function in the main function of dll to complete the registration. :

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	....
	RegisterWindow();
	return TRUE;
}
}

Use this window class to create a window in our main program:


int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
	_In_opt_ HINSTANCE hPrevInstance,
	_In_ LPWSTR    lpCmdLine,
	_In_ int       nCmdShow)
{
	...
	HWND hWnd = CreateMain("DllMain", "我是一个Dll注册的窗口");
	Display(hWnd);
	Message();
	return 0;
}

Run Program you will find that we have also successfully produced this window:



##2.3 Local window class

Local window class By definition, any registered window class that does not add CS_GLOBALCLASS to the style of wce is a local window kind.


wce.style = CS_HREDRAW | CS_VREDRAW;//未添加CS_GLOBALCLASS

       我们在上一文中注册的窗口就是一个局部的窗口类,它的特点就是只能在注册的作用域内使用,由于它和全局窗口类只在注册的style和作用域上有分别,这里就不再详述,如要了解,请参照上一文。

The above is the detailed content of Win32 SDK Basics (5) A brief introduction to the window class. 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