Create a Windows class

Source: Internet
Author: User

Windows, controls, and dialog boxes are virtually windows, and the Windows class is a type of window description that Windows can handle. There are a number of predefined Windows classes in Windows, but you can also easily create your own windows classes. For each application, you need to create at least one Windows class. Takes the Windows class as a template, generates a window, and processes the message.

There are two kinds of data structures that you can use to save Windows class messages: Wndclass and Wndclassex. WNDCLASS, an older data structure that is no longer in use, is now using the new extended version of Wndclassex.

The structure of the wndclassex is: typedef struct TAGWNDCLASSEXW {    UINT        cbsize;/    /structure size//Win 3.x */    UINT        style; Style tag    WNDPROC     lpfnwndproc;//Pointer to the event handler    int         cbclsextra;//additional class information    int         cbwndextra;// Additional window information    hinstance   hinstance;//Application instance    hicon       hicon;//main icon    hcursor     hcursor;//Mouse image    hbrush      hbrbackground;//for drawing a window background brush    lpcwstr     lpszmenuname;//menu name    lpcwstr     lpszclassname;//class name/    * Win 4.0 */    hicon       hiconsm;//small icon handle} WNDCLASSEXW

Create one such data structure and assign values to each of its fields.
Wndclassex Wcex;
The first field Cbszie, the size of the structure wndclassex itself, is used by other functions instead of being known to them. Set to
wcex.cbsize = sizeof (wndclassex);
Style tag style, which describes the general properties of the window. The combination of attributes still uses or is | To be combined.
Cs_bytealignclient: Position the user area of a window on a byte boundary (in the X direction)
Cs_bytealignwindow: Position the window on a byte boundary (in the X direction)
CS_CLASSDC: All window instances of the window class share a window class DC
Cs_dblclks: Allows a message to be sent to the window by double-clicking the mouse button
Cs_globalclass: When you call the CreateWindow or CreateWindowEx function to create a window, the HINSTANCE parameter is allowed and the RegisterClass parameter passed to hinstance when registering the window class is different. If this style is not specified, the two hinstance must be the same.
Cs_hredraw: Redraw the entire window when the horizontal length changes or moves the window
Cs_noclose: Disable the System menu off option
CS_OWNDC: Give each window instance its own DC. Note that although this is convenient, it must be used cautiously, since each DC accounts for approximately 800 bytes of memory.
CS_PARENTDC: Sets the clipping region of the child window to the DC of the parent window, so that the window can draw itself on the parent window. Note whether this is a child window or a DC from the system cache instead of the parent window. Use this style to improve system performance.
Cs_savebits: Saves the portion of the screen that is obscured by the window as a bitmap, so that when the window is moved, the system can use the saved bitmap to restore the corresponding portion of the screen movement so that the system does not send the WM_PAINT message to the window that is obscured by the window. This feature is appropriate for a window of the menu type because it is usually a short display and then disappears. Setting this attribute increases the time that the window is displayed because it typically allocates the memory that holds the bitmap first.
Cs_vredraw: Redraw the entire window when the vertical length changes or moves the window
Here's a discussion of CS_OWNDC and CS_CLASSDC:


First, in the absence of these two properties, when calling GetDC or BeginPaint, Windows creates a Device description table with default values, and all changes to the device Description table properties are invalidated after the use of ReleaseDC or EndPaint. If you do not want to use the default properties, you can use CS_OWNDC and CS_CLASSDC to define the device description table properties yourself.
With CS_OWNDC, each window of this window class is created with a device description table that persists until the window is deleted. It is only necessary to initialize the device description table (which can be done during the WM_CREATE message), which can certainly be changed when needed, and the device description table that is returned each time the GETDC and BeginPaint functions are called with the device description table you just set. CS_OWNDC style only affects the device description table obtained by GETDC and BeginPaint, and does not affect other functions (such as GETWINDOWDC) to obtain the device description table.
Instead of using CS_CLASSDC with CS_OWNDC, the window class uses this style only to create a device description table for all windows, all of which share this device description table (and are mutually exclusive).


Then there is the parameter Lpfnwndproc, which is a function pointer to the event handler, and the function in the SDK is generally a callback function. callback function, __stdcall by typedef as CALLBACK, meaning callback. The basic characteristic of a callback is that the function pointer is called as the parameter of the calling function, and the stack is cleared by the called function using __stdcall. The called function of the callback function only passes an interface, and the calling function implements the called function and sets the invocation method.


The next two fields, Cbclsextra and Cbwndextra, were originally designed to instruct Windows to preserve some extra space in the Windows class to save run-time information. However, these two fields are not currently used, and are set to 0.


The next field is HInstance, which is the instance handle of the process that is passed to the WinMain () function when the program is driven, that is, the hinstance that is generated when the system starts the program.
Hicon is the icon handle for the application. Use the function LoadIcon () to load the system icon.
Hcursor, which is the cursor handle. Use LoadCursor to load.
Hbrbackground when a window is redrawn or refreshed, Windows redraws the background of the client area of the window with at least predefined colors, that is, the brushed brush. Paint brushes, brushes,, colors, graphics as part of GDI. Generally use the function getstockobject () to get some brushes of the system. Another way of writing is direct (Hbrush) (color_window+1).
The next field is Ipszmenuname, which is a null-terminated string that contains the name of the menu resource loaded into the window.
The name of the Windows class is saved in Ipszclassname, and the system needs to be tracked to recognize these classes, so there is a name. Typically, a null-terminated string.
The last one for the small icon, displayed on the Win7 taskbar icon.
This completely defines a Windows class.

Create a Windows class

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.