


Win32 SDK Basics (5) A brief introduction to the window class
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; }
"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; } }
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:
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.
我们在上一文中注册的窗口就是一个局部的窗口类,它的特点就是只能在注册的作用域内使用,由于它和全局窗口类只在注册的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!wce.style = CS_HREDRAW | CS_VREDRAW;//未添加CS_GLOBALCLASS

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Windows 11’s native apps (like File Explorer) and other shell apps use rounded corners and Fluent Design elements (like Acrylic) by default. In addition to rounded corners, another important design feature of Windows 11 is materials like mica, which aligns the background color of apps with the desktop. Mica is similar to acrylic but works slightly differently. As Microsoft describes it, Windows Mica Materials creates "color hierarchy by aligning backgrounds with apps

The new Windows 11 SDK for build 22523 revealed that Microsoft is developing a new blur effect for Windows 11. This effect is called Tabbed, and is in addition to Acrylic and Mica. The new DWMWA_SYSTEMBACKDROP_TYPE in the 22523 SDK, Mica, Acrylic’s public Win32 API and their weird new “tab” mix: pic.twitter.com/dbsu7ZFiIi — It’s All Back (@StartIsBack) December 15, 2021 Available in the following SDK’s Sample application

Antivirus software may sometimes display a warning stating Trojan/win11.casdet rfn on Windows 32 laptops. It indicates that the PC is infected with Trojan malware, causing it to malfunction. Fortunately, there are some possible ways to fix this problem, as explained below. Additionally, you may be interested in our detailed guide on whether cdn.districtm.io is a pop-up/virus/malware or not. What is Trojan/win32.casdet rfn? Trojan/win32.casdet rfn is a severe Trojan virus infection that can overrun the system and kill its processes, making it easy

The difference between win32 and win64 is: 1. win32 refers to the 32-bit environment of the Microsoft Windows operating system, and win64 refers to the 64-bit version of the Microsoft Windows operating system, which is more stable and faster than the 32-bit version; 2. win32 supports up to 2G of memory. win64 must have more than 4G of memory; 3. win64 supports 64-bit processors, but win32 cannot fully support it; 4. win32 pursues simplicity, while win64 pursues performance.

Back in August 2021, Microsoft promised that OneNote on Windows 10 and Windows 11 would receive a series of major feature updates in the coming months, as the tech giant unifies multiple versions of the note-taking app into a single one. part of the efforts of customers. OneNote has undergone many changes over the past few years. In 2018, when Microsoft really wanted people to use its UWP version of OneNote, the company stopped bundling the original and feature-rich version of OneNote with preinstalled Office apps. Instead, Microsoft has only provided a UWP client and added new features for modern versions. Microsoft later changed

The WindowsAppSDK is a set of tools and APIs that developers can use in their Windows applications to provide "consistent" functionality across a variety of devices using Windows 10 (version 1809 and later) and Windows 11. It's really important to understand that it doesn't replace existing application types like .NET or Windows SDK, it just provides a unified API toolset that can be used to complement your existing applications. Today, Microsoft released version 1.2 of Windows App SDK with many new features. The highlight of this release may be third-party developers

Master the essential skills for secondary development of Java Hikvision SDK Introduction: With the rapid development of information technology, video surveillance systems have been widely used in various fields. As the leading domestic video surveillance solution provider, Hikvision’s products and technologies have always occupied an important position in the market. In order to meet the needs of different projects, Hikvision provides SDK for developers to carry out secondary development. This article will introduce some essential skills for mastering the secondary development of Java Hikvision SDK, and attach corresponding code examples. 1. Understand Hikvision

The full name of sdk is "Software Development Kit", which means "software development kit" in Chinese. It is a set of tools provided by manufacturers of hardware platforms, operating systems (OS) or programming languages. SDKs assist software developers in creating applications for specific platforms, systems or programming languages. A basic SDK usually consists of a compiler, debugger, and application programming interface (API), but may also include other content, such as: documentation, libraries, runtime/development environment, testing/analysis tools, network protocols, etc.
