Window procedure functions-windowproc and defwindowproc Functions

Source: Internet
Author: User
In Windows In the operating system, when the window is displayed, it can receive a steady stream of messages sent by the system, and then the window needs to process these messages, so a function is required to process these messages. In API Is defined as a callback function. When the system needs to send a message to the window, it will call the callback function provided by the window. Windowproc , If Windowproc If the function does not process the message, it can be switched Defwindowproc This is the system's default message processing function. When you press the menu or click a window, the window needs to run the message processing function. Function Windowproc The statement is as follows:

Title Lresult callback windowproc (hwnd, Uint umsg, Wparam, Lparam );

HwndIs the handle of the current window.UmsgIs a message sent by the system.WparamIs the message parameter.LparamIs the message parameter. This function must be a static function, that is, a global function. The address has been determined during compilation. Because it needs to be set in the registered window type, as follows:

Title #008 atom myregisterclass (hinstance) #009 { #010 wndclassex wcex; #011 #012 wcex. cbsize = sizeof (wndclassex ); #013 #014 wcex. Style = cs_hredraw | cs_vredraw; #015 wcex. lpfnwndproc = wndproc;

The15The row is the message processing function of the window. FunctionDefwindowprocThe statement is as follows:Lresult defwindowproc (hwnd,Uint MSG,Wparam,Lparam);This function parameter is the same as the above function.However, it processes all default messages. Examples of calling these two functions are as follows:

Title #001 // #002 //Function: Wndproc (hwnd, uint, wparam, lparam) #003 // #004 //Purpose:Process messages in the Main Window. #005 // #006 //Cai junsheng2007/07/12 #007 // #008 lresult callback wndproc (hwnd, uint message, wparam, lparam) #009 { #010 int wmid, wmevent; #011 paintstruct pS; #012 HDC; #013 #014 switch (Message) #015 { #016Case wm_command: #017 wmid = loword (wparam ); #018 wmevent = hiword (wparam ); #019//Menu option command response: #020 switch (wmid) #021 { #022 case idm_about: #023 dialogbox (hinst, makeintresource (idd_aboutbox), hwnd, about ); #024 break; #025 case idm_exit: #026 destroywindow (hwnd ); #027 break; #028 default: #029 return defwindowproc (hwnd, message, wparam, lparam ); #030} #031 break; #032 case wm_paint: #033 HDC = beginpaint (hwnd, & PS ); #034 // #035 endpaint (hwnd, & PS ); #036 break; #037 case wm_destroy: #038 postquitmessage (0 ); #039 break; // Here I add a message wm_notify to notify the parent window of a control: a message is generated on this control.. Case wm_notify:
{ // After we receive the message here, we only know that the message has occurred in the control in the window, but we do not know what the message is, so we need to further judge, then perform the processing separately (sometimes the control needs to be determined)
Nm_htmlview * pnmhtmlview = (nm_htmlview *) lparam;
Switch (pnmhtmlview-> HDR. Code)
{
Case nm_hotspot: // click the link
//// Do some thing ....
Break;
Case nm_titlechange: // The HTML title has changed.
//// Do some thing ....
Break;
.
.
.
}
} #040 default: #041 return defwindowproc (hwnd, message, wparam, lparam ); #042} #043 return 0; #044}

The 8 Row-defined message processing functions The 14 The row starts to process different messages. The 29 Row and number 41 All rows are called. Defwindowproc Function to process unprocessed messages. With the window message processing function, you can respond to different messages and implement various functions. Certificate ---------------------------------------------------------------------------------------------------------------------------------------------------------------- A deeper understanding of window functionsArticle)

Think window procedure
Author: Wen Yu

1. When programming with Win32 API, window procedure is obvious, that isProgramUser-Defined window procedure, but Win32 provides an API function defwindowproc (). The default processing is handed over to it.

Int apientry winmain (hinstance, hinstance hprevinstance, lpstr lpcmdline, int ncmdshow) {wndclassex wcex; wcex. lpszclassname = "myclass"; wcex. lpfnwndproc = (wndproc) mywndproc ;... registerclassex (& wcex); hwnd = createwindow ("myclass", sztitle, ws_overlappedwindow, cw_usedefault, 0, cw_usedefault, 0, null, null, hinstance, null ); if (! Hwnd) return false; showwindow (hwnd, ncmdshow); updatewindow (hwnd); While (getmessage (& MSG, null, 0, 0) {translatemessage (& MSG ); dispatchmessage (& MSG);} return MSG. wparam;} lresult callback mywndproc (hwnd, uint message, wparam, lparam) {Switch (Message ){... default: Return defwindowproc (hwnd, message, wparam, lparam);} return 0 ;}

2. Using MFC and window procedure will be more complicated. Let's take a look at the static ones first, which are the classes pre-registered by MFC. In a word, MFC has prepared window procedure for you.

2.1 most Abstract: MFC encapsulates window procedure. Programmers only need to "programming by difference" to create a response function for the message you are interested in. (Of course, there are also virtual functions override ...)

Void cmyclass: onlbuttondown (uint nflags, cpoint pt ){...}

2.2 To the bottom layer, we can say that cwnd: windowproc () is the current window procedure, which is a template method, and is sent by "programming by difference" by you, it will be handed over to cwnd: onwndmsg () for processing. By default, it will be handed over to cwnd: defwindowproc () for processing. Of course, the polymorphism is not considered. In fact, cwnd: onwndmsg () and cwnd: defwindowproc () are both virtual functions. We also noticed that: defwindowproc () is called in cwnd: defwindowproc (), that is, defwindowproc () of Win32 API ().

Class cwnd: Public c0000target {... protected: // for processing Windows messagesvirtual lresult windowproc (uint message, wparam, lparam); Virtual bool onwndmsg (uint message, wparam, lparam, lresult * presult );...}; /// template methodlresult cwnd: windowproc (uint message, wparam, lparam) {lresult = 0; If (! Onwndmsg (message, wparam, lparam, & lresult) lresult = defwindowproc (message, wparam, lparam); Return lresult;} // primitive methodlresult cwnd: defwindowproc (uint nmsg, wparam, lparam) {If (m_pfnsuper! = NULL) Return: callwindowproc (m_pfnsuper, m_hwnd, nmsg, wparam, lparam );...}

2.3 to the underlying layer, let's take a look at the classes pre-registered by MFC, who is window procedure. Note that pre-registers window classes is nothing mysterious, because window classes is a struct, and when you want to use a pre-registers window classes, it is nothing more than passing a parameter, when a program is judged, assign values to the WC structure and call afxregisterclass (& WC) and OK. Haha, I can see that it uses Win32 API: defwindowproc ().

Bool cwnd: createex (DWORD dwexstyle, maid, DWORD dwstyle, int X, int y, int nwidth, int nheight, hwnd hwndparent, hmenu handle, lpvoid lpparam) {createstruct Cs; CS. lpszclass = lpszclassname ;... precreatewindow (CS); // ######## pass a CS with lpszclass null ...} bool cwnd: precreatewindow (createstruct & CS) // ######### pass a CS with lpszclass null {If (CS. lpszclass = NULL) // ######## pass a CS with lpszclass null {// make sure the default window class is registeredverify (afxdeferregisterclass (afx_wnd_reg )); // ######### pass a para afx_wnd_reg // No wndclass provided-use child window defaultassert (CS. style & ws_child); CS. lpszclass = _ afxwnd;} return true ;}# define afxdeferregisterclass (fclass) afxenddeferregisterclass (fclass) bool afxapi afxenddeferregisterclass (long ftoregister) // ######### pass a para afx_wnd_reg {... // common initializationwndclass wndcls; memset (& wndcls, 0, sizeof (wndclass); wndcls. lpfnwndproc = defwindowproc; // ########### here, Win32 API: defwindowproc () wndcls. hinstance = AfxGetInstanceHandle (); wndcls. hcursor = afxdata. hcurarrow ;... if (ftoregister & afx_wnd_reg) // ######### pass a para afx_wnd_reg {wndcls. style = cs_dblclks | cs_hredraw | cs_vredraw; wndcls. lpszclassname = _ afxwnd; // ######## pass a para _ afxwndafxregisterclass (& wndcls );...}...} const tchar _ afxwnd [] = afx_wnd; # define afx_wnd afx_wndclass ("WND") # define afx_wndclass (s) \ _ T ("afx") _ T (s) _ T ("42") _ static_suffix _ unicode_suffix _ debug_suffix

summary.

Class cwnd: Public c0000target {... protected: Virtual lresult windowproc (uint message, wparam, lparam); Virtual bool onwndmsg (uint message, wparam, lparam, lresult * presult ); virtual lresult defwindowproc (uint message, wparam, lparam); Virtual bool oncommand (wparam, lparam); Virtual bool onnotify (wparam, lparam, lresult * presult );...} l Result terminate (hwnd, uint nmsg, wparam, lparam) {cwnd * pwnd = cwnd: fromhandlepermanent (hwnd); Return terminate (pwnd, hwnd, nmsg, wparam, lparam);} lresult evaluate (cwnd * pwnd, hwnd, uint nmsg, wparam = 0, lparam = 0) {lresult; lresult = pwnd-> windowproc (nmsg, wparam, lparam); Return lresult;} lresult cwnd: windowproc (uint message, Wparam, lparam) {lresult = 0; If (! Onwndmsg (message, wparam, lparam, & lresult) lresult = defwindowproc (message, wparam, lparam); Return lresult;} bool cwnd: onwndmsg (uint message, wparam, lparam, lresult * presult) {If (Message = wm_command) oncommand (wparam, lparam); else if (Message = wm_policy) onnotify (wparam, lparam, & lresult ); else... // MSG map related} lresult cwnd: defwindowproc (uint nmsg, wparam, lparam LPA Ram) {If (m_pfnsuper! = NULL) Return: callwindowproc (m_pfnsuper, m_hwnd, nmsg, wparam, lparam );}

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.