窗口类
这篇文章描述了各种类型的窗口类,他们在系统中的位置以及属于窗口类的用来定义窗口默认行为的各种元素。
窗口类是一系列窗口属性的集合。系统把窗口类当作生成窗口的模板。每一个窗口都是窗口类的成员。所有的窗口类是和进程相关的。
概述:
1. 关于窗口类
每一个窗口对应一个窗口过程,而该窗口过程是被所有使用这个窗口类的窗口所共享的。每一个进程在要创建窗口之前,必须要先注册改窗口所属的窗口类。注册窗口类就是将窗口过程,窗口风格以及其他窗口属性用一个类名相关连起来。当进程在CreateWindow, CreateWindowEx中使用窗口类名时,所创建的窗口属性就和窗口类中的各属性相联系了。
窗口类的类型有3种:系统的,应用程序全局的,应用程序局部的。
系统的窗口类是系统创建的。因此你的进程不能销毁它们。Win95,Win98和WinMe中的系统窗口类是在系统启动时就建立的。2000,NT,XP是在进程第一次调用GDI或者USER中的函数时建立的。每一个应用程序都拥有一份系统窗口类的拷贝。系统中总可以在我们的进程中使用的窗口类有:
Class Description
Button The class for a button.
ComboBox The class for a combo box.
Edit The class for an edit control.
ListBox The class for a list box.
MDIClient The class for an MDI client window.
ScrollBar The class for a scroll bar.
Static The class for a static control.
以下是只用系统能使用的系统窗口类:
Class Description
ComboLBox The class for the list box contained in a combo box.
DDEMLEvent Windows NT/Windows 2000/Windows XP: The class for Dynamic Data Exchange Management Library (DDEML) events.
Message Windows 2000/Windows XP: The class for a message-only window.
#32768 The class for a menu.
#32769 The class for the desktop window.
#32770 The class for a dialog box.
#32771 The class for the task switch window.
#32772 Windows NT/Windows 2000/Windows XP: The class for icon titles.
应用程序全局窗口类是由可执行程序或者动态链接库所建立的。它可以被进程中的其他模块所共享。比如,你的DLL通过调用RegisterClassEx注册了一个应用程序全局窗口类的自定义的控件。那么凡是将这个DLL映射到自己进程空间的进程都可以创建这个窗口制定的控件。为了能创建这样一个窗口类系统就在注册表中把该DLL的名字添加到AppInit_Dlls中:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Windows。当然,用UnregisterClass就可以卸掉这样的窗口类。
应用程序局部窗口类是在模块局部使用的。当一个模块关闭的时候,该窗口类也就被注销了。当让也可以使用UnregisterClass来释放她所占有的空间。
系统是如何定位窗口类的呢?
系统为上述三中窗口类维护一个结构列表。当使用CreateWindow,CreateWindowEx时,系统使用下列步骤来定位窗口类:
a. 在当前模块空间内搜索应用程序局部窗口类列表;
b. 搜索应用程序全局窗口类列表;
c. 搜索系统窗口类列表;
应用程序可以创建和高一级重名的窗口类,但决不会改写高一级的窗口类信息。具有局部隐藏的性质。
注册窗口类
                窗口类定义了诸如风格,图标,光标,菜单以及窗口过程等属性.注册窗口类的第一步是要填充一个WNDCLASSEX的结构.然后,把这个结构通过RegisterClassEx进行注册。在注册应用程序全局窗口类的时候,要在WNDCLASSEX的style成员中添加CS_GLOBALCLASS属性。局部窗口类就不用添加这一属性。
  注册窗口类的可执行模块或动态链接库是该窗口类的所有者。这一信息系统是从WNDCLASS的hInstance成员中得出的。
  95/98/ME 当窗口类的所有者被关闭或卸载时,窗口类就被销毁了。因而,在窗口类所有者进程被销毁之前,就应该销毁所有使用该窗口类的窗口。
  NT/2000/XP;当DLL被卸载时,窗口类不会被销毁。因此,当系统调用窗口的窗口函数时,就会发生违规访问,因为窗口过程已经不再内存中了。所以,在DLL卸下之前,要销毁所有的窗口,并调用UnregisterClass。
窗口类的元素
窗口类的各个元素定义了使用该窗口类的窗口的行为和风格。初测窗口类的应用程序要在WNDCLASSEX结构中填充各成员,然后使用RegisterClassEx函数来注册。GetClassInfoEx和GetClassLong函数能够返回该窗口的窗口类的信息。SetClassLong函数可以改变已经注册的全局和局部窗口类的属性。
Element Purpose
Class Name Distinguishes the class from other registered classes.
Window Procedure Address Pointer to the function that processes all messages sent to windows in the class and defines the behavior of the window.
Instance Handle Identifies the application or .dll that registered the class.
Class Cursor Defines the mouse cursor that the system displays for a window of the class.
Class Icons Defines the large icon and the small icon (Windows 95/98/Me, Windows NT 4.0 and later).
Class Background Brush Defines the color and pattern that fill the client area when the window is opened or painted.
Class Menu Specifies the default menu for windows that do not explicitly define a menu.
Class Styles Defines how to update the window after moving or resizing it, how to process double-clicks of the mouse, how to allocate space for the device context, and other aspects of the window.
Extra Class Memory Specifies the amount of extra memory, in bytes, that the system should reserve for the class. All windows in the class share the extra memory and can use it for any application-defined purpose. The system initializes this memory to zero.
Extra Window Memory Specifies the amount of extra memory, in bytes, that the system should reserve for each window belonging to the class. The extra memory can be used for any application-defined purpose. The system initializes this memory to zero.
Extra class memory:在系统内部,为每一窗口类维护一个WNDCLASSEX结构。在应用程序注册窗口类的时候,系统就在WNDCLASSEX结构的后面又添加了一定数量的字节空间(extra class memory)。这个空间是使用该窗口类的所用窗口所共享的。这部分空间是从系统的局部堆中分配的。当申请的这个空间超过40字节时,RegisterClassEx就会失败。否则,就必须在应用程序自己的堆中分配相应的空间,并把指针交给extra class memory。SetClassWord和SetClassLong函数可以向extra class memory中拷贝值。
Extral window memory:在系统内部,为每一个窗口都维护一个窗口结构。在注册后,系统就会在该结构的后面又添加一定字节的存储空间(extra window memory)。同样是从系统的局部堆中分配的。这些数据是和指定窗口相关的。SetWindowLong。
2.(尚未译完) 使用窗口类
函数:
结构: