Deep analysis of C + + WNDCLASS structure and its application in Windows _c language

Source: Internet
Author: User
Tags class definition data structures getmessage message queue

The

Wndclass is a system-supported structure that stores information for a particular type of window, such as Classstyle, message handlers, icon,cursor, background brush, and so on. In other words, CreateWindow only makes a form of a WNDCLASS definition an instance. To get the WNDCLASS data of a certain window, you can use Getclasslong ();
RegisterClass () is a form that registers a type on the system. Which is to register the WNDCLASS data you provide as a window class, The identity of the WNDCLASS is defined in Wndclass.lpszclassname, regardless of whether the window created by CreateWindow or CreateWindowEx must correspond to a wndclass, but a wndclass can have more than one window object.
There are system-predefined window classes, such as: classname=_t ("button" or "COMBOBOX" or "EDIT" or "LISTBOX" or "mdiclient" or "Scollbar" or "STATIC") , to use these forms, just use CreateWindow to create the corresponding object. To get a window class of a window, you can use GetClassName (); The callback function in the
WNDCLASS is the message handler function for the form: CALLBACK winproc (Messages Msg,lparam lparam,wparam WPARAM); The
Window class Property Definition
Structure WNDCLASS contains all of the information for a window class and is one of the basic data structures used in Windows programming, defined by the application defining a window class to determine the properties of a window, as follows:

typedef struct _WNDCLASS {
UINT style;
WNDPROC Lpfnwndproc;
int Cbclsextra;
int Cbwndextra;
HINSTANCE hinstance;
Hicon Hicon;
Hcursor Hcursor;
Hbrush Hbrbackground;
LPCTSTR Lpszmenuname;
LPCTSTR lpszClassName;
} wndclass, *pwndclass;

Give an example to explain
Example:

Long CALLBACK WndProc (hwnd,uint,wparam,lparam)//Declaration//winmain function is the entry point for all Windows applications, similar to the main function in C language whose function is to complete//a series of definitions and initialization, and generates a message loop. Function Description: int WINAPI WinMain (hinstance hinstance,//handle to current instance hinstance,// Handle to previous instance LPSTR lpCmdLine,//command line int ncmdshow/
  /show state) {//initialization, initialization includes window class definition, registration, Create window instance and Display window four part HWND hwnd;
  MSG msg;
  Wndclass Wndclass; Char lpszclassname[]= "window"; window class name Char lpsztitle[]= "Test window"; Window title//window class definition, window class defines the form and function of the window, the window class definition by giving the window class data structure WNDCLASS assignment completion//The data structure contains the various properties of the window class Wndclass.style = 0; The window type is the default type Cs_ Class Style wndclass.lpfnwndproc=wndproc; Defines the window handler function wndclass.cbclsextra=0; The window class has no extended wndclass.cbwndextra=0; Window instance has no extended wndclass.hinstance=hinstance; The current instance handle Wndclass.hicon=loadicon (Null,idi_application); The minimized icon for the window is the default icon Wndclass.hcursor=loadcursor (Null,idc_arrow); The window takes the arrow cursor wndclass.hbrbackground= (hbrush) (Getstockobject (White_brusH)); The window background is white wndclass.lpszmenuname=null; window without menu wndclass.lpszclassname=lpszclassname;
  Window class name "window"//Below is the registration of the window class-----------Windows system itself provides some predefined window classes, programmers can also customize window classes, window classes must be registered before use. if (!
  RegisterClass (&wndclass))///If registration fails to issue a warning {messagebeep (0); return FALSE;}
                    Create a window to create an instance of a window by function CreateWindow () implement Hwnd=createwindow (lpszClassName,//window class name, create a window must be based on the window class name we have registered, that is, "window." Lpsztitle,///Window header name Ws_overlappedwindow,//window style Ws_ Windows style Cw_usedef
                     Ault,//the upper-left coordinate value of the window is the default cw_ Create Wndow cw_usedefault, Cw_usedefault,//Window's height and width are the default values
                    Cw_usedefault, NULL,//This window has no parent window null,//This window does not have a submenu
  HINSTANCE,//The current handle of the application creating this window is NULL//not using this value);
  Display window ShowWindow (hwnd,ncmdshow);
  Draw User Area UpdateWindow (HWND); The message loop while (GetMessage (&msg,null,0,0))//getmessage () function takes a message out of the calling thread's message queue, and for each application window thread, the operating system willIt creates a message queue, and when our window has a message (that is, all the messages associated with this window thread), the manipulation system puts the message into the thread's message queue, and our window program passes through the GetMessage ()
  function to take a specific message out of its own message queue and respond to it. {TranslateMessage (&msg)//conversion of "message pair", such as wm_keydown and wm_keyup message pairs of keyboard to WM_CHAR messages, and post converted new messages to our message queues.
  This conversion operation will not affect the original message, only a new message will be generated. DispatchMessage (&msg); the//dispatchmessage () function is the callback function that passes the message we take out to the window, and it can be understood that the function routes the fetched message to the operating system.
    The operating system then invokes our window callback function to process the message. return msg.wparam; The end of the message loop is when the program ends by returning the information to the system}//Window function, which defines the response of the application to the different messages received, including the process of the application's handling of various possible messages, the set of Message Processing branch control statements, long CALLBACK
WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM)
    {switch (message) {case Wm_destroy:postquitmessage (0);
  Default://defaults to the system message default processing function return DefWindowProc (Hwnd,message,wparam,lparam);
return (0);
 }

Note: The function pointer of the window callback function defines the typedef lresult CALLBACK (* WNDPROC) (HWND, UINT, WPARAM, LPARAM);

WNDPROC Oldwndproc;
LRESULT CALLBACK Newwndproc (HWND hwnd, UINT umsg, WPARAM WPARAM, LPARAM LPARAM) {
    switch (MSG)
    {
      ...
    } Return
    CallWindowProc (Oldwndproc,g_wnd,msg,wparam,lparam);
}
Oldwndproc = (WNDPROC) GetWindowLong (g_wnd,gwl_wndproc);
SetWindowLong (hwnd, GWL_WNDPROC, (LPARAM) (WNDPROC) newwndproc);

You can modify the callback function for the form class by calling the SetWindowLong function.

What is the difference between wparam and lparam in the CallBack function:
WPARAM WPARAM, defined as Word type
LPARAM LPARAM is defined as a long type
In Win 3.x, the wparam is 16-bit, and the lparam is 32-bit, there is a clear difference between the two.
In the Win32 API, WPARAM and LPARAM are 32-bit, so there is no essential difference.
But it is customary for us to use LPARAM to deliver addresses, while wparam pass other parameters.

 function MouseHookProc (ncode:integer; wpar:wparam; lpar:lparam): Lresult; stdcall;

If I want to determine whether the left mouse button is pressed, use Wparam==wm_lbuttondown to judge.
LParam is a type of pmousehookstruct (tagmousehookstruct pointer), mainly to get the Send window handle, mouse coordinates, and some other information.
LParam needs to be cast and converted to pmousehookstruct type when used.

Pmousehookstruct = ^tmousehookstruct; 
tagmousehookstruct = packed record 
 pt:tpoint; 
 Hwnd:hwnd; 
 Whittestcode:uint; 
 Dwextrainfo:dword; 
End; 
Tmousehookstruct = tagmousehookstruct; 

For example:
function Getmsgproc (ncode:integer; wpara:wparam; lpara:lparam) 
 : lresult; stdcall 
; var 
 hgetmsghook:hhook;  
 msg:tmsg; 
Begin 
 if (ncode >= 0) then 
 begin 
  Fillchar (pmsgdata^, Sizeof (Tmessagerecord), #0); 
  MSG: = tmsg (pointer (lpara) ^); 
 End; 
 Result: = CallNextHookEx (Hgetmsghook, Ncode, Wpara, Lpara); 
End 


Data structure prototype

typedef struct _wndclass{        UINT style;
           WNDPROC Lpfnwndproc;          int Cbclsextra;          int Cbwndextra;
           HANDLE hinstance;          Hicon Hicon;
           Hcursor Hcursor;
           Hbrush Hbrbackground;
           LPCTSTR lpszmenuname;          LPCTSTR lpszclassname;
} WNDCLASS;      

Structure description
The WNDCLASS structure contains the class attributes registered by the RegisterClass function
Component Introduction
Style: Specifies the style of the class. These styles can be grouped by bitwise OR action. The style is as follows:

    • cs_bytealignclient: Position the user area of the window on the byte boundary (in x direction)
    • Cs_bytealignwindow: Position the window on the byte boundary (in x direction)
    • CS_CLASSDC: All window instances of this window class share a window class DC
    • cs_dblclks: Allow messages to be sent to a window by double-clicking the mouse button
    • cs_globalclass: When calling Createwindo The W or CreateWindowEx function to create a window allows its hinstance parameters to be different from the HINSTANCE parameters passed to the RegisterClass when the window class is registered. If you do not specify this style, the two hinstance must be the same.
    • Cs_hredraw: When the horizontal length changes or moves the window, redraw the entire window
    • cs_noclose: Disable the System menu from closing option
    • CS_OWNDC: Give each window instance its own DC. Note that, while this is convenient, it must be used with caution because each DC is about 800 bytes of memory.
    • CS_PARENTDC: Sets the cropping area of the child window to the DC of the parent window, so that the window can draw itself on the parent window. Notice whether this is a child window or a DC from the system cache, rather than a DC using the parent window. Use this style to improve system performance.
    • cs_savebits: Saves the portion of the screen blocked by the window as a bitmap so that when the window is moved, the system can restore the corresponding portion of the screen movement with the saved bitmap so that the system does not send WM_PAINT messages to the window that is blocked by the window. This feature is appropriate for a menu-type window because it usually disappears after a short display. Setting this attribute increases the time that the window is displayed because it usually assigns the memory of the saved bitmap first.
    • Cs_vredraw: Redraw the entire window when the vertical length changes or moves the window
    • lpfnwndproc: Pointing to a window procedure

Cbclsextra: Specifies the number of bytes to allocate immediately after the WNDCLASS data structure. The system initializes it to zero.
Cbwndextra: Specifies the number of bytes to allocate immediately after the window instance, and the system initializes it to zero. If an application is registering a dialog box created with the class directive in the RC resource descriptor using the WNDCLASS structure, it must set the field Dlgwindowextra.
HInstance: The handle to the module instance that contains the window procedure that identifies the window class, and cannot be null.
Hicon: An icon that identifies the window class. The Hicon field must be a handle to an icon, and if the hicon is null, the application must draw an icon whenever the user shrinks the application to the minimum.
Hcursor: The cursor that identifies the class of the window, hcursor must be a handle to a cursor resource. If the Hcursor field is NULL, the application must explicitly set the cursor shape whenever the mouse is moved to an application window.
Hbrbackground: The background brush that identifies the window class. The Hbrbackground field must be a handle to the physical brush used to draw the background, or a color value. If you give a value for a color, it must be one of the standard system colors listed below (the system will add 1 to the selected color). If a color value is given, it must be a color converted to one of the following hbrush types:

    • color_activeborder
    • color_activecaption
    • color_appworkspace
    • color_backgr Ound
    • color_btnface
    • color_bthshadow
    • color_btntext
    • color_captiontext
    • color_graytext
    • color_highlight
    • color_highlighttext
    • color_inactiveborder
    • color_inactivecaption
    • color_menu
    • color_menutext
    • color_scrollbar
    • Color_window
    • Color_windowframe
    • Color_windowtext
    • when the Hbrbackground field is NULL, the application must draw its own background whenever the user area needs to be drawn. The application can determine when the background needs to be colored by processing WM_ERASEBKGND messages or by checking the ferase fields of the PAINTSTRUCT structure that are filled out by the BeginPaint function.

Lpszmenuname: A string that points to the end of a null that describes the resource name of the menu as it would appear in the resource file. If you use an integer identifier menu, you can use the Makeintresource macro. If Lpszmenuname is null,
then the window class's window will not have a default menu.
lpszClassName: A string that points to a null end, or a prototype (atom). If the parameter is a prototype, it must be a class prototype with a previous call to the RegisterClass or RegisterClassEx function. The class prototype must be a lpszclassname, and the high word must be 0. If lpszClassName is a string, it describes the window class name. The class name can be a name registered by registerclass or registerclassex, or any predefined control class name. The
structure information header is declared in Winuser.h, containing windows.h

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.