Overview of window classes (Windows Class)

Source: Internet
Author: User

Windows Programming (Win32 in general) has several core concepts: entry function WinMain, window class, window procedure, message processing mechanism, general control. This article mainly introduces the related concepts of window class, including:

    • The type of the window class;
    • Registration and use of window classes;
    • The composition of the window class.

The window class is process-based, and each application must register the window class (or use the Windows class defined by the operating system) before creating the window, which needs to be destroyed after the use is complete (anti-registration).

The main purpose of the introduction window class is to clarify the related concepts of Windows window programming and to master the mechanism of GUI processing inside windows. If you are using MFC or other interface framework, this article is not necessary to read.

First, the type of window class

There are three types of Windows under window:

    • System window Class
    • Application Global Window class
    • Application Local window class

The main difference between the three is the scope, the time and mode of registration destruction.

1. system window Class

As the name implies, the System window class is registered with the operating system. Some system window classes are accessible to all processes, and some system window classes can only be used internally by the operating system. For System window classes, the application cannot be destroyed.

The operating system registers the system window class for the current process when the application first calls the GUI function. This means that each individual application is used for the same system window class registration. The following table shows the name of the system window class that can be used by any process.

window class Description Information
Button The window class name of the button.
ComboBox The window class name of the combo box.
Edit The window class name of the edit box control.
ListBox The name of the window class for the list box.
Mdiclient The window class name of the MDI child window.
ScrollBar The window class name of the scroll bar.
Static The window class name of the static control.

The following table is the name of the window class that is used only by the operating system.

window class Description Information
Combolbox

The window class name of the combo list box.

The class for the list box contained in a combo box.

Ddemlevent

The window class name for the Dynamic Data Exchange Management Library (DDEML) event.

The class for Dynamic Data Exchange Management Library (DDEML) events.

Message The class for a message-only window.
#32768 The window class name of the menu.
#32769 The window class name of the desktop window.
#32770 The window class name for the dialog box.
#32771 The window class name of the taskbar switch window.
#32772 The name of the window class for the icon title bar. The class for icon titles.
2. Application Global Window class

The Application Global window class refers to a window class that is registered by an executable program or DLL and can be used by other modules of the current process. For example, if you register a global window class in a DLL, the application can use the corresponding window class by loading the DLL.

The global window class must be destroyed by the user itself (using unregisterclass) when not in use.

3. Application local window class

The application local window class refers to the window class that is registered by an executable program or DLL and is used only for the current module. We can register many local window classes, but the recommended practice is to register only one window class for creating the main application window.

The operating system automatically destroys the local window class when the application exits, and we can also manually destroy the local window class through the unregisterclass function.

Second, the registration and use of window class

The Window class defines window properties under Windows, including window styles, icons, cursors, menu items, window procedures, and so on. To register a window class, you first need to populate the wndclass ,wndclassex structure, and then call registerclass, RegisterClassEx, using the set parameters. functions (The main difference is that the registerclass function does not support small icon settings, which can be used uniformly in common use registerclassex functions).

If you need to register the application Global window class, you need to set the wndclassex structure's style property to Cs_globalclass; Register the local window class do not specify the Cs_globalclass property.

The window class is primarily used for calls to CreateWindow,CreateWindowEx functions (the first parameter, Lpclassname), and is prototyped as follows:

HWND WINAPI CreateWindow (_in_opt_ lpctstr lpclassname, _in_opt_ lpctstr lpwindowname, _in_ DWORD dwstyle, _in_intx, _in_inty, _in_intnwidth, _in_intnheight, _in_opt_ HWND hwndparent, _in_opt_ HMENU HMENU, _in_opt_ hinstance hinstance, _in_opt_ lpvoid Lppara m);  HWND WINAPI CreateWindowEx (_in_ DWORD dwexstyle, _in_opt_ lpctstr lpclassname, _in_opt_ lpctstr lpwindowname, _in_ DWORD dwstyle, _in_intx, _in_inty, _in_intnwidth, _in_intnheight, _in_opt_ HWND hwndparent, _in_opt_ HMENU HMENU, _in_opt_ hinstance hinstance, _in_opt_ lpvoid Lppara m);

The window class is matched by a string of lookups.

1. How the system looks for window classes

The operating system maintains a list of window classes categorized by three types, searching for the location window class in the following order when the application needs to create a window:

    • Finds the application Local window class list using the window class name and the current module instance handle (note that the current module instance handle is primarily designed to differentiate between different modules registered with the same name window class);
    • If not found in the local window class list, continue to find the Application Global window class list;
    • If not found in the Global Window class list, the System window class list is found.

All window creation will follow the above lookup order. Thus this also provides a way to rewrite the System window class, registering in the application and the System window class with the same name as the local window class, so that you can modify the current application to replace some system windows, without affecting the other application's system window class use.

2. Attribution of the window class

The typical meaning of a window class is that it belongs to an executable program or DLL that registers the class. The operating system uses the hinstance of the wndclassex structure that invokes the RegisterClassEx function to determine ownership of the window class. This means that the DLL must use a handle to the DLL itself when registering the window class.

Windows that use a DLL to register a window class may also exist when a dynamically loaded DLL is unloaded. This requires the caller to ensure that all Windows referencing the window class registered by the DLL are closed before the DLL is unloaded, and the window class is destroyed using the unregisterclass function. Otherwise, there may be a situation where access is out of bounds. (The procedure function address is invalid because the DLL was unloaded.) )

Third, the composition of the window class

The window class gives some properties of the Windows window that uses the class. The primary parameter settings are located in the wndclassex structure. It is defined as follows:

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     *pwndclassex;

In practice, the system only requires three parameters for the class name (lpszClassName), callback function address (LPFNWNDPROC), instance handle (hinstance), and other parameters for setting the window properties. The following describes the element composition of the window class:

Class name (field: lpszClassName)

Used to uniquely identify a window class. The window class is process-dependent and must be used to ensure that the class name of the window class is unique in the current process.

In addition, because the class name occupies the system private meta table (Systems ' private atom table), please ensure that the class name is as short as possible when registering.

You can use the getclassname function to get the window class name for the current window.

Window procedure address (field: Lpfnwndproc)

The system uses this address to callback all Windows messages. See the Reference window procedures for details. The prototype must conform to the following definition:

LRESULT CALLBACK WindowProc (  _in_  hwnd hwnd,  _in_  UINT umsg,  _in_  WPARAM WPARAM,  _in_  LPARAM LPARAM);
Instance Handle (field: hinstance

The window class needs to use an instance handle to identify the executable program or DLL to which it belongs. The system sets an instance handle to an executable program or DLL when it is started, and is used to manage each module, which can be obtained in the entry function of the executing module (for example, in WinMain or DllMain).

Class cursor (field: hcursor)

Used to specify the style that the mouse displays in the customer area. You can use the loadcursor function to load a cursor file to set the field.

You can use the setcursor function to set the cursor properties. Additional details can be found in cursors.

Class icons (fields: Hicon and HICONSM

The class icon is a picture that identifies a particular window class. Divided into large icons and small icons. Large icons are used for window switching (ALT + TAB) and the taskbar and desktop browser (Explorer.exe) under Large icon view. Small icons are used to display the taskbar and desktop browser under the title bar, small icon view.

The actual size of the icon can be obtained through the getsystemmetrics function. Set fields Sm_cxicon and Sm_cyicon to get the long width of large icons, set fields Sm_cxsmicon and Sm_cysmicon to get the length and width of small icons.

Class background brush (field: Hbrbackground

A brush that sets the redraw of the client area of the window, with detailed settings to refer to the wm_erasebkgnd message. Use to create custom brushes, or use system brushes (getsyscolorbrush).

Class menu (field: HMenu)

Lets you set the system default menu. You can use the menu name or use makeintresource . Detailed settings are recommended for reference menus.

Window class style (field: style

Specify some of the default parameters that are created by the window class, which you can refer to in Windows class Styles.

Additional window class storage space (field: Cbclsextra

The only class storage that is shared by all windows, similar to the static member function of a class in C + +, is stored by default on Wndclassex , which must be set to 0 if not required.

Additional class storage is allocated in the local heap of the system, and the recommended field length is not too large (no more than 40 bytes). You can use the setclassword,setclasslong function to set the corresponding fields, using the getclassword,getclasslong function to get the corresponding parameters.

Many of the classic Win32 programming data may refer to this field, and most applications do not need to set this parameter at this time. It is also not recommended.

Additional window storage space (field: Cbwndextra

The concept is similar to the Attachment window class storage, except that the attachment window storage space is allocated per window, similar to the member variable in C + +, each instance has one. The Attachment window storage space is typically used to store window-related data.

You can use the setwindowlong and getwindowlong functions to get and set the data in the storage space of the attachment window.

Iv. Summary

The core of Windows programming is the message processing mechanism, and the window class as a separate abstract unit provides us with a way to register the window class and create it, some of which are worth learning from. Although the concept is older, if you want to know more about the internal processing of Win32, we need to deepen it on the basis of understanding.

There are many parameters to the Windows window class, and if there are some default parameters that cannot be confirmed, it is recommended that you review the corresponding content on MSDN, as described in general.

Overview of window classes (Windows Class)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.