Window Classes

本文详细介绍了窗口类的概念,包括不同类型的窗口类及其系统定位方式。解释了窗口类如何定义默认行为,并提供了注册本地窗口及创建主窗口的代码示例。此外还列举了一系列用于窗口类操作的函数,如获取、设置、注册等。
Window Classes

Window Classes

This topic describes the types of window classes, how the system locates them, and the elements that define the default behavior of windows that belong to them.

A window class is a set of attributes that the system uses as a template to create a window. Every window is a member of a window class. All window classes are process specific.

Overviews

About Window Classes

Each window class has an associated window procedure shared by all windows of the same class. The window procedure processes messages for all windows of that class and therefore controls their behavior and appearance.

Using Window Classes

This topic has a code example that shows how to register a local window and use it to create a main window.

Functions

GetClassInfo

The GetClassInfo function retrieves information about a window class.

Note    The GetClassInfo function has been superseded by the GetClassInfoEx function. You can still use GetClassInfo , however, if you do not need information about the class small icon.
GetClassInfoEx

The GetClassInfoEx function retrieves information about a window class, including a handle to the small icon associated with the window class. The GetClassInfo function does not retrieve a handle to the small icon.

GetClassLong

The GetClassLong function retrieves the specified 32-bit (long ) value from the WNDCLASSEX structure associated with the specified window.

GetClassLongPtr

The GetClassLongPtr function retrieves the specified value from the WNDCLASSEX structure associated with the specified window.

If you are retrieving a pointer or a handle, this function supersedes the GetClassLong function. (Pointers and handles are 32 bits on 32-bit Windows and 64 bits on 64-bit Windows.) To write code that is compatible with both 32-bit and 64-bit versions of Windows, use GetClassLongPtr .

GetClassName

The GetClassName function retrieves the name of the class to which the specified window belongs.

GetClassWord

The GetClassWord function retrieves the 16-bit (WORD ) value at the specified offset into the extra class memory for the window class to which the specified window belongs.

Note    This function is deprecated for any use other than nIndex set to GCW_ATOM. The function is provided only for compatibility with 16-bit versions of Windows. Applications should use the GetClassLong function.
GetWindowLong

The GetWindowLong function retrieves information about the specified window. The function also retrieves the 32-bit (long ) value at the specified offset into the extra window memory.

If you are retrieving a pointer or a handle, this function has been superseded by the GetWindowLongPtr function. (Pointers and handles are 32 bits on 32-bit Windows and 64 bits on 64-bit Windows.) To write code that is compatible with both 32-bit and 64-bit versions of Windows, use GetWindowLongPtr .

GetWindowLongPtr

The GetWindowLongPtr function retrieves information about the specified window. The function also retrieves the value at a specified offset into the extra window memory.

If you are retrieving a pointer or a handle, this function supersedes the GetWindowLong function. (Pointers and handles are 32 bits on 32-bit Windows and 64 bits on 64-bit Windows.) To write code that is compatible with both 32-bit and 64-bit versions of Windows, use GetWindowLongPtr .

RegisterClass

The RegisterClass function registers a window class for subsequent use in calls to the CreateWindow or CreateWindowEx function.

The RegisterClass function has been superseded by the RegisterClassEx function. You can still use RegisterClass , however, if you do not need to set the class small icon.

RegisterClassEx

The RegisterClassEx function registers a window class for subsequent use in calls to the CreateWindow or CreateWindowEx function.

SetClassLong

The SetClassLong function replaces the specified 32-bit (long ) value at the specified offset into the extra class memory or the WNDCLASSEX structure for the class to which the specified window belongs.

Note   This function has been superseded by the SetClassLongPtr function. To write code that is compatible with both 32-bit and 64-bit versions of Windows, use SetClassLongPtr .
SetClassLongPtr

The SetClassLongPtr function replaces the specified value at the specified offset in the extra class memory or the WNDCLASSEX structure for the class to which the specified window belongs.

This function supersedes the SetClassLong function. To write code that is compatible with both 32-bit and 64-bit Windows, use SetClassLongPtr .

SetClassWord

The SetClassWord function replaces the 16-bit (WORD ) value at the specified offset into the extra class memory for the window class to which the specified window belongs.

Note   This function is provided only for compatibility with 16-bit versions of Windows. Applications should use the SetClassLong function.
SetWindowLong

The SetWindowLong function changes an attribute of the specified window. The function also sets the 32-bit (long) value at the specified offset into the extra window memory.

Note   This function has been superseded by the SetWindowLongPtr function. To write code that is compatible with both 32-bit and 64-bit versions of Windows, use the SetWindowLongPtr function.
SetWindowLongPtr

The SetWindowLongPtr function changes an attribute of the specified window. The function also sets a value at the specified offset in the extra window memory.

This function supersedes the SetWindowLong function. To write code that is compatible with both 32-bit and 64-bit versions of Windows, use SetWindowLongPtr .

UnregisterClass

The UnregisterClass function unregisters a window class, freeing the memory required for the class.

Structures

WNDCLASS

The WNDCLASS structure contains the window class attributes that are registered by the RegisterClass function.

This structure has been superseded by the WNDCLASSEX structure used with the RegisterClassEx function. You can still use WNDCLASS and RegisterClass if you do not need to set the small icon associated with the window class.

WNDCLASSEX

The WNDCLASSEX structure contains window class information. It is used with the RegisterClassEx and GetClassInfoEx

以下是一个改进版的 `DllMain` 函数示例,它实现了基本的安全性,包含初始化全局变量、创建同步对象等功能,同时避免了死锁和复杂操作: ```cpp #include <windows.h> // 全局变量 HANDLE g_hEvent; // 同步事件对象 int g_globalValue = 0; // 全局整型变量 BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: // 初始化全局变量 g_globalValue = 10; // 创建同步对象(事件) g_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); if (g_hEvent == NULL) { // 处理创建事件失败的情况 return FALSE; } // 避免在 DllMain 中进行复杂操作,不进行线程创建、同步线程等操作 break; case DLL_THREAD_ATTACH: // 线程附加时,这里不进行复杂操作 break; case DLL_THREAD_DETACH: // 线程分离时,这里不进行复杂操作 break; case DLL_PROCESS_DETACH: // 清理同步对象 if (g_hEvent != NULL) { CloseHandle(g_hEvent); g_hEvent = NULL; } break; } return TRUE; } ``` ### 代码解释 - **`DLL_PROCESS_ATTACH`**:当 DLL 被加载到进程地址空间时调用。在此处初始化全局变量 `g_globalValue`,并创建一个事件对象 `g_hEvent` 用于同步。如果事件对象创建失败,返回 `FALSE` 以表示初始化失败。 - **`DLL_THREAD_ATTACH`**:当新线程创建时调用。这里不执行复杂操作,避免死锁。 - **`DLL_THREAD_DETACH`**:当线程终止时调用。同样不执行复杂操作。 - **`DLL_PROCESS_DETACH`**:当 DLL 从进程地址空间卸载时调用。关闭之前创建的事件对象 `g_hEvent`,并将其置为 `NULL`。 ### 注意事项 - 避免在 `DllMain` 中进行复杂操作,如创建线程、同步线程、调用 `CoInitializeEx`、`LoadLibraryEx`、`CreateProcess` 等,这些操作可能会导致死锁。 - 不要在 `DllMain` 中进行长时间运行的操作,以免阻塞其他线程。 - 确保在 `DLL_PROCESS_DETACH` 中清理所有在 `DLL_PROCESS_ATTACH` 中创建的资源。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值