Today, I saw the first chapter of the MFC, I'm really writing.
No need to say, I have seen it in the past, I have seen me, I haven't figured it out.
Today, look at the other figure, True willow dark flowers!
Compile the diagram of the connection:
Message Send and Processing Graphical:
Connection calls for functions and resources:
Optimization of function code:
In fact, there is nothing to say, see the code is better than what text description
Here is the optimized code.
int CALLBACK WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {if (hPrevInstance!) if (InitApplication (hInstance)!) return (FALSE); if (InitInstance (hInstance, nCmdShow)!) return (FALSE); ...} // ------------------------------------------------------------------------------------------------------------------------------------ ----- Bool initApplication {WNDCLASS WC; ... RETURN (RegisterClass (& WC));} // -------------------- ----------------------------- Bool InitInstance (Hinstance Hinstance, Int ncmdshow) {_ hWnd = CREATEWINDOW (...) ;. ..}
Message Map of Zou Xing:
Is it possible to design the content of the window function more modular, more general? Here is a practice. caution,
The following practices are the Zou-shaped, the structure name and variable name used by the MFC "message mapping table".
They are the same as MFC.
First, define a MSGMap_ENTRY structure and a DIM huge set:
Struct msgmap_ENTRY {
Uint NMessage;
Long (* PFN) (HWND, UINT, WPARAM, LPARAM);
}
#DEFINE DIM (X) (SizeOf (x) / sizeof (x [0])))
Note that the second element PFN of MSGMap_ENTRY is a function pointer to let the letter referred to in this pointer.
Processing NMESSAGE message.
Next, organize two arrays _MessageEntries [] and _commandentries [], to deal with the program
Interest and message processing common network structure established:
// Message in the control form
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 the message, this is a message processing.
// Command-id on the control form
Struct msgmap_entry _commandentries =
{
IDM_ABOUT, ONWABOUT,
IDM_FILEOPEN, ONFILEOPEN,
IDM_SAVEAS, OONSAVEAS,
}; ↑
This is the WM_COMMAND command item, which is a command processing.
So the window function can be as designed:
/ / -------------------------------------------------------------------------------------------- ---------------------- // Window function
/ / -------------------------------------------------------------------------------------------- ----------------------
LResult Callback WndProc (HWND HWND, UINT MESSAGE,
WPARAM WPARAM, LPARAM LPARAM)
{
INT I;
For (i = 0; i IF (message == _MessageEntries [i] .nmessage) Return (* _ MessageEntries [i] .pfn) (HWND, Message, WPARAM, LPARAM); } Return (DEFWINDOWPROC (HWND, MESSAGE, WPARAM, LPARAM); } / / -------------------------------------------------------------------------------------------- ---------------------- // oncommand - dedicated WM_COMMAND / / -------------------------------------------------------------------------------------------- ---------------------- Long Oncommand (HWND HWND, UINT MESSAGE, WPARAM WPARAM, LPARAM LPARAM) { INT I; For (i = 0; i IF (loword (wparam) == _commandentries [i] .nmessage) Return ((* _ Commandentries [i] .pfn) (HWND, Message, WPARAM, LPARAM); } Return (DEFWINDOWPROC (HWND, MESSAGE, WPARAM, LPARAM); } / / -------------------------------------------------------------------------------------------- ---------------------- Long Oncreate (HWND HWND, UINT WMSG, UINT WPARAM, Long LPARAM) { ... } / / -------------------------------------------------------------------------------------------- ---------------------- Long Onabout (HWND HWND, UINT WMSG, UINT WPARAM, Long LPARAM) { ... } / / -------------------------------------------------------------------------------------------- ---------------------- In this way, WndProc and OnCommand never have to change, every new message, as long as _MessageEntries [] and _commandentries [] two arrays plus new elements, and write new news Treatment of the process.