Today, I read the first chapter of MFC, which is really good.
Needless to say, I was dizzy when I read more texts.
Let's take a look at the illustration above!
Graph compiled on the connection:
Message sending and processing diagram:
Function and resource connection call:
Function code optimization:
In fact, there is nothing to say, and the code is better than any text description.
The following section shows 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 (HINSTANCE hInstance)
{
WNDCLASS wc;
...
Return (RegisterClass (& wc ));
}
//--------------------------------------------------
BOOL InitInstance (HINSTANCE hInstance, int nCmdShow)
{
_ HWnd = CreateWindow (...);
...
}
Message Map ):
Is it possible to make the content of window functions more modular and generalized? Below is a practice. Please note that,
The structure name and variable name of the message ing table are as follows,
Same as MFC.
First, define a MSGMAP_ENTRY structure and a dim collection:
Struct MSGMAP_ENTRY {
UINT nMessage;
LONG (* pfn) (HWND, UINT, WPARAM, LPARAM );
};
# Define dim (x) (sizeof (x)/sizeof (x [0])
Note that the second element of MSGMAP_ENTRY, pfn, is a function pointer.
NMessage.
Next, organize the two arrays _ messageEntries [] and _ commandEntries [] to remove
Information and the regular network structure of message processing are established:
// Compare the message processing Regular Expression
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,
}; Too many
This is a message. This is a message processing standard.
// Command-ID is used to process the regular comparison table
Struct MSGMAP_ENTRY _ commandEntries =
{
IDM_ABOUT, OnAbout,
IDM_FILEOPEN, OnFileOpen,
IDM_SAVEAS, OnSaveAs,
}; Too many
This is the WM_COMMAND command. This is a common command processing method.
Therefore, window functions can be designed as follows:
//----------------------------------------------------------------------
// Window functions
//----------------------------------------------------------------------
Lresult callback WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
Int I;
For (I = 0; I <dim (_ messageentries); I ++) {// message table
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 hWnd, UINT wMsg, UINT wParam, LONG lParam)
{
...
}
//----------------------------------------------------------------------
LONG OnAbout (HWND hWnd, UINT wMsg, UINT wParam, LONG lParam)
{
...
}
//----------------------------------------------------------------------
In this way, WndProc and OnCommand never need to be changed. Every time there is a new message to be processed
Add new elements to the _ messageEntries [] and _ commandEntries [] arrays and write new elements for new messages.
.