I. Basic Concepts
1,WindowsProgramThe essence is message-based event-driven.
WindowsPrograms are divided into programsCodeAndUIResources ., The actual content of the resource is binary code, which is generated using various tools.
In 32 Bit operating systems do not have Small/medium/large Memory mode. Libc. Lib Yes C Runtime Static link version of the function library, Msvcrt. Lib Yes C Runtime Function library dynamic link version Import Function library.
Windows messages are divided into two types (by input): messages generated by hardware devices (such as mouse clicks ), put in system queue (system queue) , and Windows system or other Windows messages sent by the program, put in the program queue (Application Queue) medium. Call getmessage API to get a message, program life is driven by it. The main character of receiving and processing a message is the window. Each window should have a window function to process the message.
2Prototype of message ing
Design the content of window functions in a more modular and general way:
First, defineMsgmap_entryStructure andDimMACRO:
Struct msgmap_entry {
Uint nmessage;
Long (* PFn) (hwnd, uint, wparam, lparam );
};
# Define dim (x) (sizeof (X)/sizeof (X [0])
Note:: PFNIs a function pointer. I want to process the function referred to by this pointer.NmessageMessage. This is a specific implementation of encapsulating "data" and "Data Processing Methods" in the object-oriented concept, but it is notC ++Language .]
Next, organize two Arrays_ Messageentries []And_ Commandentries []To establish the relevance of the messages to be processed in the program and the message processing routines:
Definition example
// Message and processing routine comparison table
Struct msgmap_entry _ messageentries [] =
{
Wm_create, oncreate,
Wm_paint, onpaint,
Wm_size, onsize,
Wm_command, oncommand,
Wm_setfocus, onsetfocus,
Wm_close, onclose,
Wm_destroy, ondestroy,
};
This is a message. This is a message processing routine.
// Command-ID comparison table with processing routine
Struct msgmap_entry _ commandentries =
{
Idm_about, onabout,
Idm_fileopen, onfileopen,
Idm_saveas, onsaveas,
};
This is the wm_command command item. This is a command processing routine.
Therefore, window functions can be designed as follows:
Window Function Design Example
//----------------------------------------------------------------------
// Window functions
//----------------------------------------------------------------------
Lresult callback wndproc (hwnd, uint message,
Wparam, lparam)
{
Int I;
For (I = 0; I <dim (_ messageentries); I ++ ){//
If (Message = _ messageentries [I]. nmessage)
Return (* _ messageentries [I]. PFn) (hwnd, message, wparam, lparam ));
}
Return (defwindowproc (hwnd, message, wparam, lparam ));
}
//----------------------------------------------------------------------
// Oncommand-specially handles wm_command
//----------------------------------------------------------------------
Long oncommand (hwnd, uint message,
Wparam, lparam)
{
Int I;
For (I = 0; I <dim (_ commandentries); I ++) {// command project comparison table
If (loword (wparam) ==_ commandentries [I]. nmessage)
Return (* _ commandentries [I]. PFn) (hwnd, message, wparam, lparam ));
}
Return (defwindowproc (hwnd, message, wparam, lparam ));
}
//----------------------------------------------------------------------
Long oncreate (hwnd, uint wmsg, uint wparam, long lparam)
{
...
}
//----------------------------------------------------------------------
Long onabout (hwnd, uint wmsg, uint wparam, long lparam)
{
...
}
//----------------------------------------------------------------------
This wayWndprocAndOncommandNo need to change. Every time there is a new message to be processed_ Messageentries []And_ Commandentries []Add new elements to the two arrays and write new processing routines for the new messages.
This concept and practice isMFCOfMessage Map.MFCPackage the actions in them better and more refined (and of course more complicated) to form a huge map of messages. Once a program obtains a message, it can be traced back to the graph, until it is processed.
3, Dialog box running
Reference
[1]In-depth IntroductionMFC
[2]Http://msdn.microsoft.com/en-us/library/0x0cx6b1%28VS.80%29.aspx