One-day Windows API training (9) windowproc and defwindowproc Functions
Source: Internet
Author: User
In Windows, when the window is displayed, it can receive a steady stream of messages from the system, and then the window needs to process these messages, therefore, a function is required to process these messages. The API defines a function as a callback function. When the system needs to send a message to the window, it calls the callback function windowproc provided by the window. If the windowproc function does not process the message, you can turn it to the defwindowproc function for processing. This is the default message processing function of the system. When you press the menu or click a window, the window needs to run the message processing function.
The windowproc function declaration is as follows:
Lresult callback windowproc (hwnd,
Uint umsg,
Wparam,
Lparam
);
Hwnd is the handle of the current window.
Umsg is a message sent by the system.
Wparam is a message parameter.
Lparam is a 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:
#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;
Row 15th is the message processing function of the window.
The defwindowproc function declaration 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:
#001 //
#002 // function: wndproc (hwnd, uint, wparam, lparam)
#003 //
#004 // purpose: to process messages in the main window.
#005 //
#006 // Cai junsheng 2007/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 {
#016 case 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;
#040 default:
#041 return defwindowproc (hwnd, message, wparam, lparam );
#042}
#043 return 0;
#044}
8th rows define message processing functions
The first line starts Processing Based on different messages.
Both lines 29th and 41st call the defwindowproc function to process unprocessed messages.
With the window message processing function, you can respond to different messages and implement various functions.
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.