04.ÏûÏ¢Ó³ÉäµÄʵÏÖ ÏÂÔØ±¾ÎÄ

4. ÏûÏ¢Ó³ÉäµÄʵÏÖ

1. WindowsÏûÏ¢¸ÅÊö

WindowsÓ¦ÓóÌÐòµÄÊäÈëÓÉWindowsϵͳÒÔÏûÏ¢µÄÐÎʽ·¢Ë͸øÓ¦ÓóÌÐòµÄ´°¿Ú¡£ÕâЩ´°¿Úͨ¹ý´°¿Ú¹ý³ÌÀ´½ÓÊպʹ¦ÀíÏûÏ¢£¬È»ºó°Ñ¿ØÖÆ·µ»¹¸øWindows¡£

1. ÏûÏ¢µÄ·ÖÀà

1. ¶ÓÁÐÏûÏ¢ºÍ·Ç¶ÓÁÐÏûÏ¢

´ÓÏûÏ¢µÄ·¢ËÍ;¾¶ÉÏ¿´£¬ÏûÏ¢·ÖÁ½ÖÖ£º¶ÓÁÐÏûÏ¢ºÍ·Ç¶ÓÁÐÏûÏ¢¡£¶ÓÁÐÏûÏ¢Ë͵½ÏµÍ³ÏûÏ¢¶ÓÁУ¬È»ºóµ½Ïß³ÌÏûÏ¢¶ÓÁУ»·Ç¶ÓÁÐÏûÏ¢Ö±½ÓË͸øÄ¿µÄ´°¿Ú¹ý³Ì¡£

ÕâÀ¶ÔÏûÏ¢¶ÓÁвûÊöÈçÏ£º

Windowsά»¤Ò»¸öϵͳÏûÏ¢¶ÓÁУ¨System message queue£©£¬Ã¿¸öGUIÏß³ÌÓÐÒ»¸öÏß³ÌÏûÏ¢¶ÓÁУ¨Thread message queue£©¡£

Êó±ê¡¢¼üÅÌʼþÓÉÊó±ê»ò¼üÅÌÇý¶¯³ÌÐòת»»³ÉÊäÈëÏûÏ¢²¢°ÑÏûÏ¢·Å½øÏµÍ³ÏûÏ¢¶ÓÁУ¬ÀýÈçWM_MOUSEMOVE¡¢WM_LBUTTONUP¡¢WM_KEYDOWN¡¢WM_CHARµÈµÈ¡£Windowsÿ´Î´ÓϵͳÏûÏ¢¶ÓÁÐÒÆ×ßÒ»¸öÏûÏ¢£¬È·¶¨ËüÊÇË͸øÄĸö´°¿ÚµÄºÍÕâ¸ö´°¿ÚÊÇÓÉÄĸöÏ̴߳´½¨µÄ£¬È»ºó£¬°ÑËü·Å½ø´°¿Ú´´½¨Ï̵߳ÄÏß³ÌÏûÏ¢¶ÓÁС£Ïß³ÌÏûÏ¢¶ÓÁнÓÊÕË͸ø¸ÃÏß³ÌËù´´½¨´°¿ÚµÄÏûÏ¢¡£Ï̴߳ÓÏûÏ¢¶ÓÁÐÈ¡³öÏûÏ¢£¬Í¨¹ýWindows°ÑËüË͸øÊʵ±µÄ´°¿Ú¹ý³ÌÀ´´¦Àí¡£ ³ýÁ˼üÅÌ¡¢Êó±êÏûÏ¢ÒÔÍ⣬¶ÓÁÐÏûÏ¢»¹ÓÐWM_PAINT¡¢WM_TIMERºÍWM_QUIT¡£ ÕâЩ¶ÓÁÐÏûÏ¢ÒÔÍâµÄ¾ø´ó¶àÊýÏûÏ¢ÊǷǶÓÁÐÏûÏ¢¡£ 2. ϵͳÏûÏ¢ºÍÓ¦ÓóÌÐòÏûÏ¢

´ÓÏûÏ¢µÄÀ´Ô´À´¿´£¬¿ÉÒÔ·ÖΪ£ºÏµÍ³¶¨ÒåµÄÏûÏ¢ºÍÓ¦ÓóÌÐò¶¨ÒåµÄÏûÏ¢¡£ ϵͳÏûÏ¢IDµÄ·¶Î§ÊÇ´Ó0µ½WM_USER-1£¬»ò0X80000µ½0XBFFFF£»Ó¦ÓóÌÐòÏûÏ¢´ÓWM_USER£¨0X0400£©µ½0X7FFF£¬»ò0XC000µ½0XFFFF£»WM_USERµ½0X7FFF·¶Î§µÄÏûÏ¢ÓÉÓ¦ÓóÌÐò×Ô¼ºÊ¹Óã»0XC000µ½0XFFFF·¶Î§µÄÏûÏ¢ÓÃÀ´ºÍÆäËûÓ¦ÓóÌÐòͨÐÅ£¬ÎªÁËIDµÄΨһÐÔ£¬Ê¹ÓÃ::RegisterWindowMessageÀ´µÃµ½¸Ã·¶Î§µÄÏûÏ¢ID¡£

1. ÏûÏ¢½á¹¹ºÍÏûÏ¢´¦Àí

1. ÏûÏ¢µÄ½á¹¹

ΪÁË´ÓÏûÏ¢¶ÓÁлñÈ¡ÏûÏ¢ÐÅÏ¢£¬ÐèҪʹÓÃMSG½á¹¹¡£ÀýÈ磬::GetMessageº¯Êý£¨´ÓÏûÏ¢¶ÓÁеõ½ÏûÏ¢²¢´Ó¶ÓÁÐÖÐÒÆ×ߣ©ºÍ::PeekMessageº¯Êý(´ÓÏûÏ¢¶ÓÁеõ½ÏûÏ¢µ«ÊÇ¿ÉÒÔ²»ÒÆ×ß)¶¼Ê¹ÓÃÁ˸ýṹÀ´±£´æ»ñµÃµÄÏûÏ¢ÐÅÏ¢¡£

MSG½á¹¹µÄ¶¨ÒåÈçÏ£º

typedef struct tagMSG { // msg HWND hwnd; UINT message; WPARAM wParam; LPARAM lParam; DWORD time; POINT pt; } MSG;

¸Ã½á¹¹°üÀ¨ÁËÁù¸ö³ÉÔ±£¬ÓÃÀ´ÃèÊöÏûÏ¢µÄÓйØÊôÐÔ£º

½ÓÊÕÏûÏ¢µÄ´°¿Ú¾ä±ú¡¢ÏûÏ¢±êʶ£¨ID£©¡¢µÚÒ»¸öÏûÏ¢²ÎÊý¡¢µÚ¶þ¸öÏûÏ¢²ÎÊý¡¢ÏûÏ¢²úÉúµÄʱ¼ä¡¢ÏûÏ¢²úÉúʱÊó±êµÄλÖᣠ2. Ó¦ÓóÌÐòͨ¹ý´°¿Ú¹ý³ÌÀ´´¦ÀíÏûÏ¢

ÈçǰËùÊö£¬Ã¿¸ö¡°´°¿ÚÀࡱ¶¼ÒªµÇ¼ÇÒ»¸öÈçÏÂÐÎʽµÄ´°¿Ú¹ý³Ì£º LRESULT CALLBACK MainWndProc ( HWND hwnd,// ´°¿Ú¾ä±ú UINT msg,// ÏûÏ¢±êʶ WPARAM wParam,//ÏûÏ¢²ÎÊý1 LPARAM lParam//ÏûÏ¢²ÎÊý2 )

Ó¦ÓóÌÐòͨ¹ý´°¿Ú¹ý³ÌÀ´´¦ÀíÏûÏ¢£º·Ç¶ÓÁÐÏûÏ¢ÓÉWindowsÖ±½ÓË͸øÄ¿µÄ´°¿ÚµÄ´°¿Ú¹ý³Ì£¬¶ÓÁÐÏûÏ¢ÓÉ::DispatchMessageµÈÅÉ·¢¸øÄ¿µÄ´°¿ÚµÄ´°¿Ú¹ý³Ì¡£´°¿Ú¹ý³Ì±»µ÷ÓÃʱ£¬½ÓÊÜËĸö²ÎÊý£º a window handle£¨´°¿Ú¾ä±ú£©£» a message identifier£¨ÏûÏ¢±êʶ£©£»

two 32-bit values called message parameters£¨Á½¸ö32λµÄÏûÏ¢²ÎÊý£©£» ÐèÒªµÄ»°£¬´°¿Ú¹ý³ÌÓÃ::GetMessageTime»ñÈ¡ÏûÏ¢²úÉúµÄʱ¼ä£¬ÓÃ::GetMessagePos»ñÈ¡ÏûÏ¢²úÉúʱÊó±ê¹â±êËùÔÚµÄλÖᣠÔÚ´°¿Ú¹ý³ÌÀÓÃswitch/case·ÖÖ§´¦ÀíÓï¾äÀ´Ê¶±ðºÍ´¦ÀíÏûÏ¢¡£ 3. Ó¦ÓóÌÐòͨ¹ýÏûϢѭ»·À´»ñµÃ¶ÔÏûÏ¢µÄ´¦Àí

ÿ¸öGDIÓ¦ÓóÌÐòÔÚÖ÷´°¿Ú´´½¨Ö®ºó£¬¶¼»á½øÈëÏûϢѭ»·£¬½ÓÊÜÓû§ÊäÈë¡¢½âÊͺʹ¦ÀíÏûÏ¢¡£ ÏûϢѭ»·µÄ½á¹¹ÈçÏ£º

while (GetMessage(&msg, (HWND) NULL, 0, 0)) {//´ÓÏûÏ¢¶ÓÁеõ½ÏûÏ¢

if (hwndDlgModeless == (HWND) NULL || !IsDialogMessage(hwndDlgModeless, &msg) && !TranslateAccelerator(hwndMain, haccel, &msg)) { TranslateMessage(&msg);

DispatchMessage(&msg); //·¢ËÍÏûÏ¢ } }

ÏûϢѭ»·´ÓÏûÏ¢¶ÓÁÐÖеõ½ÏûÏ¢£¬Èç¹û²»ÊÇ¿ì½Ý¼üÏûÏ¢»òÕß¶Ô»°¿òÏûÏ¢£¬¾Í½øÐÐÏûϢת»»ºÍÅÉ·¢£¬ÈÃÄ¿µÄ´°¿ÚµÄ´°¿Ú¹ý³ÌÀ´´¦Àí¡£ µ±µÃµ½ÏûÏ¢WM_QUIT£¬»òÕß::GetMessage³ö´íʱ£¬Í˳öÏûϢѭ»·¡£ 4. MFCÏûÏ¢´¦Àí

ʹÓÃMFC¿ò¼Ü±à³Ìʱ£¬ÏûÏ¢·¢Ëͺʹ¦ÀíµÄ±¾ÖÊÒ²ÈçÉÏËùÊö¡£µ«ÊÇ£¬ÓÐÒ»µãÐèҪǿµ÷µÄÊÇ£¬ËùÓеÄMFC´°¿Ú¶¼Ê¹ÓÃͬһ´°¿Ú¹ý³Ì£¬³ÌÐòÔ±²»±ØÈ¥Éè¼ÆºÍʵÏÖ×Ô¼ºµÄ´°¿Ú¹ý³Ì£¬¶øÊÇͨ¹ýMFCÌṩµÄÒ»Ì×ÏûÏ¢Ó³Éä»úÖÆÀ´´¦ÀíÏûÏ¢¡£Òò´Ë£¬MFC¼ò»¯Á˳ÌÐòÔ±±à³Ìʱ´¦ÀíÏûÏ¢µÄ¸´ÔÓÐÔ¡£

ËùνÏûÏ¢Ó³É䣬¼òµ¥µØ½²£¬¾ÍÊÇÈóÌÐòÔ±Ö¸¶¨ÒªÄ³¸öMFCÀࣨÓÐÏûÏ¢´¦ÀíÄÜÁ¦µÄÀࣩ´¦Àíij¸öÏûÏ¢¡£MFCÌṩÁ˹¤¾ßClassWizardÀ´°ïÖúʵÏÖÏûÏ¢Ó³É䣬ÔÚ´¦ÀíÏûÏ¢µÄÀàÖÐÌí¼ÓһЩÓйØÏûÏ¢Ó³ÉäµÄÄÚÈݺʹ¦ÀíÏûÏ¢µÄ³ÉÔ±º¯Êý¡£³ÌÐòÔ±½«Íê³ÉÏûÏ¢´¦Àíº¯Êý£¬ÊµÏÖËùÏ£ÍûµÄÏûÏ¢´¦ÀíÄÜÁ¦¡£

Èç¹ûÅÉÉúÀàÒª¸²¸Ç»ùÀàµÄÏûÏ¢´¦Àíº¯Êý£¬¾ÍÓÃClassWizardÔÚÅÉÉúÀàÖÐÌí¼ÓÒ»¸öÏûÏ¢Ó³ÉäÌõÄ¿£¬ÓÃͬÑùµÄÔ­ÐͶ¨ÒåÒ»¸öº¯Êý£¬È»ºóʵÏָú¯Êý¡£Õâ¸öº¯Êý¸²¸ÇÅÉÉúÀàµÄÈκλùÀàµÄͬÃû´¦Àíº¯Êý¡£

ÏÂÃæ¼¸½Ú½«·ÖÎöMFCµÄÏûÏ¢»úÖÆµÄʵÏÖÔ­ÀíºÍÏûÏ¢´¦ÀíµÄ¹ý³Ì¡£Îª´Ë£¬Ê×ÏÈÒª·ÖÎöClassWizardʵÏÖÏûÏ¢Ó³ÉäµÄÄÚÄ»£¬È»ºóÌÖÂÛMFCµÄ´°¿Ú¹ý³Ì£¬·ÖÎöMFC´°¿Ú¹ý³ÌÊÇÈçºÎʵÏÖÏûÏ¢´¦ÀíµÄ¡£

1. ÏûÏ¢Ó³ÉäµÄ¶¨ÒåºÍʵÏÖ

1. MFC´¦ÀíµÄÈýÀàÏûÏ¢

¸ù¾Ý´¦Àíº¯ÊýºÍ´¦Àí¹ý³ÌµÄ²»Í¬£¬MFCÖ÷Òª´¦ÀíÈýÀàÏûÏ¢£º

WindowsÏûÏ¢£¬Ç°×ºÒÔ¡°WM_¡±´òÍ·£¬WM_COMMANDÀýÍâ¡£WindowsÏûÏ¢Ö±½ÓË͸øMFC´°¿Ú¹ý³Ì´¦Àí£¬´°¿Ú¹ý³Ìµ÷ÓöÔÓ¦µÄÏûÏ¢´¦Àíº¯Êý¡£Ò»°ã£¬ÓÉ´°¿Ú¶ÔÏóÀ´´¦ÀíÕâÀàÏûÏ¢£¬Ò²¾ÍÊÇ˵£¬ÕâÀàÏûÏ¢´¦Àíº¯ÊýÒ»°ãÊÇMFC´°¿ÚÀàµÄ³ÉÔ±º¯Êý¡£

? ¿ØÖÆÍ¨ÖªÏûÏ¢£¬ÊÇ¿ØÖÆ×Ó´°¿ÚË͸ø¸¸´°¿ÚµÄWM_COMMAND֪ͨÏûÏ¢¡£´°¿Ú¹ý³Ìµ÷ÓöÔÓ¦µÄÏûÏ¢´¦Àíº¯Êý¡£Ò»°ã£¬ÓÉ´°¿Ú¶ÔÏóÀ´´¦ÀíÕâÀàÏûÏ¢£¬Ò²¾ÍÊÇ˵£¬ÕâÀàÏûÏ¢´¦Àíº¯ÊýÒ»°ãÊÇMFC´°¿ÚÀàµÄ³ÉÔ±º¯Êý¡£

?

ÐèÒªÖ¸³öµÄÊÇ£¬Win32ʹÓÃеÄWM_NOFITYÀ´´¦Àí¸´ÔÓµÄ֪ͨÏûÏ¢¡£WM_COMMANDÀàÐ͵Ä֪ͨÏûÏ¢½ö½öÄÜ´«µÝÒ»¸ö¿ØÖÆ´°¿Ú¾ä±ú(lparam)¡¢¿ØÖÆ´°IDºÍ֪ͨ´úÂë(wparam)¡£WM_NOTIFYÄÜ´«µÝÈÎÒ⸴ÔÓµÄÐÅÏ¢¡£

?

ÃüÁîÏûÏ¢£¬ÕâÊÇÀ´×Բ˵¥¡¢¹¤¾ßÌõ°´Å¥¡¢¼ÓËÙ¼üµÈÓû§½Ó¿Ú¶ÔÏóµÄ

WM_COMMAND֪ͨÏûÏ¢£¬ÊôÓÚÓ¦ÓóÌÐò×Ô¼º¶¨ÒåµÄÏûÏ¢¡£Í¨¹ýÏûÏ¢Ó³Éä»úÖÆ£¬MFC¿ò¼Ü°ÑÃüÁî°´Ò»¶¨µÄ·¾¶·Ö·¢¸ø¶àÖÖÀàÐ͵ĶÔÏ󣨾߱¸ÏûÏ¢´¦ÀíÄÜÁ¦£©´¦Àí£¬ÈçÎĵµ¡¢´°¿Ú¡¢Ó¦ÓóÌÐò¡¢ÎĵµÄ£°åµÈ¶ÔÏó¡£ÄÜ´¦ÀíÏûÏ¢Ó³ÉäµÄÀà±ØÐë´ÓCCmdTargetÀàÅÉÉú¡£

ÔÚÌÖÂÛÁËÏûÏ¢µÄ·ÖÀàÖ®ºó£¬Ó¦¸ÃÊÇÌÖÂÛ¸÷ÀàÏûÏ¢ÈçºÎ´¦ÀíµÄʱºòÁË¡£µ«ÊÇ£¬ÒªÖªµÀÔõô´¦ÀíÏûÏ¢£¬Ê×ÏÈÒªÖªµÀÈçºÎÓ³ÉäÏûÏ¢¡£

1. MFCÏûÏ¢Ó³ÉäµÄʵÏÖ·½·¨

MFCʹÓÃClassWizard°ïÖúʵÏÖÏûÏ¢Ó³É䣬ËüÔÚÔ´ÂëÖÐÌí¼ÓһЩÏûÏ¢Ó³ÉäµÄÄÚÈÝ£¬²¢ÉùÃ÷ºÍʵÏÖÏûÏ¢´¦Àíº¯Êý¡£ÏÖÔÚÀ´·ÖÎöÕâЩ±»Ìí¼ÓµÄÄÚÈÝ¡£

ÔÚÀàµÄ¶¨Ò壨ͷÎļþ£©ÀËüÔö¼ÓÁËÏûÏ¢´¦Àíº¯ÊýÉùÃ÷£¬²¢Ìí¼ÓÒ»ÐÐÉùÃ÷ÏûÏ¢Ó³ÉäµÄºêDECLARE_MESSAGE_MAP¡£ ÔÚÀàµÄʵÏÖ£¨ÊµÏÖÎļþ£©ÀʵÏÖÏûÏ¢´¦Àíº¯Êý£¬²¢Ê¹ÓÃIMPLEMENT_MESSAGE_MAPºêʵÏÖÏûÏ¢Ó³Éä¡£Ò»°ãÇé¿öÏ£¬ÕâЩÉùÃ÷ºÍʵÏÖÊÇÓÉMFCµÄClassWizard×Ô¶¯À´Î¬»¤µÄ¡£¿´Ò»¸öÀý×Ó£º

ÔÚAppWizard²úÉúµÄÓ¦ÓóÌÐòÀàµÄÔ´ÂëÖУ¬Ó¦ÓóÌÐòÀàµÄ¶¨Ò壨ͷÎļþ£©°üº¬ÁËÀàËÆÈçϵĴúÂ룺 //{{AFX_MSG(CTttApp) afx_msg void OnAppAbout(); //}}AFX_MSG

DECLARE_MESSAGE_MAP()

Ó¦ÓóÌÐòÀàµÄʵÏÖÎļþÖаüº¬ÁËÀàËÆÈçϵĴúÂ룺 BEGIN_MESSAGE_MAP(CTApp, CWinApp) //{{AFX_MSG_MAP(CTttApp)

ON_COMMAND(ID_APP_ABOUT, OnAppAbout) //}}AFX_MSG_MAP END_MESSAGE_MAP()

Í·ÎļþÀïÊÇÏûÏ¢Ó³ÉäºÍÏûÏ¢´¦Àíº¯ÊýµÄÉùÃ÷£¬ÊµÏÖÎļþÀïÊÇÏûÏ¢Ó³ÉäµÄʵÏÖºÍÏûÏ¢´¦Àíº¯ÊýµÄʵÏÖ¡£Ëü±íʾÈÃÓ¦ÓóÌÐò¶ÔÏó´¦ÀíÃüÁîÏûÏ¢ID_APP_ABOUT£¬ÏûÏ¢´¦Àíº¯ÊýÊÇOnAppAbout¡£

ΪʲôÕâÑù×öÖ®ºó¾ÍÍê³ÉÁËÒ»¸öÏûÏ¢Ó³É䣿ÕâЩÉùÃ÷ºÍʵÏÖµ½µ××÷ÁËÐ©Ê²Ã´ÄØ£¿½Ó×Å£¬½«ÌÖÂÛÕâЩÎÊÌâ¡£ 2. ÔÚÉùÃ÷ÓëʵÏÖµÄÄÚ²¿

1. DECLARE_MESSAGE_MAPºê£º

Ê×ÏÈ£¬¿´DECLARE_MESSAGE_MAPºêµÄÄÚÈÝ£º #ifdef _AFXDLL

#define DECLARE_MESSAGE_MAP() \\ private: \\

static const AFX_MSGMAP_ENTRY _messageEntries[]; \\ protected: \\

static AFX_DATA const AFX_MSGMAP messageMap; \\

static const AFX_MSGMAP* PASCAL _GetBaseMessageMap(); \\ virtual const AFX_MSGMAP* GetMessageMap() const; \\ #else

#define DECLARE_MESSAGE_MAP() \\ private: \\

static const AFX_MSGMAP_ENTRY _messageEntries[]; \\ protected: \\

static AFX_DATA const AFX_MSGMAP messageMap; \\ virtual const AFX_MSGMAP* GetMessageMap() const; \\ #endif

DECLARE_MESSAGE_MAP¶¨ÒåÁËÁ½¸ö°æ±¾£¬·Ö±ðÓÃÓÚ¾²Ì¬»òÕß¶¯Ì¬Á´½Óµ½MFC DLLµÄÇéÐΡ£ 2. BEGIN_MESSAE_MAPºê

È»ºó£¬¿´BEGIN_MESSAE_MAPºêµÄÄÚÈÝ£º #ifdef _AFXDLL

#define BEGIN_MESSAGE_MAP(theClass, baseClass) \\

const AFX_MSGMAP* PASCAL theClass::_GetBaseMessageMap() \\

{ return &baseClass::messageMap; } \\

const AFX_MSGMAP* theClass::GetMessageMap() const \\ { return &theClass::messageMap; } \\

AFX_DATADEF const AFX_MSGMAP theClass::messageMap = \\

{ &theClass::_GetBaseMessageMap, &theClass::_messageEntries[0] }; \\

const AFX_MSGMAP_ENTRY theClass::_messageEntries[] = \\ { \\ #else

#define BEGIN_MESSAGE_MAP(theClass, baseClass) \\ const AFX_MSGMAP* theClass::GetMessageMap() const \\ { return &theClass::messageMap; } \\

AFX_DATADEF const AFX_MSGMAP theClass::messageMap = \\ { &baseClass::messageMap, &theClass::_messageEntries[0] }; \\ const AFX_MSGMAP_ENTRY theClass::_messageEntries[] = \\ { \\ #endif

#define END_MESSAGE_MAP() \\

{0, 0, 0, 0, AfxSig_end, (AFX_PMSG)0 } \\ }; \\

¶ÔÓ¦µØ£¬BEGIN_MESSAGE_MAP¶¨ÒåÁËÁ½¸ö°æ±¾£¬·Ö±ðÓÃÓÚ¾²Ì¬»òÕß¶¯Ì¬Á´½Óµ½MFC DLLµÄÇéÐΡ£END_MESSAGE_MAPÏà¶Ô¼òµ¥£¬¾ÍÖ»ÓÐÒ»ÖÖ¶¨Òå¡£ 3. ON_COMMANDºê

×îºó£¬¿´ON_COMMANDºêµÄÄÚÈÝ£º

#define ON_COMMAND(id, memberFxn) \\ {\\

WM_COMMAND,\\ CN_COMMAND,\\ (WORD)id,\\ (WORD)id,\\ AfxSig_vv,\\

(AFX_PMSG)memberFxn\\ }£»

1. ÏûÏ¢Ó³ÉäÉùÃ÷µÄ½âÊÍ

ÔÚÇå³þÁËÓйغêµÄ¶¨ÒåÖ®ºó£¬ÏÖÔÚÀ´·ÖÎöËüÃǵÄ×÷Óú͹¦ÄÜ¡£

ÏûÏ¢Ó³ÉäÉùÃ÷µÄʵÖÊÊǸøËùÔÚÀàÌí¼Ó¼¸¸ö¾²Ì¬³ÉÔ±±äÁ¿ºÍ¾²Ì¬»òÐéÄ⺯Êý£¬µ±È»ËüÃÇÊÇÓëÏûÏ¢Ó³ÉäÏà¹ØµÄ±äÁ¿ºÍº¯Êý¡£ 1. ³ÉÔ±±äÁ¿

ÓÐÁ½¸ö³ÉÔ±±äÁ¿±»Ìí¼Ó£¬µÚÒ»¸öÊÇ_messageEntries£¬µÚ¶þ¸öÊÇmessageMap¡£

?

µÚÒ»¸ö³ÉÔ±±äÁ¿µÄÉùÃ÷£º

AFX_MSGMAP_ENTRY _messageEntries[]

ÕâÊÇÒ»¸öAFX_MSGMAP_ENTRY ÀàÐ͵ÄÊý×é±äÁ¿£¬ÊÇÒ»¸ö¾²Ì¬³ÉÔ±±äÁ¿£¬ÓÃÀ´ÈÝÄÉÀàµÄÏûÏ¢Ó³ÉäÌõÄ¿¡£Ò»¸öÏûÏ¢Ó³ÉäÌõÄ¿¿ÉÒÔÓÃAFX_MSGMAP_ENTRY½á¹¹À´ÃèÊö¡£

AFX_MSGMAP_ENTRY½á¹¹µÄ¶¨ÒåÈçÏ£º struct AFX_MSGMAP_ENTRY {

//WindowsÏûÏ¢ID UINT nMessage;

//¿ØÖÆÏûÏ¢µÄ֪ͨÂë UINT nCode;

//Windows ControlµÄID UINT nID;

//Èç¹ûÊÇÒ»¶¨·¶Î§µÄÏûÏ¢±»Ó³É䣬ÔònLastIDÖ¸¶¨Æä·¶Î§ UINT nLastID;

UINT nSig;//ÏûÏ¢µÄ¶¯×÷±êʶ

//ÏìÓ¦ÏûϢʱӦִÐеĺ¯Êý(routine to call (or special value)) AFX_PMSG pfn; };

´ÓÉÏÊö½á¹¹¿ÉÒÔ¿´³ö£¬Ã¿ÌõÓ³ÉäÓÐÁ½²¿·ÖµÄÄÚÈÝ£ºµÚÒ»²¿·ÖÊǹØÓÚÏûÏ¢IDµÄ£¬°üÀ¨Ç°ËĸöÓò£»µÚ¶þ²¿·ÖÊǹØÓÚÏûÏ¢¶ÔÓ¦µÄÖ´Ðк¯Êý£¬°üÀ¨ºóÁ½¸öÓò¡£

ÔÚÉÏÊö½á¹¹µÄÁù¸öÓòÖУ¬pfnÊÇÒ»¸öÖ¸ÏòCCmdTarger³ÉÔ±º¯ÊýµÄÖ¸Õë¡£º¯ÊýÖ¸ÕëµÄÀàÐͶ¨ÒåÈçÏ£º

typedef void (AFX_MSG_CALL CCmdTarget::*AFX_PMSG)(void);

µ±Ê¹ÓÃÒ»Ìõ»òÕß¶àÌõÏûÏ¢Ó³ÉäÌõÄ¿³õʼ»¯ÏûÏ¢Ó³ÉäÊý×éʱ£¬¸÷ÖÖ²»Í¬ÀàÐ͵ÄÏûÏ¢º¯Êý¶¼±»×ª»»³ÉÕâÑùµÄÀàÐÍ£º²»½ÓÊÕ²ÎÊý£¬Ò²²»·µ»Ø²ÎÊýµÄÀàÐÍ¡£ÒòΪËùÓпÉÒÔÓÐÏûÏ¢Ó³ÉäµÄÀà¶¼ÊÇ´ÓCCmdTargeÅÉÉúµÄ£¬ËùÒÔ¿ÉÒÔʵÏÖÕâÑùµÄת»»¡£ nSigÊÇÒ»¸ö±êʶ±äÁ¿£¬ÓÃÀ´±êʶ²»Í¬Ô­Ð͵ÄÏûÏ¢´¦Àíº¯Êý£¬Ã¿Ò»¸ö²»Í¬Ô­Ð͵ÄÏûÏ¢´¦Àíº¯Êý¶ÔÓ¦Ò»¸ö²»Í¬µÄnSig¡£ÔÚÏûÏ¢·Ö·¢Ê±£¬MFCÄÚ²¿¸ù¾ÝnSig°ÑÏûÏ¢ÅÉ·¢¸ø¶ÔÓ¦µÄ³ÉÔ±º¯Êý´¦Àí£¬Êµ¼ÊÉÏ£¬¾ÍÊǸù¾ÝnSigµÄÖµ°Ñpfn»¹Ô­³ÉÏàÓ¦ÀàÐ͵ÄÏûÏ¢´¦Àíº¯Êý²¢Ö´ÐÐËü¡£

?

µÚ¶þ¸ö³ÉÔ±±äÁ¿µÄÉùÃ÷

AFX_MSGMAP messageMap;

ÕâÊÇÒ»¸öAFX_MSGMAPÀàÐ͵ľ²Ì¬³ÉÔ±±äÁ¿£¬´ÓÆäÀàÐÍÃû³ÆºÍ±äÁ¿Ãû³Æ¿ÉÒԲ³ö£¬ËüÊÇÒ»¸ö°üº¬ÁËÏûÏ¢Ó³ÉäÐÅÏ¢µÄ±äÁ¿¡£µÄÈ·£¬Ëü°ÑÏûÏ¢Ó³ÉäµÄÐÅÏ¢£¨ÏûÏ¢Ó³ÉäÊý×飩ºÍÏà¹Øº¯Êý´ò°üÔÚÒ»Æð£¬Ò²¾ÍÊÇ˵£¬µÃµ½ÁËÒ»¸öÏûÏ¢´¦ÀíÀàµÄ¸Ã±äÁ¿£¬¾ÍµÃµ½ÁËËüÈ«²¿µÄÏûÏ¢Ó³ÉäÊý¾ÝºÍ¹¦ÄÜ¡£AFX_MSGMAP½á¹¹µÄ¶¨ÒåÈçÏ£º

struct AFX_MSGMAP {

//µÃµ½»ùÀàµÄÏûÏ¢Ó³ÉäÈë¿ÚµØÖ·µÄÊý¾Ý»òÕߺ¯Êý #ifdef _AFXDLL

//pfnGetBaseMapÖ¸Ïò_GetBaseMessageMapº¯Êý const AFX_MSGMAP* (PASCAL* pfnGetBaseMap)(); #else

//pBaseMap±£´æ»ùÀàÏûÏ¢Ó³ÉäÈë¿Ú_messageEntriesµÄµØÖ· const AFX_MSGMAP* pBaseMap; #endif

//lpEntries±£´æÏûÏ¢Ó³ÉäÈë¿Ú_messageEntriesµÄµØÖ· const AFX_MSGMAP_ENTRY* lpEntries; };

´ÓÉÏÃæµÄ¶¨Òå¿ÉÒÔ¿´³ö£¬Í¨¹ýmessageMap¿ÉÒԵõ½ÀàµÄÏûÏ¢Ó³ÉäÊý×é_messageEntriesºÍº¯Êý_GetBaseMessageMapµÄµØÖ·£¨²»Ê¹ÓÃMFC DLLʱ£¬ÊÇ»ùÀàÏûÏ¢Ó³ÉäÊý×éµÄµØÖ·£©¡£ 1. ³ÉÔ±º¯Êý

?

_GetBaseMessageMap()

ÓÃÀ´µÃµ½»ùÀàÏûÏ¢Ó³ÉäµÄº¯Êý¡£

?

GetMessageMap()

ÓÃÀ´µÃµ½×ÔÉíÏûÏ¢Ó³ÉäµÄº¯Êý¡£

1. ÏûÏ¢Ó³ÉäʵÏֵĽâÊÍ

ÏûÏ¢Ó³ÉäʵÏÖµÄʵÖÊÊdzõʼ»¯ÉùÃ÷Öж¨ÒåµÄ¾²Ì¬³ÉÔ±º¯Êý_messageEntriesºÍmessageMap£¬ÊµÏÖËùÉùÃ÷µÄ¾²Ì¬»òÐéÄ⺯ÊýGetMessageMap¡¢_GetBaseMessageMap¡£

ÕâÑù£¬ÔÚ½øÈëWinMainº¯Êý֮ǰ£¬Ã¿¸ö¿ÉÒÔÏìÓ¦ÏûÏ¢µÄMFCÀà¶¼Éú³ÉÁËÒ»¸öÏûÏ¢Ó³Éä±í£¬³ÌÐòÔËÐÐʱͨ¹ý²éѯ¸Ã±íÅжÏÊÇ·ñÐèÒªÏìӦijÌõÏûÏ¢¡£ 1. ¶ÔÏûÏ¢Ó³ÉäÈë¿Ú±í(ÏûÏ¢Ó³ÉäÊý×é)µÄ³õʼ»¯

ÈçǰËùÊö£¬ÏûÏ¢Ó³ÉäÊý×éµÄÔªËØÊÇÏûÏ¢Ó³ÉäÌõÄ¿£¬ÌõÄ¿µÄ¸ñʽ·ûºÏ½á¹¹AFX_MESSAGE_ENTRYµÄÃèÊö¡£ËùÒÔ£¬Òª³õʼ»¯ÏûÏ¢Ó³ÉäÊý×飬¾Í±ØÐëʹÓ÷ûºÏ¸Ã¸ñʽµÄÊý¾ÝÀ´Ìî³ä£ºÈç¹ûÖ¸¶¨µ±Ç°Àà´¦Àíij¸öÏûÏ¢£¬Ôò°ÑºÍ¸ÃÏûÏ¢ÓйصÄÐÅÏ¢£¨Ëĸö£©ºÍÏûÏ¢´¦Àíº¯ÊýµÄµØÖ·¼°Ô­ÐÍ×éºÏ³ÉΪһ¸öÏûÏ¢Ó³ÉäÌõÄ¿£¬¼ÓÈëµ½ÏûÏ¢Ó³ÉäÊý×éÖС£

ÏÔÈ»£¬ÕâÊÇÒ»¸ö·±ËöµÄ¹¤×÷¡£ÎªÁ˼ò»¯²Ù×÷£¬MFC¸ù¾ÝÏûÏ¢µÄ²»Í¬ºÍÏûÏ¢´¦Àí·½Ê½µÄ²»Í¬£¬°ÑÏûÏ¢Ó³Éä»®·Ö³ÉÈô¸ÉÀà±ð£¬Ã¿Ò»ÀàµÄÏûÏ¢Ó³ÉäÖÁÉÙÓÐÒ»¸ö¹²ÐÔ£ºÏûÏ¢´¦Àíº¯ÊýµÄÔ­ÐÍÏàͬ¡£¶ÔÿһÀàÏûÏ¢Ó³É䣬MFC¶¨ÒåÁËÒ»¸öºêÀ´¼ò»¯³õʼ»¯ÏûÏ¢Êý×éµÄ¹¤×÷¡£ÀýÈ磬ǰÎÄÌáµ½µÄON_COMMANDºêÓÃÀ´Ó³ÉäÃüÁîÏûÏ¢£¬Ö»ÒªÖ¸¶¨ÃüÁîIDºÍÏûÏ¢´¦Àíº¯Êý¼´¿É£¬ÒòΪ¶ÔÕâÀàÃüÁîÏûÏ¢Ó³ÉäÌõÄ¿£¬ÆäËûËĸöÊôÐÔ¶¼Êǹ̶¨µÄ¡£ON_COMMANDºêµÄ³õʼ»¯ÄÚÈÝÈçÏ£º {WM_COMMAND, CN_COMMAND, (WORD)ID_APP_ABOUT, (WORD)ID_APP_ABOUT, AfxSig_vv,

(AFX_PMSG)OnAppAbout }

Õâ¸öÏûÏ¢Ó³ÉäÌõÄ¿µÄº¬ÒåÊÇ£ºÏûÏ¢IDÊÇID_APP_ABOUT£¬OnAppAbout±»×ª»»³ÉAFX_PMSGÖ¸ÕëÀàÐÍ£¬AfxSig_vvÊÇMFCÔ¤¶¨ÒåµÄö¾Ù±äÁ¿£¬ÓÃÀ´±êʶOnAppAboutµÄº¯ÊýÀàÐÍΪ²ÎÊý¿Õ(Void)¡¢·µ»Ø¿Õ(Void)¡£

ÔÚÏûÏ¢Ó³ÉäÊý×éµÄ×îºó£¬ÊǺêEND_MESSAGE_MAPµÄÄÚÈÝ£¬Ëü±êʶÏûÏ¢´¦ÀíÀàµÄÏûÏ¢Ó³ÉäÌõÄ¿µÄÖÕÖ¹¡£ 2. ¶ÔmessageMapµÄ³õʼ»¯

ÈçǰËùÊö£¬messageMapµÄÀàÐÍÊÇAFX_MESSMAP¡£

¾­¹ý³õʼ»¯£¬ÓòlpEntries±£´æÁËÏûÏ¢Ó³ÉäÊý×é_messageEntriesµÄµØÖ·£»Èç¹û¶¯Ì¬Á´½Óµ½MFC DLL£¬ÔòpfnGetBaseMap±£´æÁË_GetBaseMessageMap³ÉÔ±º¯ÊýµÄµØÖ·£»·ñÔòpBaseMap±£´æÁË»ùÀàµÄÏûÏ¢Ó³ÉäÊý×éµÄµØÖ·¡£ 3. ¶Ôº¯ÊýµÄʵÏÖ _GetBaseMessageMap()

Ëü·µ»Ø»ùÀàµÄ³ÉÔ±±äÁ¿messagMap£¨µ±Ê¹ÓÃMFC DLLʱ£©£¬Ê¹Óøú¯ÊýµÃµ½»ùÀàÏûÏ¢Ó³ÉäÈë¿Ú±í¡£ GetMessageMap()£º

Ëü·µ»Ø³ÉÔ±±äÁ¿messageMap£¬Ê¹Óøú¯ÊýµÃµ½×ÔÉíÏûÏ¢Ó³ÉäÈë¿Ú±í¡£ ˳±ã˵һÏ£¬ÏûÏ¢Ó³ÉäÀàµÄ»ùÀàCCmdTargetҲʵÏÖÁËÉÏÊöºÍÏûÏ¢Ó³ÉäÏà¹ØµÄº¯Êý£¬²»¹ý£¬ËüµÄÏûÏ¢Ó³ÉäÊý×éÊǿյġ£

¼ÈÈ»ÏûÏ¢Ó³Éäºê·½±ãÁËÏûÏ¢Ó³ÉäµÄʵÏÖ£¬ÄÇôÓбØÒªÏêϸµÄÌÖÂÛÏûÏ¢Ó³Éäºê¡£ÏÂÒ»½Ú£¬½éÉÜÏûÏ¢Ó³ÉäºêµÄ·ÖÀà¡¢Ó÷¨ºÍÓÃ;¡£

1. ÏûÏ¢Ó³ÉäºêµÄÖÖÀà

ΪÁ˼ò»¯³ÌÐòÔ±µÄ¹¤×÷£¬MFC¶¨ÒåÁËһϵÁеÄÏûÏ¢Ó³ÉäºêºÍÏñAfxSig_vvÕâÑùµÄö¾Ù±äÁ¿£¬ÒÔ¼°±ê×¼ÏûÏ¢´¦Àíº¯Êý£¬²¢ÇÒ¾ßÌåµØÊµÏÖÕâЩº¯Êý¡£ÕâÀïÖ÷ÒªÌÖÂÛÏûÏ¢Ó³Éäºê£¬³£ÓõķÖΪÒÔϼ¸Àà¡£

1. ÓÃÓÚWindowsÏûÏ¢µÄºê£¬Ç°×ºÎª¡°ON_WM_¡±¡£

ÕâÑùµÄºê²»´ø²ÎÊý£¬ÒòΪËü¶ÔÓ¦µÄÏûÏ¢ºÍÏûÏ¢´¦Àíº¯ÊýµÄº¯ÊýÃû³Æ¡¢º¯ÊýÔ­ÐÍÊÇÈ·¶¨µÄ¡£MFCÌṩÁËÕâÀàÏûÏ¢´¦Àíº¯ÊýµÄ¶¨ÒåºÍȱʡʵÏÖ¡£Ã¿¸öÕâÑùµÄºê´¦Àí²»Í¬µÄWindowsÏûÏ¢¡£

ÀýÈ磺ºêON_WM_CREATE()°ÑÏûÏ¢WM_CREATEÓ³Éäµ½OnCreateº¯Êý£¬ÏûÏ¢Ó³ÉäÌõÄ¿µÄµÚÒ»¸ö³ÉÔ±nMessageÖ¸¶¨ÎªÒª´¦ÀíµÄWindowsÏûÏ¢µÄID£¬µÚ¶þ¸ö³ÉÔ±nCodeÖ¸¶¨Îª0¡£ 2. ÓÃÓÚÃüÁîÏûÏ¢µÄºêON_COMMAND

ÕâÀàºê´øÓвÎÊý£¬ÐèҪͨ¹ý²ÎÊýÖ¸¶¨ÃüÁîIDºÍÏûÏ¢´¦Àíº¯Êý¡£ÕâЩÏûÏ¢¶¼Ó³Éäµ½WM_COMMANDÉÏ£¬Ò²¾ÍÊǽ«ÏûÏ¢Ó³ÉäÌõÄ¿µÄµÚÒ»¸ö³ÉÔ±nMessageÖ¸¶¨ÎªWM_COMMAND£¬µÚ¶þ¸ö³ÉÔ±nCodeÖ¸¶¨ÎªCN_COMMAND(¼´0)¡£ÏûÏ¢´¦Àíº¯ÊýµÄÔ­ÐÍÊÇvoid (void)£¬²»´ø²ÎÊý£¬²»·µ»ØÖµ¡£

³ýÁ˵¥ÌõÃüÁîÏûÏ¢µÄÓ³É䣬»¹ÓаÑÒ»¶¨·¶Î§µÄÃüÁîÏûÏ¢Ó³Éäµ½Ò»¸öÏûÏ¢´¦Àíº¯ÊýµÄÓ³ÉäºêON_COMMAND_RANGE¡£ÕâÀàºê´øÓвÎÊý£¬ÐèÒªÖ¸¶¨ÃüÁîIDµÄ·¶Î§ºÍÏûÏ¢

´¦Àíº¯Êý¡£ÕâЩÏûÏ¢¶¼Ó³Éäµ½WM_COMMANDÉÏ£¬Ò²¾ÍÊǽ«ÏûÏ¢Ó³ÉäÌõÄ¿µÄµÚÒ»¸ö³ÉÔ±nMessageÖ¸¶¨ÎªWM_COMMAND£¬µÚ¶þ¸ö³ÉÔ±nCodeÖ¸¶¨ÎªCN_COMMAND(¼´0)£¬µÚÈý¸ö³ÉÔ±nIDºÍµÚËĸö³ÉÔ±nLastIDÖ¸¶¨ÁËÓ³ÉäÏûÏ¢µÄÆðÖ¹·¶Î§¡£ÏûÏ¢´¦Àíº¯ÊýµÄÔ­ÐÍÊÇvoid (UINT)£¬ÓÐÒ»¸öUINTÀàÐ͵IJÎÊý£¬±íʾҪ´¦ÀíµÄÃüÁîÏûÏ¢ID£¬²»·µ»ØÖµ¡£

£¨3£©ÓÃÓÚ¿ØÖÆÍ¨ÖªÏûÏ¢µÄºê

ÕâÀàºê¿ÉÄÜ´øÓÐÈý¸ö²ÎÊý£¬ÈçON_CONTROL£¬¾ÍÐèÒªÖ¸¶¨¿ØÖÆ´°¿ÚID£¬Í¨ÖªÂëºÍÏûÏ¢´¦Àíº¯Êý£»Ò²¿ÉÄÜ´øÓÐÁ½¸ö²ÎÊý£¬Èç¾ßÌå´¦ÀíÌØ¶¨Í¨ÖªÏûÏ¢µÄºê

ON_BN_CLICKED¡¢ON_LBN_DBLCLK¡¢ON_CBN_EDITCHANGEµÈ£¬ÐèÒªÖ¸¶¨¿ØÖÆ´°¿ÚIDºÍÏûÏ¢´¦Àíº¯Êý¡£

¿ØÖÆÍ¨ÖªÏûÏ¢Ò²±»Ó³Éäµ½WM_COMMANDÉÏ£¬Ò²¾ÍÊǽ«ÏûÏ¢Ó³ÉäÌõÄ¿µÄµÚÒ»¸ö³ÉÔ±µÄnMessageÖ¸¶¨ÎªWM_COMMAND£¬µ«Êǵڶþ¸ö³ÉÔ±nCodeÊÇÌØ¶¨µÄ֪ͨÂ룬µÚÈý¸ö³ÉÔ±nIDÊÇ¿ØÖÆ×Ó´°¿ÚµÄID£¬µÚËĸö³ÉÔ±nLastIDµÈÓÚµÚÈý¸ö³ÉÔ±µÄÖµ¡£ÏûÏ¢´¦Àíº¯ÊýµÄÔ­ÐÍÊÇvoid (void)£¬Ã»ÓвÎÊý£¬²»·µ»ØÖµ¡£

»¹ÓÐÒ»Ààºê´¦Àí֪ͨÏûÏ¢ON_NOTIFY£¬ËüÀàËÆÓÚON_CONTROL£¬µ«ÊÇ¿ØÖÆÍ¨ÖªÏûÏ¢±»Ó³Éäµ½WM_NOTIFY¡£ÏûÏ¢Ó³ÉäÌõÄ¿µÄµÚÒ»¸ö³ÉÔ±µÄnMessage±»Ö¸¶¨Îª

WM_NOTIFY£¬µÚ¶þ¸ö³ÉÔ±nCodeÊÇÌØ¶¨µÄ֪ͨÂ룬µÚÈý¸ö³ÉÔ±nIDÊÇ¿ØÖÆ×Ó´°¿ÚµÄID£¬µÚËĸö³ÉÔ±nLastIDµÈÓÚµÚÈý¸ö³ÉÔ±µÄÖµ¡£ÏûÏ¢´¦Àíº¯ÊýµÄÔ­ÐÍÊÇvoid (NMHDR*, LRESULT*)£¬²ÎÊý1ÊÇNMHDRÖ¸Õ룬²ÎÊý2ÊÇLRESULTÖ¸Õ룬ÓÃÓÚ·µ»Ø½á¹û£¬µ«º¯Êý²»·µ»ØÖµ¡£

¶ÔÓ¦µØ£¬»¹ÓаÑÒ»¶¨·¶Î§µÄ¿ØÖÆ×Ó´°¿ÚµÄij¸ö֪ͨÏûÏ¢Ó³Éäµ½Ò»¸öÏûÏ¢´¦Àíº¯ÊýµÄÓ³Éäºê£¬ÕâÀàºê°üÀ¨ON__CONTROL_RANGEºÍON_NOTIFY_RANGE¡£ÕâÀàºê´øÓвÎÊý£¬ÐèÒªÖ¸¶¨¿ØÖÆ×Ó´°¿ÚIDµÄ·¶Î§ºÍ֪ͨÏûÏ¢£¬ÒÔ¼°ÏûÏ¢´¦Àíº¯Êý¡£ ¶ÔÓÚON__CONTROL_RANGE£¬Êǽ«ÏûÏ¢Ó³ÉäÌõÄ¿µÄµÚÒ»¸ö³ÉÔ±µÄnMessageÖ¸¶¨ÎªWM_COMMAND£¬µ«Êǵڶþ¸ö³ÉÔ±nCodeÊÇÌØ¶¨µÄ֪ͨÂ룬µÚÈý¸ö³ÉÔ±nIDºÍµÚËĸö³ÉÔ±nLastIDµÈÓÚÖ¸¶¨ÁË¿ØÖÆ´°¿ÚIDµÄ·¶Î§¡£ÏûÏ¢´¦Àíº¯ÊýµÄÔ­ÐÍÊÇvoid (UINT)£¬²ÎÊý±íʾҪ´¦ÀíµÄ֪ͨÏûÏ¢ÊÇÄĸöIDµÄ¿ØÖÆ×Ó´°¿Ú·¢Ë͵ģ¬º¯Êý²»·µ»ØÖµ¡£

¶ÔÓÚON__NOTIFY_RANGE£¬ÏûÏ¢Ó³ÉäÌõÄ¿µÄµÚÒ»¸ö³ÉÔ±µÄnMessage±»Ö¸¶¨ÎªWM_NOTIFY£¬µÚ¶þ¸ö³ÉÔ±nCodeÊÇÌØ¶¨µÄ֪ͨÂ룬µÚÈý¸ö³ÉÔ±nIDºÍµÚËĸö³ÉÔ±nLastIDÖ¸¶¨ÁË¿ØÖÆ´°¿ÚIDµÄ·¶Î§¡£ÏûÏ¢´¦Àíº¯ÊýµÄÔ­ÐÍÊÇvoid (UINT, NMHDR*, LRESULT*)£¬²ÎÊý1±íʾҪ´¦ÀíµÄ֪ͨÏûÏ¢ÊÇÄĸöIDµÄ¿ØÖÆ×Ó´°¿Ú·¢Ë͵쬲ÎÊý2ÊÇNMHDRÖ¸Õ룬²ÎÊý3ÊÇLRESULTÖ¸Õ룬ÓÃÓÚ·µ»Ø½á¹û£¬µ«º¯Êý²»·µ»ØÖµ¡£ £¨4£©ÓÃÓÚÓû§½çÃæ½Ó¿Ú״̬¸üеÄON_UPDATE_COMMAND_UIºê

ÕâÀàºê±»Ó³Éäµ½ÏûÏ¢WM_COMMNDÉÏ£¬´øÓÐÁ½¸ö²ÎÊý£¬ÐèÒªÖ¸¶¨Óû§½Ó¿Ú¶ÔÏóIDºÍÏûÏ¢´¦Àíº¯Êý¡£ÏûÏ¢Ó³ÉäÌõÄ¿µÄµÚÒ»¸ö³ÉÔ±nMessage±»Ö¸¶¨ÎªWM_COMMAND£¬

µÚ¶þ¸ö³ÉÔ±nCode±»Ö¸¶¨Îª-1£¬µÚÈý¸ö³ÉÔ±nIDºÍµÚËĸö³ÉÔ±nLastID¶¼Ö¸¶¨ÎªÓû§½Ó¿Ú¶ÔÏóID¡£ÏûÏ¢´¦Àíº¯ÊýµÄÔ­ÐÍÊÇ void (CCmdUI*)£¬²ÎÊýÖ¸ÏòÒ»¸öCCmdUI¶ÔÏ󣬲»·µ»ØÖµ¡£

¶ÔÓ¦µØ£¬ÓиüÐÂÒ»¶¨ID·¶Î§µÄÓû§½Ó¿Ú¶ÔÏóµÄºêON_UPDATE_COMMAND_UI_RANGE£¬´Ëºê´øÓÐÈý¸ö²ÎÊý£¬ÓÃÓÚÖ¸¶¨Óû§½Ó¿Ú¶ÔÏóIDµÄ·¶Î§ºÍÏûÏ¢´¦Àíº¯Êý¡£ÏûÏ¢Ó³ÉäÌõÄ¿µÄµÚÒ»¸ö³ÉÔ±nMessage±»Ö¸¶¨ÎªWM_COMMAND£¬µÚ¶þ¸ö³ÉÔ±nCode±»Ö¸¶¨Îª-1£¬µÚÈý¸ö³ÉÔ±nIDºÍµÚËĸö³ÉÔ±nLastIDÓÃÓÚÖ¸¶¨Óû§½Ó¿Ú¶ÔÏóIDµÄ·¶Î§¡£ÏûÏ¢´¦Àíº¯ÊýµÄÔ­ÐÍÊÇ void (CCmdUI*)£¬²ÎÊýÖ¸ÏòÒ»¸öCCmdUI¶ÔÏ󣬺¯Êý²»·µ»ØÖµ¡£Ö®ËùÒÔ²»Óõ±Ç°Óû§½Ó¿Ú¶ÔÏóID×÷Ϊ²ÎÊý£¬ÊÇÒòΪCCmdUI¶ÔÏó°üº¬ÁËÓйØÐÅÏ¢¡£

£¨5£©ÓÃÓÚÆäËûÏûÏ¢µÄºê

ÀýÈçÓÃÓÚÓû§¶¨ÒåÏûÏ¢µÄON_MESSAGE¡£ÕâÀàºê´øÓвÎÊý£¬ÐèÒªÖ¸¶¨ÏûÏ¢IDºÍÏûÏ¢´¦Àíº¯Êý¡£ÏûÏ¢Ó³ÉäÌõÄ¿µÄµÚÒ»¸ö³ÉÔ±nMessage±»Ö¸¶¨ÎªÏûÏ¢ID£¬µÚ¶þ¸ö³ÉÔ±nCode±»Ö¸¶¨Îª0£¬µÚÈý¸ö³ÉÔ±nIDºÍµÚËĸö³ÉÔ±Ò²ÊÇ0¡£ÏûÏ¢´¦ÀíµÄÔ­ÐÍÊÇLRESULT (WPARAM, LPARAM)£¬²ÎÊý1ºÍ²ÎÊý2ÊÇÏûÏ¢²ÎÊýwParamºÍlParam£¬·µ»ØLRESULTÀàÐ͵ÄÖµ¡£ £¨6£©À©Õ¹ÏûÏ¢Ó³Éäºê

ºÜ¶àÆÕͨÏûÏ¢Ó³Éäºê¶¼ÓжÔÓ¦µÄÀ©Õ¹ÏûÏ¢Ó³Éäºê£¬ÀýÈ磺ON_COMMAND¶ÔÓ¦µÄON_COMMAND_EX£¬ON_ONTIFY¶ÔÓ¦µÄON_ONTIFY_EX£¬µÈµÈ¡£À©Õ¹ºê³ýÁ˾ßÓÐÆÕͨºêµÄ¹¦ÄÜ£¬»¹ÓÐÌØ±ðµÄÓÃ;¡£¹ØÓÚÀ©Õ¹ºêµÄ¾ßÌåÌÖÂۺͷÖÎö£¬¼û4.4.3.2½Ú¡£ ×÷Ϊһ¸ö×ܽᣬϱíÁгöÁËÕâЩ³£ÓõÄÏûÏ¢Ó³Éäºê¡£

±í4-1 ³£ÓõÄÏûÏ¢Ó³Éäºê

ÏûÏ¢Ó³Éäºê ON_COMMAND ÓÃ; °Ñcommand messageÓ³Éäµ½ÏàÓ¦µÄº¯Êý °Ñcontrol notification messageÓ³Éäµ½ÏàÓ¦µÄº¯Êý¡£MFC¸ù¾Ý²»Í¬µÄ¿ØÖÆÏûÏ¢£¬ÔÚ´Ë»ù´¡É϶¨ÒåÁ˸ü¾ßÌåµÄºê£¬ÕâÑùÓû§ÔÚʹÓÃʱ¾Í²»ÐèÒªÖ¸¶¨Í¨Öª´úÂëID£¬ÈçON_BN_CLICKED¡£ °Ñuser-defined message.Ó³Éäµ½ÏàÓ¦µÄº¯Êý °Ñregistered user-defined ON_CONTROL ON_MESSAGE ON_REGISTERED_MESSAGE messageÓ³Éäµ½ÏàÓ¦µÄº¯Êý£¬Êµ¼ÊÉÏnMessageµÈÓÚ0x0C000£¬nSigµÈÓÚºêµÄÏûÏ¢²ÎÊý¡£nSigµÄÕæÊµÖµÎªAfxsig_lwl¡£ ON_UPDATE_COMMAND_UI °Ñuser interface user update command messageÓ³Éäµ½ÏàÓ¦µÄº¯ÊýÉÏ¡£ °ÑÒ»¶¨·¶Î§ÄÚµÄcommand IDs Ó³Éäµ½ÏàÓ¦µÄº¯ÊýÉÏ ON_COMMAND_RANGE ON_UPDATE_COMMAND_UI_RANGE °ÑÒ»¶¨·¶Î§ÄÚµÄuser interface user update command messageÓ³Éäµ½ÏàÓ¦µÄº¯ÊýÉÏ ON_CONTROL_RANGE °ÑÒ»¶¨·¶Î§ÄÚµÄcontrol notification messageÓ³Éäµ½ÏàÓ¦µÄº¯ÊýÉÏ ÔÚ±í4-1ÖУ¬ºêON_REGISTERED_MESSAGEµÄ¶¨ÒåÈçÏ£º #define ON_REGISTERED_MESSAGE(nMessageVariable, memberFxn) \\ { 0xC000, 0, 0, 0,\\

(UINT)(UINT*)(&nMessageVariable), \\ /*implied 'AfxSig_lwl'*/ \\ (AFX_PMSG)(AFX_PMSGW)(LRESULT\\ (AFX_MSG_CALL CWnd::*)\\ (WPARAM, LPARAM))&memberFxn }

´ÓÉÏÃæµÄ¶¨Òå¿ÉÒÔ¿´³ö£¬Êµ¼ÊÉÏ£¬¸ÃÏûÏ¢±»Ó³Éäµ½WM_COMMAND(0XC000)£¬Ö¸¶¨µÄregisteredÏûÏ¢ID´æ·ÅÔÚnSigÓòÄÚ£¬nSigµÄÖµÔÚÕâÑùµÄÓ³ÉäÌõÄ¿ÏÂÒþº¬µØ¶¨ÎªAfxSig_lwl¡£ÓÉÓÚIDºÍÕý³£µÄnSigÓò´æ·ÅµÄÖµ·¶Î§²»Í¬£¬ËùÒÔMFC¿ÉÒÔÅжϳöÊÇ·ñÊÇregisteredÏûÏ¢Ó³ÉäÌõÄ¿¡£Èç¹ûÊÇ£¬ÔòʹÓÃAfxSig_lwl°ÑÏûÏ¢´¦Àíº¯Êýת»»³É²ÎÊý1ΪWord¡¢²ÎÊý2Ϊlong¡¢·µ»ØÖµÎªlongµÄÀàÐÍ¡£ ÔÚ½éÉÜÍêÁËÏûÏ¢Ó³ÉäµÄÄÚĻ֮ºó£¬Ó¦¸ÃÌÖÂÛÏûÏ¢´¦Àí¹ý³ÌÁË¡£ÓÉÓÚCCmdTargeµÄÌØÊâÐÔºÍÖØÒªÐÔ£¬ÔÚ4.3½ÚÏÈ¶ÔÆä×÷Ò»¸ö´óÂԵĽéÉÜ¡£

1. CcmdTargetÀà

³ýÁËCObjectÀàÍ⣬»¹ÓÐÒ»¸ö·Ç³£ÖØÒªµÄÀàCCmdTarget¡£ËùÓÐÏìÓ¦ÏûÏ¢»òʼþµÄÀà¶¼´ÓËüÅÉÉú¡£ÀýÈ磬CWinapp£¬CWnd£¬CDocument£¬CView£¬CDocTemplate£¬CFrameWnd£¬µÈµÈ¡£

CCmdTargetÀàÊÇMFC´¦ÀíÃüÁîÏûÏ¢µÄ»ù´¡¡¢ºËÐÄ¡£MFCΪ¸ÃÀàÉè¼ÆÁËÐí¶à³ÉÔ±º¯ÊýºÍһЩ³ÉÔ±Êý¾Ý£¬»ù±¾ÉÏÊÇΪÁ˽â¾öÏûÏ¢Ó³ÉäÎÊÌâµÄ£¬¶øÇÒ£¬ºÜ´óÒ»²¿·ÖÊÇÕë¶ÔOLEÉè¼ÆµÄ¡£ÔÚOLEÓ¦ÓÃÖУ¬CCmdTargetÊÇMFC´¦ÀíÄ£¿é״̬µÄÖØÒª»·½Ú£¬ËüÆðµ½ÁË´«µÝÄ£¿é״̬µÄ×÷ÓãºÆä¹¹Ô캯Êý»ñÈ¡µ±Ç°Ä£¿é״̬£¬²¢±£´æÔÚ³ÉÔ±±äÁ¿m_pModuleStateÀïÍ·¡£¹ØÓÚÄ£¿é״̬£¬ÔÚºóÃæÕ½ڽ²Êö¡£

CCmdTargetÓÐÁ½¸öÓëÏûÏ¢Ó³ÉäÓÐÃÜÇйØÏµµÄ³ÉÔ±º¯Êý£ºDispatchCmdMsgºÍOnCmdMsg¡£

1. ¾²Ì¬³ÉÔ±º¯ÊýDispatchCmdMsg

CCmdTargetµÄ¾²Ì¬³ÉÔ±º¯ÊýDispatchCmdMsg£¬ÓÃÀ´·Ö·¢WindowsÏûÏ¢¡£´Ëº¯ÊýÊÇMFCÄÚ²¿Ê¹Óõ쬯äÔ­ÐÍÈçÏ£º static BOOL DispatchCmdMsg( CCmdTarget* pTarget, UINT nID, int nCode, AFX_PMSG pfn, void* pExtra, UINT nSig,

AFX_CMDHANDLERINFO* pHandlerInfo)

¹ØÓڴ˺¯Êý½«ÔÚ4.4.3.2Õ½ÚÃüÁîÏûÏ¢µÄ´¦ÀíÖÐ×÷¸üÏêϸµÄÃèÊö¡£ 2. ÐéÄ⺯ÊýOnCmdMsg

CCmdTargetµÄÐéÄ⺯ÊýOnCmdMsg£¬ÓÃÀ´´«µÝºÍ·¢ËÍÏûÏ¢¡¢¸üÐÂÓû§½çÃæ¶ÔÏóµÄ״̬£¬ÆäÔ­ÐÍÈçÏ£º OnCmdMsg(

UINT nID,

int nCode, void* pExtra,

AFX_CMDHANDLERINFO* pHandlerInfo)

¿ò¼ÜµÄÃüÁîÏûÏ¢´«µÝ»úÖÆÖ÷ÒªÊÇͨ¹ý¸Ãº¯ÊýÀ´ÊµÏֵġ£Æä²ÎÊýÃèÊö²Î¼û4.3.3.2Õ½ÚDispacthCMdMessageµÄ²ÎÊýÃèÊö¡£

ÔÚ±¾ÊéÖУ¬ÃüÁîÄ¿±êָϣÍû»òÕß¿ÉÄÜ´¦ÀíÏûÏ¢µÄ¶ÔÏó£»ÃüÁîÄ¿±êÀàÖ¸ÃüÁîÄ¿±êµÄÀà¡£

CCmdTarget¶ÔOnCmdMsgµÄĬÈÏʵÏÖ£ºÔÚµ±Ç°ÃüÁîÄ¿±ê(thisËùÖ¸)µÄÀàºÍ»ùÀàµÄÏûÏ¢Ó³ÉäÊý×éÀïËÑË÷Ö¸¶¨ÃüÁîÏûÏ¢µÄÏûÏ¢´¦Àíº¯Êý£¨±ê×¼WindowsÏûÏ¢²»»áË͵½ÕâÀï´¦Àí£©¡£

ÕâÀïʹÓÃÐéÄ⺯ÊýGetMessageMapµÃµ½ÃüÁîÄ¿±êÀàµÄÏûÏ¢Ó³ÉäÈë¿ÚÊý×é

_messageEntries£¬È»ºóÔÚÊý×éÀïÆ¥ÅäÖ¸¶¨µÄÏûÏ¢Ó³ÉäÌõÄ¿¡£Æ¥Åä±ê×¼£ºÃüÁîÏûÏ¢IDÏàͬ£¬¿ØÖÆÍ¨Öª´úÂëÏàͬ¡£ÒòΪGetMessageMapÊÇÐéÄ⺯Êý£¬ËùÒÔ¿ÉÒÔÈ·Èϵ±Ç°ÃüÁîÄ¿±êµÄÈ·ÇÐÀà¡£

Èç¹ûÕÒµ½ÁËÒ»¸öÆ¥ÅäµÄÏûÏ¢Ó³ÉäÌõÄ¿£¬ÔòʹÓÃDispachCmdMsgµ÷ÓÃÕâ¸ö´¦Àíº¯Êý£»

Èç¹ûûÓÐÕÒµ½£¬ÔòʹÓÃ_GetBaseMessageMapµÃµ½»ùÀàµÄÏûÏ¢Ó³ÉäÊý×飬²éÕÒ£¬Ö±µ½ÕÒµ½»òËÑѰÁËËùÓеĻùÀࣨµ½CCmdTarget£©ÎªÖ¹£» Èç¹û×îºóûÓÐÕÒµ½£¬Ôò·µ»ØFASLE¡£

ÿ¸ö´ÓCCmdTargetÅÉÉúµÄÃüÁîÄ¿±êÀà¶¼¿ÉÒÔ¸²¸ÇOnCmdMsg£¬ÀûÓÃËüÀ´È·¶¨ÊÇ·ñ¿ÉÒÔ´¦ÀíijÌõÃüÁÈç¹û²»ÄÜ£¬¾Íͨ¹ýµ÷ÓÃÏÂÒ»ÃüÁîÄ¿±êµÄOnCmdMsg£¬°Ñ¸ÃÃüÁîË͸øÏÂÒ»¸öÃüÁîÄ¿±ê´¦Àí¡£Í¨³££¬ÅÉÉúÀา¸ÇOnCmdMsgʱ£¬Òªµ÷ÓûùÀàµÄ±»¸²¸ÇµÄOnCmdMsg¡£

ÔÚMFC¿ò¼ÜÖУ¬Ò»Ð©MFCÃüÁîÄ¿±êÀา¸ÇÁËOnCmdMsg£¬Èç¿ò¼Ü´°¿ÚÀา¸ÇÁ˸ú¯Êý£¬ÊµÏÖÁËMFCµÄ±ê×¼ÃüÁîÏûÏ¢·¢ËÍ·¾¶¡£¾ßÌåʵÏÖ¼ûºóÐøÕ½ڡ£

±ØÒªµÄ»°£¬Ó¦ÓóÌÐòÒ²¿ÉÒÔ¸²¸ÇOnCmdMsg£¬¸Ä±äÒ»¸ö»ò¶à¸öÀàÖеķ¢Ë͹涨£¬ÊµÏÖÓë±ê×¼¿ò¼Ü·¢Ë͹涨²»Í¬µÄ·¢ËÍ·¾¶¡£ÀýÈ磬ÔÚÒÔÏÂÇé¿ö¿ÉÒÔ×÷ÕâÑùµÄ´¦Àí£ºÔÚÒª´ò¶Ï·¢ËÍ˳ÐòµÄÀàÖаÑÃüÁî´«¸øÒ»¸ö·ÇMFCĬÈ϶ÔÏó£»ÔÚеķÇĬÈ϶ÔÏóÖлòÔÚ¿ÉÄÜÒª´«³öÃüÁîµÄÃüÁîÄ¿±êÖС£

±¾½Ú¶ÔCCmdTargetµÄÁ½¸ö³ÉÔ±º¯Êý×÷һЩÌÖÂÛ£¬ÊÇΪÁ˶ÔMFCµÄÏûÏ¢´¦ÀíÓÐÒ»¸ö´óÖÂÓ¡Ï󡣺óÃæ4.4.3.2½ÚºÍ4.4.3.3½Ú½«×÷½øÒ»²½µÄÌÖÂÛ¡£

1. MFC´°¿Ú¹ý³Ì

ǰÎÄÔø¾­Ìáµ½£¬ËùÓеÄÏûÏ¢¶¼Ë͸ø´°¿Ú¹ý³Ì´¦Àí£¬MFCµÄËùÓд°¿Ú¶¼Ê¹ÓÃͬһ´°¿Ú¹ý³Ì£¬ÏûÏ¢»òÕßÖ±½ÓÓÉ´°¿Ú¹ý³Ìµ÷ÓÃÏàÓ¦µÄÏûÏ¢´¦Àíº¯Êý´¦Àí£¬»òÕß°´MFCÃüÁîÏûÏ¢ÅÉ·¢Â·¾¶Ë͸øÖ¸¶¨µÄÃüÁîÄ¿±ê´¦Àí¡£

ÄÇô£¬MFCµÄ´°¿Ú¹ý³ÌÊÇʲô£¿Ôõô´¦Àí±ê×¼WindowsÏûÏ¢£¿ÔõôʵÏÖÃüÁîÏûÏ¢µÄÅÉ·¢£¿ÕâЩ¶¼½«ÊÇÏÂÎÄÒª»Ø´ðµÄÎÊÌâ¡£

1. MFC´°¿Ú¹ý³ÌµÄÖ¸¶¨

´ÓÇ°ÃæµÄÌÖÂÛ¿ÉÖª£¬Ã¿Ò»¸ö¡°´°¿ÚÀࡱ¶¼ÓÐ×Ô¼ºµÄ´°¿Ú¹ý³Ì¡£Õý³£Çé¿öÏÂʹÓøá°´°¿ÚÀࡱ´´½¨µÄ´°¿Ú¶¼Ê¹ÓÃËüµÄ´°¿Ú¹ý³Ì¡£

MFCµÄ´°¿Ú¶ÔÏóÔÚ´´½¨HWND´°¿Úʱ£¬Ò²Ê¹ÓÃÁËÒѾ­×¢²áµÄ¡°´°¿ÚÀࡱ£¬ÕâЩ¡°´°¿ÚÀࡱ»òÕßʹÓÃÓ¦ÓóÌÐòÌṩµÄ´°¿Ú¹ý³Ì£¬»òÕßʹÓÃWindowsÌṩµÄ´°¿Ú¹ý³Ì£¨ÀýÈçWindows¿ØÖÆ´°¿Ú¡¢¶Ô»°¿òµÈ£©¡£ÄÇô£¬ÎªÊ²Ã´ËµMFC´´½¨µÄËùÓÐHWND´°¿ÚʹÓÃͬһ¸ö´°¿Ú¹ý³ÌÄØ£¿

ÔÚMFCÖУ¬µÄÈ·ËùÓеĴ°¿Ú¶¼Ê¹ÓÃͬһ¸ö´°¿Ú¹ý³Ì£º

AfxWndProc»òAfxWndProcBase£¨Èç¹û¶¨ÒåÁË_AFXDLL£©¡£ËüÃǵÄÔ­ÐÍÈçÏ£º LRESULT CALLBACK

AfxWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)

LRESULT CALLBACK

AfxWndProcBase(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)

ÕâÁ½¸öº¯ÊýµÄÔ­ÐͶ¼Èç4.1.1½ÚÃèÊöµÄ´°¿Ú¹ý³ÌÒ»Ñù¡£ Èç¹û¶¯Ì¬Á´½Óµ½MFC DLL(¶¨ÒåÁË_AFXDLL)£¬Ôò

AfxWndProcBase±»ÓÃ×÷´°¿Ú¹ý³Ì£¬·ñÔòAfxWndProc±»ÓÃ×÷´°¿Ú¹ý³Ì¡£AfxWndProcBaseÊ×ÏÈʹÓúêAFX_MANAGE_STATEÉèÖÃÕýÈ·µÄÄ£¿é״̬£¬È»ºóµ÷ÓÃAfxWndProc¡£

ÏÂÃæ£¬¼ÙÉ費ʹÓÃMFC DLL£¬ÌÖÂÛMFCÈçºÎʹÓÃAfxWndProcÈ¡´ú¸÷¸ö´°¿ÚµÄÔ­´°¿Ú¹ý³Ì¡£

´°¿Ú¹ý³ÌµÄÈ¡´ú·¢ÉúÔÚ´°¿Ú´´½¨µÄ¹ý³Ìʱ£¬Ê¹ÓÃÁË×ÓÀ໯(Subclass)µÄ·½·¨¡£ËùÒÔ£¬´Ó´°¿ÚµÄ´´½¨¹ý³ÌÀ´¿¼²ìÈ¡´ú¹ý

³Ì¡£´ÓÇ°Ãæ¿ÉÒÔÖªµÀ£¬´°¿Ú´´½¨×îÖÕÊÇͨ¹ýµ÷ÓÃCWnd::CreateExº¯ÊýÍê³ÉµÄ£¬·ÖÎö¸Ãº¯ÊýµÄÁ÷³Ì£¬Èçͼ4-1Ëùʾ¡£

ͼ4-1ÖеÄCREATESTRUCT½á¹¹ÀàÐ͵ıäÁ¿cs°üº¬ÁË´«µÝ¸ø´°¿Ú¹ý³ÌµÄ³õʼ»¯²ÎÊý¡£CREATESTRUCT½á¹¹ÃèÊöÁË´´½¨´°¿ÚËùÐèÒªµÄÐÅÏ¢£¬¶¨ÒåÈçÏ£º typedef struct tagCREATESTRUCT {

LPVOID lpCreateParams; //ÓÃÀ´´´½¨´°¿ÚµÄÊý¾Ý HANDLE hInstance; //´´½¨´°¿ÚµÄʵÀý HMENU hMenu; //´°¿Ú²Ëµ¥ HWND hwndParent; //¸¸´°¿Ú int cy; //¸ß¶È int cx; //¿í¶È int y; //Ô­µãY×ø±ê int x;//Ô­µãX×ø±ê LONG style; //´°¿Ú·ç¸ñ LPCSTR lpszName; //´°¿ÚÃû LPCSTR lpszClass; //´°¿ÚÀà

DWORD dwExStyle; //´°¿ÚÀ©Õ¹·ç¸ñ } CREATESTRUCT;

cs±íʾµÄ´´½¨²ÎÊý¿ÉÒÔÔÚ´´½¨´°¿Ú֮ǰ±»³ÌÐòÔ±Ð޸쬳ÌÐòÔ±¿ÉÒÔ¸²¸Çµ±Ç°´°¿ÚÀàµÄÐéÄâ³ÉÔ±º¯ÊýPreCreateWindow£¬Í¨¹ý¸Ãº¯ÊýÀ´ÐÞ¸ÄcsµÄstyleÓò£¬¸Ä±ä´°¿Ú·ç¸ñ¡£ÕâÀïcsµÄÖ÷Òª×÷ÓÃÊDZ£´æ´´½¨´°¿ÚµÄ¸÷ÖÖÐÅÏ¢£¬::CreateWindowExº¯ÊýʹÓÃcsµÄ¸÷¸öÓò×÷Ϊ²ÎÊýÀ´´´½¨´°¿Ú£¬¹ØÓڸú¯Êý¼û2.2.2½Ú¡£

ÔÚ´´½¨´°¿Ú֮ǰ£¬´´½¨ÁËÒ»¸öWH_CBTÀàÐ͵Ĺ³×Ó£¨Hook£©¡£ÕâÑù£¬´´½¨´°¿ÚʱËùÓеÄÏûÏ¢¶¼»á±»¹³×Ó¹ý³Ìº¯Êý_AfxCbtFilterHook½Ø»ñ¡£

AfxCbtFilterHookº¯ÊýÊ×Ïȼì²éÊDz»ÊÇÏ£Íû´¦ÀíµÄHook©¤©¤HCBT_CREATEWND¡£Èç¹ûÊÇ£¬ÔòÏȰÑMFC´°¿Ú¶ÔÏ󣨸öÔÏó±ØÐëÒѾ­´´½¨ÁË£©ºÍ¸Õ¸Õ´´½¨µÄWindows´°¿Ú¶ÔÏóÀ¦°óÔÚÒ»Æð£¬½¨Á¢ËüÃÇÖ®¼äµÄÓ³É䣨¼ûºóÃæÄ£¿é-Ïß³Ì״̬£©£»È»ºó£¬µ÷ÓÃ::SetWindowLongÉèÖô°¿Ú¹ý³ÌΪAfxWndProc£¬²¢±£´æÔ­´°¿Ú¹ý³ÌÔÚ´°¿ÚÀà³ÉÔ±±äÁ¿m_pfnSuperÖУ¬ÕâÑùÐγÉÒ»¸ö´°¿Ú¹ý³ÌÁ´¡£ÐèÒªµÄʱºò£¬Ô­´°¿Ú¹ý³ÌµØÖ·¿ÉÒÔͨ¹ý´°¿ÚÀà³ÉÔ±º¯ÊýGetSuperWndProcAddrµÃµ½¡£

ÕâÑù£¬AfxWndProc¾Í³ÉΪCWnd»òÆäÅÉÉúÀàµÄ´°¿Ú¹ý³Ì¡£²»ÂÛ¶ÓÁÐÏûÏ¢£¬»¹ÊǷǶÓÁÐÏûÏ¢£¬¶¼Ë͵½AfxWndProc´°¿Ú¹ý³ÌÀ´´¦Àí£¨Èç¹ûʹÓÃMFC DLL£¬ÔòAfxWndProcBase±»µ÷Óã¬È»ºóÊÇAfxWndProc£©¡£¾­¹ýÏûÏ¢·Ö·¢Ö®ºóûÓб»´¦ÀíµÄÏûÏ¢£¬½«Ë͸øÔ­´°¿Ú¹ý³Ì´¦Àí¡£

×îºó£¬ÓÐÒ»µã¿ÉÄÜÐèÒª½âÊÍ£ºÎªÊ²Ã´²»Ö±½ÓÖ¸¶¨´°¿Ú¹ý³ÌΪAfxWndProc£¬¶øÒªÕâô´ó·ÑÖÜÕÛÄØ£¿ÕâÊÇÒòΪԭ´°¿Ú¹ý³Ì£¨¡°´°¿ÚÀࡱָ¶¨µÄ´°¿Ú¹ý³Ì£©³£³£ÊDZØÒªµÄ£¬ÊDz»¿ÉȱÉٵġ£

½ÓÏÂÀ´£¬ÌÖÂÛAfxWndProc´°¿Ú¹ý³ÌÈçºÎʹÓÃÏûÏ¢Ó³ÉäÊý¾ÝʵÏÖÏûÏ¢Ó³Éä¡£WindowsÏûÏ¢ºÍÃüÁîÏûÏ¢µÄ´¦Àí²»Ò»Ñù£¬Ç°ÕßûÓÐÏûÏ¢·Ö·¢µÄ¹ý³Ì¡£ 2. ¶ÔWindowsÏûÏ¢µÄ½ÓÊպʹ¦Àí

WindowsÏûÏ¢Ë͸øAfxWndProc´°¿Ú¹ý³ÌÖ®ºó£¬AfxWndProcµÃµ½HWND´°¿Ú¶ÔÓ¦µÄMFC´°¿Ú¶ÔÏó£¬È»ºó£¬ËÑË÷¸ÃMFC´°¿Ú¶ÔÏóºÍÆä»ùÀàµÄÏûÏ¢Ó³ÉäÊý×飬Åж¨ËüÃÇÊÇ·ñ´¦Àíµ±Ç°Ïû

Ï¢£¬Èç¹ûÊÇÔòµ÷ÓöÔÓ¦µÄÏûÏ¢´¦Àíº¯Êý£¬·ñÔò£¬½øÐÐȱʡ´¦Àí¡£

ÏÂÃæ£¬ÒÔÒ»¸öÓ¦ÓóÌÐòµÄÊÓ´°¿Ú´´½¨Ê±£¬¶ÔWM_CREATEÏûÏ¢µÄ´¦ÀíΪÀý£¬ÏêϸµØÌÖÂÛWindowsÏûÏ¢µÄ·Ö·¢¹ý³Ì¡£ ÓõÚÒ»ÕµÄÀý×Ó£¬ÀàCTviewÒª´¦ÀíWM_CREATEÏûÏ¢£¬Ê¹ÓÃClassWizard¼ÓÈëÏûÏ¢´¦Àíº¯ÊýCTview::OnCreate¡£ÏÂÃæ£¬¿´Õâ¸öº¯ÊýÔõô±»µ÷Óãº

ÊÓ´°¿Ú×îÖÕµ÷ÓÃ::CreateExº¯ÊýÀ´´´½¨¡£ÓÉWindowsϵͳ·¢ËÍWM_CREATEÏûÏ¢¸øÊӵĴ°¿Ú¹ý³ÌAfxWndProc£¬²ÎÊý1ÊÇ´´½¨µÄÊÓ´°¿ÚµÄ¾ä±ú£¬²ÎÊý2ÊÇÏûÏ¢ID£¨WM_CREATE)£¬²ÎÊý3¡¢4ÊÇÏûÏ¢²ÎÊý¡£Í¼4-2ÃèÊöÁËÆäÓàµÄ´¦Àí¹ý³Ì¡£Í¼Öк¯ÊýµÄÀàÊôÏÞÖÆ²¢·ÇÔ´ÂëÖÐËù¾ßÓе쬶øÊǸù¾Ý´¦Àí¹ý³ÌµÃ³öµÄÅжϡ£ÀýÈ磬¡°CWnd::WindowProc¡±±íʾCWndÀàµÄÐéÄ⺯ÊýWindowProc±»µ÷Ó㬲¢²»Ò»¶¨µ±Ç°¶ÔÏóÊÇCWndÀàµÄʵÀý£¬ÊÂʵÉÏ£¬ËüÊÇCWndÅÉÉúÀàCTviewÀàµÄʵÀý£»¶ø¡°CTview::OnCreate¡±±íʾCTviewµÄÏûÏ¢´¦Àíº¯ÊýOnCreate±»µ÷Óá£ÏÂÃæÃèÊöÿһ²½µÄÏêϸ´¦Àí¡£

1. ´Ó´°¿Ú¹ý³Ìµ½ÏûÏ¢Ó³Éä

Ê×ÏÈ£¬·ÖÎöAfxWndProc´°¿Ú¹ý³Ìº¯Êý¡£

?

AfxWndProcµÄÔ­ÐÍÈçÏ£º

LRESULT AfxWndProc(HWND hWnd,

UINT nMsg, WPARAM wParam, LPARAM lParam)

Èç¹ûÊÕµ½µÄÏûÏ¢nMsg²»ÊÇWM_QUERYAFXWNDPROC£¨¸ÃÏûÏ¢±»MFCÄÚ²¿ÓÃÀ´È·ÈÏ´°¿Ú¹ý³ÌÊÇ·ñʹÓÃAfxWndProc£©£¬Ôò´ÓhWndµÃµ½¶ÔÓ¦µÄMFC Windows¶ÔÏ󣨸öÔÏó±ØÐëÒÑ´æÔÚ£¬ÊÇÓÀ¾ÃÐÔ¶ÔÏó£©Ö¸ÕëpWnd¡£pWndËùÖ¸µÄMFC´°¿Ú¶ÔÏ󽫸ºÔðÍê³ÉÏûÏ¢µÄ´¦Àí¡£ÕâÀpWndËùָʾµÄ¶ÔÏóÊÇMFCÊÓ´°¿Ú¶ÔÏ󣬼´CTview¶ÔÏó¡£

È»ºó£¬°ÑpWndºÍAfxWndProc½ÓÊܵÄËĸö²ÎÊý´«µÝ¸øº¯ÊýAfxCallWndProcÖ´ÐС£

?

AfxCallWndProcÔ­ÐÍÈçÏ£º

LRESULT AFXAPI AfxCallWndProc(CWnd* pWnd, HWND hWnd,

UINT nMsg, WPARAM wParam = 0, LPARAM lParam = 0)

MFCʹÓÃAfxCallWndProcº¯Êý°ÑÏûÏ¢Ë͸øCWndÀà»òÆäÅÉÉúÀàµÄ¶ÔÏ󡣸ú¯ÊýÖ÷ÒªÊǰÑÏûÏ¢ºÍÏûÏ¢²ÎÊý(nMsg¡¢wParam¡¢lParam)´«µÝ¸øMFC´°¿Ú¶ÔÏóµÄ³ÉÔ±º¯ÊýWindowProc£¨pWnd->WindowProc£©×÷½øÒ»²½´¦Àí¡£Èç¹ûÊÇWM_INITDIALOGÏûÏ¢£¬ÔòÔÚµ÷ÓÃWindowProcǰºóÒª×÷һЩ´¦Àí¡£ WindowProcµÄº¯ÊýÔ­ÐÍÈçÏ£º

LRESULT CWnd::WindowProc(UINT message,

WPARAM wParam, LPARAM lParam)

ÕâÊÇÒ»¸öÐéÄ⺯Êý£¬³ÌÐòÔ±¿ÉÒÔÔÚCWndµÄÅÉÉúÀàÖи²¸ÇËü£¬¸Ä±äMFC·Ö·¢ÏûÏ¢µÄ·½Ê½¡£ÀýÈ磬MFCµÄCControlBar¾Í¸²¸ÇÁËWindowProc£¬¶ÔijЩÏûÏ¢×÷ÁË×Ô¼ºµÄÌØ±ð´¦Àí£¬ÆäËûÏûÏ¢´¦ÀíÓÉ»ùÀàµÄWindowProcº¯ÊýÍê³É¡£

µ«ÊÇÔÚµ±Ç°Àý×ÓÖУ¬µ±Ç°¶ÔÏóµÄÀàCTviewûÓи²¸Ç¸Ãº¯Êý£¬ËùÒÔCWndµÄWindowProc±»µ÷Óá£

Õâ¸öº¯Êý°ÑÏÂÒ»²½µÄ¹¤×÷½»¸øOnWndMsgº¯ÊýÀ´´¦Àí¡£Èç¹ûOnWndMsgûÓд¦Àí£¬Ôò½»¸øDefWindowProcÀ´´¦Àí¡£

OnWndMsgºÍDefWindowProc¶¼ÊÇCWndÀàµÄÐéÄ⺯Êý¡£

?

OnWndMsgµÄÔ­ÐÍÈçÏ£º

BOOL CWnd::OnWndMsg( UINT message,

WPARAM wParam, LPARAM lParam,RESULT*pResult ); ¸Ãº¯ÊýÊÇÐéÄ⺯Êý¡£

ºÍWindowProcÒ»Ñù£¬ÓÉÓÚµ±Ç°¶ÔÏóµÄÀàCTviewûÓи²¸Ç¸Ãº¯Êý£¬ËùÒÔCWndµÄOnWndMsg±»µ÷Óá£

ÔÚCWndÖУ¬MFCʹÓÃOnWndMsgÀ´·Ö±ð´¦Àí¸÷ÀàÏûÏ¢£º Èç¹ûÊÇWM_COMMANDÏûÏ¢£¬½»¸øOnCommand´¦Àí£»È»ºó·µ»Ø¡£ Èç¹ûÊÇWM_NOTIFYÏûÏ¢£¬½»¸øOnNotify´¦Àí£»È»ºó·µ»Ø¡£

Èç¹ûÊÇWM_ACTIVATEÏûÏ¢£¬ÏȽ»¸ø_AfxHandleActivate´¦Àí£¨ºóÃæ5.3.3.7½Ú»á½âÊÍËüµÄ´¦Àí£©£¬ÔÙ¼ÌÐøÏÂÃæµÄ´¦Àí¡£

Èç¹ûÊÇWM_SETCURSORÏûÏ¢£¬ÏȽ»¸ø_AfxHandleSetCursor´¦Àí£»È»ºó·µ»Ø¡£ Èç¹ûÊÇÆäËûµÄWindowsÏûÏ¢£¨°üÀ¨WM_ACTIVATE£©£¬Ôò

Ê×ÏÈÔÚÏûÏ¢»º³å³Ø½øÐÐÏûϢƥÅ䣬

ÈôÆ¥Åä³É¹¦£¬Ôòµ÷ÓÃÏàÓ¦µÄÏûÏ¢´¦Àíº¯Êý£»

Èô²»³É¹¦£¬ÔòÔÚÏûϢĿ±êµÄÏûÏ¢Ó³ÉäÊý×éÖнøÐвéÕÒÆ¥Å䣬¿´ËüÊÇ·ñ´¦Àíµ±Ç°ÏûÏ¢¡£ÕâÀÏûϢĿ±ê¼´CTview¶ÔÏó¡£

Èç¹ûÏûϢĿ±ê´¦ÀíÁ˸ÃÏûÏ¢£¬Ôò»áÆ¥Åäµ½ÏûÏ¢´¦Àíº¯Êý£¬µ÷ÓÃËü½øÐд¦Àí£»

·ñÔò£¬¸ÃÏûϢûÓб»Ó¦ÓóÌÐò´¦Àí£¬OnWndMsg·µ»ØFALSE¡£

¹ØÓÚWindowsÏûÏ¢ºÍÏûÏ¢´¦Àíº¯ÊýµÄÆ¥Å䣬¼ûÏÂÒ»½Ú¡£

ȱʡ´¦Àíº¯ÊýDefWindowProc½«ÔÚÌÖÂÛ¶Ô»°¿òµÈµÄʵÏÖʱ¾ßÌå·ÖÎö¡£

1. WindowsÏûÏ¢µÄ²éÕÒºÍÆ¥Åä

CWnd»òÕßÅÉÉúÀàµÄ¶ÔÏóµ÷ÓÃOnWndMsgËÑË÷±¾¶ÔÏó»òÕß»ùÀàµÄÏûÏ¢Ó³ÉäÊý×飬ѰÕÒµ±Ç°ÏûÏ¢µÄÏûÏ¢´¦Àíº¯Êý¡£Èç¹ûµ±Ç°¶ÔÏó»òÕß»ùÀà´¦ÀíÁ˵±Ç°ÏûÏ¢£¬Ôò±Ø¶¨ÔÚÆäÖÐÒ»¸öÀàµÄÏûÏ¢Ó³ÉäÊý×éÖÐÆ¥Åäµ½µ±Ç°ÏûÏ¢µÄ´¦Àíº¯Êý¡£

ÏûϢƥÅäÊÇÒ»¸ö±È½ÏºÄʱµÄÈÎÎñ£¬ÎªÁËÌá¸ßЧÂÊ£¬MFCÉè¼ÆÁËÒ»¸öÏûÏ¢»º³å³Ø£¬°ÑÒª´¦ÀíµÄÏûÏ¢ºÍÆ¥Åäµ½µÄÏûÏ¢Ó³ÉäÌõÄ¿£¨ÌõÄ¿°üº¬ÁËÏûÏ¢´¦Àíº¯ÊýµÄµØÖ·£©ÒÔ¼°½øÐÐÏûÏ¢´¦ÀíµÄµ±Ç°ÀàµÈÐÅÏ¢¹¹³ÉÒ»Ìõ»º³åÐÅÏ¢£¬

·Åµ½»º³å³ØÖС£Èç¹ûÒÔºóÓÖÓÐͬÑùµÄÏûÏ¢ÐèҪͬһ¸öÀà´¦Àí£¬ÔòÖ±½Ó´Ó»º³å³Ø²éÕÒµ½¶ÔÓ¦µÄÏûÏ¢Ó³ÉäÌõÄ¿¾Í¿ÉÒÔÁË¡£

MFCÓùþÏ£²éÕÒÀ´²éѯÏûÏ¢Ó³É仺³å³Ø¡£ÏûÏ¢»º³å³ØÏ൱ÓÚÒ»¸ö¹þÏ£±í£¬ËüÊÇÓ¦ÓóÌÐòµÄÒ»¸öÈ«¾Ö±äÁ¿£¬¿ÉÒÔ·Å512Ìõ×îÐÂÓõ½µÄÏûÏ¢Ó³ÉäÌõÄ¿µÄ»º³åÐÅÏ¢£¬Ã¿Ò»Ìõ»º³åÐÅÏ¢ÊǹþÏ£±íµÄÒ»¸öÈë¿Ú¡£

²ÉÓÃAFX_MSG_CACHE½á¹¹ÃèÊöÿÌõ»º³åÐÅÏ¢£¬Æä¶¨ÒåÈçÏ£º

struct AFX_MSG_CACHE {

UINT nMsg;

const AFX_MSGMAP_ENTRY* lpEntry; const AFX_MSGMAP* pMessageMap; };

nMsg´æ·ÅÏûÏ¢ID£¬Ã¿¸ö¹þÏ£±íÈë¿ÚÓв»Í¬µÄnMsg¡£ lpEnty´æ·ÅºÍÏûÏ¢IDÆ¥ÅäµÄÏûÏ¢Ó³ÉäÌõÄ¿µÄµØÖ·£¬Ëü¿ÉÄÜÊÇthisËùÖ¸¶ÔÏóµÄÀàµÄÓ³ÉäÌõÄ¿£¬Ò²¿ÉÄÜÊÇÕâ¸öÀàµÄij¸ö»ùÀàµÄÓ³ÉäÌõÄ¿£¬Ò²¿ÉÄÜÊǿա£ pMessageMap´æ·ÅÏûÏ¢´¦Àíº¯ÊýÆ¥Åä³É¹¦Ê±½øÐÐÏûÏ¢´¦ÀíµÄµ±Ç°ÀࣨthisËùÖ¸¶ÔÏóµÄÀࣩµÄ¾²Ì¬³ÉÔ±±äÁ¿messageMapµÄµØÖ·£¬ËüΨһµÄ±êʶÁËÒ»¸öÀࣨÿ¸öÀàµÄmessageMap±äÁ¿¶¼²»Ò»Ñù£©¡£

thisËùÖ¸¶ÔÏóÊÇÒ»¸öCWnd»òÆäÅÉÉúÀàµÄʵÀý£¬ÊÇÕýÔÚ´¦ÀíÏûÏ¢µÄMFC´°¿Ú¶ÔÏó¡£

¹þÏ£²éÕÒ£ºÊ¹ÓÃÏûÏ¢IDµÄÖµ×÷Ϊ¹Ø¼üÖµ½øÐйþÏ£²éÕÒ£¬Èç¹û³É¹¦£¬¼´¿É´ÓlpEntry»ñµÃÏûÏ¢Ó³ÉäÌõÄ¿µÄµØÖ·£¬´Ó¶øµÃµ½ÏûÏ¢´¦Àíº¯Êý¼°ÆäÔ­ÐÍ¡£ ÈçºÎÅжÏÊÇ·ñ³É¹¦Æ¥ÅäÄØ£¿ÓÐÁ½Ìõ±ê×¼£º

µÚÒ»£¬µ±Ç°Òª´¦ÀíµÄÏûÏ¢messageÔÚ¹þÏ£±í£¨»º³å³Ø£©ÖÐÓÐÈë¿Ú£»µÚ¶þ£¬µ±Ç°´°¿Ú¶ÔÏó£¨thisËùÖ¸¶ÔÏó£©

µÄÀàµÄ¾²Ì¬±äÁ¿messageMapµÄµØÖ·Ó¦¸ÃµÈÓÚ±¾Ìõ»º³åÐÅÏ¢µÄpMessagMap¡£MFCͨ¹ýÐéÄ⺯ÊýGetMessagMapµÃµ½messageMapµÄµØÖ·¡£

Èç¹ûÔÚÏûÏ¢»º³å³ØÖÐûÓÐÕÒµ½Æ¥Å䣬ÔòËÑË÷µ±Ç°¶ÔÏóµÄÏûÏ¢Ó³ÉäÊý×飬¿´ÊÇ·ñÓкÏÊʵÄÏûÏ¢´¦Àíº¯Êý¡£ Èç¹ûÆ¥Åäµ½Ò»¸öÏûÏ¢´¦Àíº¯Êý,Ôò°ÑÆ¥Åä½á¹û¼ÓÈëµ½ÏûÏ¢»º³å³ØÖУ¬¼´Ìîд¸ÃÌõÏûÏ¢¶ÔÓ¦µÄ¹þÏ£±íÈë¿Ú£º nMsg=message;

pMessageMap=this->GetMessageMap; lpEntry=²éÕÒ½á¹û

È»ºó£¬µ÷ÓÃÆ¥Åäµ½µÄÏûÏ¢´¦Àíº¯Êý¡£·ñÔò£¨Ã»ÓÐÕÒµ½£©£¬Ê¹ÓÃ_GetBaseMessageMapµÃµ½»ùÀàµÄÏûÏ¢Ó³ÉäÊý×飬²éÕÒºÍÆ¥Å䣻ֱµ½Æ¥Åä³É¹¦»òËÑѰÁËËùÓеĻùÀࣨµ½CCmdTarget£©ÎªÖ¹¡£

Èç¹û×îºóûÓÐÕÒµ½£¬ÔòÒ²°Ñ¸ÃÌõÏûÏ¢µÄÆ¥Åä½á¹û¼ÓÈëµ½»º³å³ØÖС£ºÍÆ¥Åä³É¹¦²»Í¬µÄÊÇ£ºÖ¸¶¨lpEntryΪ¿Õ¡£ÕâÑùOnWndMsg·µ»Ø£¬°Ñ¿ØÖÆÈ¨·µ»¹¸ø

AfxCallWndProcº¯Êý£¬AfxCallWndProc½«¼ÌÐøµ÷ÓÃDefWndProc½øÐÐȱʡ´¦Àí¡£

ÏûÏ¢Ó³ÉäÊý×éµÄËÑË÷ÔÚCCmdTarget::OnCmdMsgº¯ÊýÖÐÒ²Óõ½ÁË£¬¶øÇÒËã·¨Ïàͬ¡£ÎªÁËÌá¸ßËÙ¶È£¬MFC°ÑºÍÏûÏ¢Ó³ÉäÊý×éÌõÄ¿ÖðÒ»±È½Ï¡¢Æ¥ÅäµÄº¯ÊýAfxFindMessageEntryÓûã±àÊéд¡£ const AFX_MSGMAP_ENTRY* AFXAPI

AfxFindMessageEntry(const AFX_MSGMAP_ENTRY* lpEntry,

UINT nMsg, UINT nCode, UINT nID)

µÚÒ»¸ö²ÎÊýÊÇÒªËÑË÷µÄÓ³ÉäÊý×éµÄÈë¿Ú£»µÚ¶þ¸ö²ÎÊýÊÇWindowsÏûÏ¢±êʶ£»µÚÈý¸ö²ÎÊýÊÇ¿ØÖÆÍ¨ÖªÏûÏ¢±êʶ£»µÚËĸö²ÎÊýÊÇÃüÁîÏûÏ¢±êʶ¡£

¶ÔWindowsÏûÏ¢À´Ëµ£¬nMsgÊÇÿÌõÏûÏ¢²»Í¬µÄ£¬nIDºÍnCodeΪ0¡£

¶ÔÃüÁîÏûÏ¢À´Ëµ£¬nMsg¹Ì¶¨ÎªWM_COMMAND£¬nIDÊÇÿÌõÏûÏ¢²»Í¬£¬nCode¶¼ÊÇCN_COMMAND£¨¶¨ÒåΪ0£©¡£ ¶Ô¿ØÖÆÍ¨ÖªÏûÏ¢À´Ëµ£¬nMsg¹Ì¶¨ÎªWM_COMMAND»òÕßWM_NOTIFY£¬nIDºÍnCodeÊÇÿÌõÏûÏ¢²»Í¬¡£ ¶ÔÓÚRegisterÏûÏ¢£¬nMsgÖ¸¶¨Îª0XC000£¬nIDºÍnCodeΪ0¡£ÔÚʹÓú¯ÊýAfxFindMessageEntryµÃµ½Æ¥Åä½á¹ûÖ®ºó£¬»¹±ØÐëÅжÏnSigÊÇ·ñµÈÓÚmessage£¬Ö»ÓÐÏàµÈ²Åµ÷ÓöÔÓ¦µÄÏûÏ¢´¦Àíº¯Êý¡£ 2. WindowsÏûÏ¢´¦Àíº¯ÊýµÄµ÷ÓÃ

¶ÔÒ»¸öWindowsÏûÏ¢£¬Æ¥Åäµ½ÁËÒ»¸öÏûÏ¢Ó³ÉäÌõĿ֮ºó£¬½«µ÷ÓÃÓ³ÉäÌõÄ¿ËùָʾµÄÏûÏ¢´¦Àíº¯Êý¡£ µ÷Óô¦Àíº¯ÊýµÄ¹ý³Ì¾ÍÊÇת»»Ó³ÉäÌõÄ¿µÄpfnÖ¸ÕëΪÊʵ±µÄº¯ÊýÀàÐͲ¢Ö´ÐÐËü£ºMFC¶¨ÒåÁËÒ»¸ö³ÉÔ±º¯ÊýÖ¸Õëmmf£¬Ê×ÏȰÑÏûÏ¢´¦Àíº¯ÊýµÄµØÖ·¸³Öµ¸ø¸Ãº¯ÊýÖ¸Õ룬Ȼºó¸ù¾ÝÏûÏ¢Ó³ÉäÌõÄ¿µÄnSigֵת»»Ö¸ÕëµÄÀàÐÍ¡£µ«ÊÇ£¬Òª¸øº¯ÊýÖ¸Õëmmf¸³Öµ£¬±ØÐëʹ¸ÃÖ¸Õë¿ÉÒÔÖ¸ÏòËùÓеÄÏûÏ¢´¦Àíº¯Êý£¬Îª´ËÔò¸ÃÖ¸ÕëµÄÀàÐÍÊÇËùÓÐÀàÐ͵ÄÏûÏ¢´¦Àíº¯ÊýÖ¸ÕëµÄÁªºÏÌå¡£ ¶ÔÉÏÊö¹ý³Ì£¬MFCµÄʵÏÖ´óÂÔÈçÏ£º union MessageMapFunctions mmf£» mmf.pfn = lpEntry->pfn; swithc (value_of_nsig){ ?

case AfxSig_is: //OnCreate¾ÍÊǸÃÀàÐÍ

lResult = (this->*mmf.pfn_is)((LPTSTR)lParam); break; ? default:

ASSERT(FALSE); break;

} ?

LDispatchRegistered: // ´¦Àíregistered windows messages

ASSERT(message >= 0xC000); mmf.pfn = lpEntry->pfn;

lResult = (this->*mmf.pfn_lwl)(wParam, lParam); ?

Èç¹ûÏûÏ¢´¦Àíº¯ÊýÓзµ»ØÖµ£¬Ôò·µ»Ø¸Ã½á¹û£¬·ñÔò£¬·µ»ØTRUE¡£

¶ÔÓÚͼ4-1ËùʾµÄÀý×Ó£¬nSigµÈÓÚAfxSig_is£¬ËùÒÔ½«Ö´ÐÐÓï¾ä

(this->*mmf.pfn_is)((LPTSTR)lParam) Ò²¾ÍÊǶÔCTview::OnCreateµÄµ÷Óá£

˳±ãÖ¸³ö£¬¶ÔÓÚRegistered´°¿ÚÏûÏ¢£¬ÏûÏ¢´¦Àíº¯Êý¶¼ÊÇͬһԭÐÍ£¬ËùÒÔ¶¼±»×ª»»³ÉlwlÐÍ£¨¹ØÓÚRegistered´°¿ÚÏûÏ¢µÄÓ³É䣬¼û4.4.2½Ú£©¡£ ×ÛÉÏËùÊö£¬±ê×¼WindwosÏûÏ¢ºÍÓ¦ÓóÌÐòÏûÏ¢ÖеÄRegisteredÏûÏ¢£¬ÓÉ´°¿Ú¹ý³ÌÖ±½Óµ÷ÓÃÏàÓ¦µÄ´¦Àíº¯Êý´¦Àí£º

Èç¹ûij¸öÀàÐ͵Ĵ°¿Ú£¨C++Àࣩ´¦ÀíÁËijÌõÏûÏ¢£¨¸²¸ÇÁËCWnd»òÖ±½Ó»ùÀàµÄ´¦Àíº¯Êý£©£¬Ôò¶ÔÓ¦µÄHWND´°¿Ú£¨Winodws window£©ÊÕµ½¸ÃÏûϢʱ¾Íµ÷Óøø²¸Çº¯ÊýÀ´´¦Àí£»Èç¹û¸ÃÀà´°¿ÚûÓд¦Àí¸ÃÏûÏ¢£¬Ôòµ÷ÓÃʵÏָô¦Àíº¯Êý×îÖ±½ÓµÄ»ùÀࣨÔÚC++µÄÀà²ã´ÎÉϽӽü¸ÃÀࣩÀ´´¦Àí£¬ÉÏÊöÀý×ÓÖÐÈç¹ûCTview²»´¦ÀíWM_CREATEÏûÏ¢£¬Ôòµ÷ÓÃÉÏÒ»²ãµÄCWnd::OnCreate´¦Àí£»

Èç¹û»ùÀà¶¼²»´¦Àí¸ÃÏûÏ¢£¬Ôòµ÷ÓÃDefWndProcÀ´´¦Àí¡£

3. ÏûÏ¢Ó³Éä»úÖÆÍê³ÉÐéÄ⺯Êý¹¦ÄܵÄÔ­Àí

×ۺ϶ÔWindowsÏûÏ¢µÄ´¦ÀíÀ´¿´£¬MFCʹÓÃÏûÏ¢Ó³Éä»úÖÆÍê³ÉÁËC++ÐéÄ⺯ÊýµÄ¹¦ÄÜ¡£ÕâÖ÷Òª»ùÓÚÒÔϼ¸µã£º

ËùÓд¦ÀíÏûÏ¢µÄÀà´ÓCCmdTargetÅÉÉú¡£

? ʹÓþ²Ì¬³ÉÔ±±äÁ¿_messageEntriesÊý×é´æ·ÅÏûÏ¢Ó³ÉäÌõÄ¿£¬Ê¹Óþ²Ì¬³ÉÔ±±äÁ¿messageMapÀ´Î¨Ò»µØÇø±ðºÍµÃµ½ÀàµÄÏûÏ¢Ó³Éä¡£

? ͨ¹ýGetMessageÐéÄ⺯ÊýÀ´»ñÈ¡µ±Ç°¶ÔÏóµÄÀàµÄmessageMap±äÁ¿£¬½ø¶øµÃµ½ÏûÏ¢Ó³ÉäÈë¿Ú¡£ ? °´ÕÕÏȵײ㣬ºó»ù²ãµÄ˳ÐòÔÚÀàµÄÏûÏ¢Ó³ÉäÊý×éÖÐËÑË÷ÏûÏ¢´¦Àíº¯Êý¡£»ùÓÚÕâÑùµÄ»úÖÆ£¬Ò»°ãÔÚ¸²¸Ç»ùÀàµÄÏûÏ¢´¦Àíº¯Êýʱ£¬Ó¦¸Ãµ÷ÓûùÀàµÄͬÃûº¯Êý¡£

?

ÒÔÉÏÂÛ¶ÏÊʺÏÓÚMFCÆäËûÏûÏ¢´¦Àí»úÖÆ£¬Èç¶ÔÃüÁîÏûÏ¢µÄ´¦ÀíµÈ¡£²»Í¬µÄÊÇÆäËûÏûÏ¢´¦ÀíÓÐÒ»¸öÃüÁîÅÉ·¢/·Ö·¢µÄ¹ý³Ì¡£ ÏÂÒ»½Ú£¬ÌÖÂÛÃüÁîÏûÏ¢µÄ½ÓÊܺʹ¦Àí¡£

1. ¶ÔÃüÁîÏûÏ¢µÄ½ÓÊպʹ¦Àí

1. MFC±ê×¼ÃüÁîÏûÏ¢µÄ·¢ËÍ

ÔÚSDI»òÕßMDIÓ¦ÓóÌÐòÖУ¬ÃüÁîÏûÏ¢ÓÉÓû§½çÃæ¶ÔÏó£¨Èç²Ëµ¥¡¢¹¤¾ßÌõµÈ£©²úÉú£¬È»ºóË͸øÖ÷±ß¿ò´°¿Ú¡£Ö÷±ß¿ò´°¿ÚʹÓñê×¼MFC´°¿Ú¹ý³Ì´¦ÀíÃüÁîÏûÏ¢¡£´°¿Ú¹ý³Ì°ÑÃüÁî´«µÝ¸øMFCÖ÷±ß¿ò´°¿Ú¶ÔÏ󣬿ªÊ¼ÃüÁîÏûÏ¢µÄ·Ö·¢¡£MFC±ß¿ò´°¿ÚÀàCFrameWndÌṩÁËÏûÏ¢·Ö·¢µÄÄÜÁ¦¡£

ÏÂÃæ£¬»¹ÊÇͨ¹ýÒ»¸öÀý×ÓÀ´ËµÃ÷ÃüÁîÏûÏ¢µÄ´¦Àí¹ý³Ì¡£

ʹÓÃAppWizard²úÉúÒ»¸öµ¥ÎĵµÓ¦ÓóÌÐòt¡£´Óhelp²Ëµ¥Ñ¡Ôñ¡°About¡±£¬¾Í»áµ¯³öÒ»¸öABOUT¶Ô»°¿ò¡£ÏÂÃæ£¬ÌÖÂÛ´ÓÃüÁîÏûÏ¢µÄ·¢³öµ½¶Ô»°¿òµ¯³öµÄ¹ý³Ì¡£ Ê×ÏÈ£¬Ñ¡Ôñ¡° About¡±²Ëµ¥ÏîµÄ¶¯×÷µ¼ÖÂÒ»¸öWindowsÃüÁîÏûÏ¢ID_APP_ABOUTµÄ²úÉú¡£Windowsϵͳ·¢Ë͸ÃÃüÁîÏûÏ¢µ½±ß¿ò´°¿Ú£¬µ¼ÖÂËüµÄ´°¿Ú¹ý³ÌAfxWndProc±»µ÷Ó㬲ÎÊý1ÊDZ߿ò´°¿ÚµÄ¾ä±ú£¬²ÎÊý2ÊÇÏûÏ¢ID£¨¼´WM_COMMAND£©£¬²ÎÊý3¡¢4ÊÇÏûÏ¢²ÎÊý£¬²ÎÊý3µÄÖµÊÇID_APP_ABOUT¡£½Ó×ŵÄϵÁе÷ÓÃÈçͼ4-3Ëùʾ¡£

ÏÂÃæ·Ö±ð½²Êöÿһ²ãËùµ÷Óõĺ¯Êý¡£ ǰ4²½Í¬¶ÔWindowsÏûÏ¢µÄ´¦Àí¡£ÕâÀï½ÓÊÜÏûÏ¢µÄHWND´°¿ÚÊÇÖ÷±ß¿ò´°¿Ú£¬Òò´Ë£¬AfxWndProc¸ù¾ÝHWND¾ä±úµÃµ½µÄMFC´°¿Ú¶ÔÏóÊÇMFC±ß¿ò´°¿Ú¶ÔÏó¡£

ÔÚ4.2.2½Ú̸µ½£¬Èç¹ûCWnd::OnWndMsgÅжÏÒª´¦ÀíµÄÏûÏ¢ÊÇÃüÁîÏûÏ¢

(WM_COMMAND)£¬¾Íµ÷ÓÃOnCommand½øÒ»²½´¦Àí¡£ÓÉÓÚ

OnCommandÊÇÐéÄ⺯Êý£¬µ±Ç°MFC´°¿Ú¶ÔÏóÊDZ߿ò´°¿Ú¶ÔÏó£¬

ËüµÄÀà´ÓCFrameWndÀർ³ö£¬Ã»Óи²¸ÇCWndµÄÐéÄ⺯ÊýOnCommand£¬¶øCFrameWnd¸²¸ÇÁËCWndµÄOnCommand£¬ËùÒÔ£¬CFrameWndµÄOnCommand±»µ÷Ó᣻»¾ä»°Ëµ£¬CFrameWndµÄOnCommand±»µ÷ÓÃÊǶ¯Ì¬Ô¼ÊøµÄ½á¹û¡£½Ó׎éÉܵı¾Àý×ÓµÄÓйص÷Óã¬Ò²ÊÇͨ¹ý¶¯Ì¬Ô¼Êø¶øÊµ¼Ê·¢ÉúµÄº¯Êýµ÷Óá£

½Ó×ŵÄÓйص÷Ó㬽«²»½øÐÐΪʲôµ÷ÓÃij¸öÀàµÄÐéÄâ»òÕßÏûÏ¢´¦Àíº¯ÊýµÄ·ÖÎö¡£ £¨1£©CFrameWndµÄOnCommandº¯Êý

BOOL CFrameWnd::OnCommand(WPARAM wParam, LPARAM lParam)

²ÎÊýwParamµÄµÍ½×word´æ·ÅÁ˲˵¥ÃüÁînID»ò¿ØÖÆ×Ó´°¿ÚID£»Èç¹ûÏûÏ¢À´×Ô¿ØÖÆ´°¿Ú£¬¸ß½×word´æ·ÅÁË¿ØÖÆÍ¨ÖªÏûÏ¢£»Èç¹ûÏûÏ¢À´×Ô¼ÓËÙ¼ü£¬¸ß½×wordֵΪ1£»Èç¹ûÏûÏ¢À´×Բ˵¥£¬¸ß½×wordֵΪ0¡£

Èç¹ûÊÇ֪ͨÏûÏ¢£¬²ÎÊýlParam´æ·ÅÁË¿ØÖÆ´°¿ÚµÄ¾ä±úhWndCtrl£¬ÆäËûÇé¿öÏÂlParamÊÇ0¡£

ÔÚÕâ¸öÀý×ÓÀµÍ½×wordÊÇID_APP_ABOUT£¬¸ß½×wordÊÇ1£»lParamÊÇ0¡£ MFC¶ÔCFrameWndµÄȱʡʵÏÖÖ÷ÒªÊÇ»ñµÃÒ»¸ö»ú»áÀ´¼ì²é³ÌÐòÊÇ·ñÔËÐÐÔÚHELP״̬£¬ÐèÒªÖ´ÐÐÉÏÏÂÎİïÖú£¬Èç¹û²»ÐèÒª£¬Ôòµ÷ÓûùÀàµÄCWnd::OnCommandʵÏÖÕý³£µÄÃüÁîÏûÏ¢·¢ËÍ¡£

£¨2£©CWndµÄOnCommandº¯Êý

BOOL CWnd::OnCommand(WPARAM wParam, LPARAM lParam)

Ëü°´Ò»¶¨µÄ˳Ðò´¦ÀíÃüÁî»òÕß֪ͨÏûÏ¢£¬Èç¹û·¢Ëͳɹ¦£¬·µ»ØTRUE£¬·ñÔò£¬FALSE¡£´¦Àí˳ÐòÈçÏ£º

Èç¹ûÊÇÃüÁîÏûÏ¢£¬Ôòµ÷ÓÃOnCmdMsg(nID, CN_UPDATE_COMMAND_UI, &state, NULL)²âÊÔnIDÃüÁîÊÇ·ñÒѾ­±»½ûÖ¹£¬Èç¹ûÕâÑù£¬·µ»ØFALSE£»·ñÔò£¬µ÷ÓÃOnCmdMsg½øÐÐÃüÁî·¢ËÍ¡£¹ØÓÚCN_UPDATE_COMMAND_UI֪ͨÏûÏ¢£¬¼ûºóÃæÓû§½çÃæ×´Ì¬µÄ¸üд¦Àí¡£

Èç¹ûÊÇ¿ØÖÆÍ¨ÖªÏûÏ¢£¬ÔòÏÈÓÃReflectLastMsg·´Éä֪ͨÏûÏ¢µ½×Ó´°¿Ú¡£Èç¹û×Ó´°¿Ú´¦ÀíÁ˸ÃÏûÏ¢£¬Ôò·µ»ØTRUE£»·ñÔò£¬µ÷ÓÃOnCmdMsg½øÐÐÃüÁî·¢ËÍ¡£¹ØÓÚ֪ͨÏûÏ¢µÄ·´Éä¼ûºóÃæ4.4.4.3½Ú¡£OnCommand¸øOnCmdMsg´«µÝËĸö²ÎÊý£ºnID£¬¼´ÃüÁîÏûÏ¢ID£»nCode£¬Èç¹ûÊÇ֪ͨÏûÏ¢ÔòΪ֪ͨ´úÂ룬Èç¹ûÊÇÃüÁîÏûÏ¢ÔòΪNC_COMMAND(¼´0)£»ÆäÓàÁ½¸ö²ÎÊýΪ¿Õ¡£ £¨3£©CFrameWndµÄOnCmdMsgº¯Êý

BOOL CFrameWnd::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)

²ÎÊý1ÊÇÃüÁîID£»Èç¹ûÊÇ֪ͨÏûÏ¢£¨WM_COMMAND»òÕßWM_NOTIFY£©£¬Ôò²ÎÊý2±íʾ֪ͨ´úÂ룬Èç¹ûÊÇÃüÁîÏûÏ¢£¬²ÎÊý2ÊÇ0£»Èç¹ûÊÇWM_NOTIFY£¬²ÎÊý3°üº¬ÁËһЩ¶îÍâµÄÐÅÏ¢£»²ÎÊý4ÔÚÕý³£ÏûÏ¢´¦ÀíÖÐÓ¦¸ÃÊǿա£ ÔÚÕâ¸öÀý×ÓÀ²ÎÊý1ÊÇÃüÁîID£¬²ÎÊý2Ϊ0£¬²ÎÊý3¿Õ¡£

OnCmdMsgÊÇÐéÄ⺯Êý£¬CFrameWnd¸²¸ÇÁ˸ú¯Êý£¬µ±Ç°¶ÔÏó£¨thisËùÖ¸£©ÊÇMFCµ¥ÎĵµµÄ±ß¿ò´°¿Ú¶ÔÏó¡£¹ÊCFrameWndµÄOnCmdMsg±»µ÷Óá£CFrameWnd::OnCmdMsgÔÚMFCÏûÏ¢·¢ËÍÖÐÕ¼Óзdz£ÖØÒªµÄµØÎ»£¬MFC¶Ô¸Ãº¯ÊýµÄȱʡʵÏÖÈ·¶¨ÁËMFCµÄ±ê×¼ÃüÁî·¢ËÍ·¾¶£º

1. Ë͸ø»î¶¯£¨Active£©ÊÓ´¦Àí£¬µ÷ÓûÊÓµÄOnCmdMsg¡£ÓÉÓÚµ±Ç°¶ÔÏóÊÇMFCÊÓ¶ÔÏó£¬ËùÒÔ£¬OnCmdMsg½«ËÑË÷CTview¼°Æä»ùÀàµÄÏûÏ¢Ó³ÉäÊý×飬ÊÔͼµÃµ½ÏàÓ¦µÄ´¦Àíº¯Êý¡£

2. Èç¹ûÊÓ¶ÔÏó×Ô¼º²»´¦Àí£¬ÔòÊӵõ½ºÍËü¹ØÁªµÄÎĵµ£¬µ÷ÓùØÁªÎĵµµÄOnCmdMsg¡£ÓÉÓÚµ±Ç°¶ÔÏóÊÇMFCÊÓ¶ÔÏó£¬ËùÒÔ£¬OnCmdMsg½«ËÑË÷CTdoc¼°Æä»ùÀàµÄÏûÏ¢Ó³ÉäÊý×飬ÊÔͼµÃµ½ÏàÓ¦µÄ´¦Àíº¯Êý¡£ 3. Èç¹ûÎĵµ¶ÔÏó²»´¦Àí£¬ÔòËüµÃµ½¹ÜÀíÎĵµµÄÎĵµÄ£°å¶ÔÏ󣬵÷ÓÃÎĵµÄ£°åµÄOnCmdMsg¡£ÓÉÓÚµ±Ç°¶ÔÏóÊÇMFCÎĵµÄ£°å¶ÔÏó£¬ËùÒÔ£¬OnCmdMsg½«ËÑË÷ÎĵµÄ£°åÀ༰Æä»ùÀàµÄÏûÏ¢Ó³ÉäÊý×飬ÊÔͼµÃµ½ÏàÓ¦µÄ´¦Àíº¯Êý¡£

4. Èç¹ûÎĵµÄ£°å²»´¦Àí£¬Ôò°ÑûÓд¦ÀíµÄÐÅÏ¢Öð¼¶·µ»Ø£ºÎĵµÄ£°å¸æËßÎĵµ¶ÔÏó£¬Îĵµ¶ÔÏó¸æËßÊÓ¶ÔÏó£¬ÊÓ¶ÔÏó¸æË߱߿ò´°¿Ú¶ÔÏó¡£×îºó£¬±ß¿ò´°¿ÚµÃÖª£¬ÊÓ¡¢Îĵµ¡¢ÎĵµÄ£°å¶¼Ã»Óд¦ÀíÏûÏ¢¡£ 5. CFrameWndµÄOnCmdMsg¼ÌÐøµ÷ÓÃCWnd::OnCmdMsg£¨Ð±Ìå±íʾÓÐÀàÊôÏÞÖÆ£©À´´¦ÀíÏûÏ¢¡£ÓÉÓÚCWndûÓи²¸ÇOnCmdMsg£¬¹Êʵ¼ÊÉϵ÷ÓÃÁ˺¯ÊýCCmdTarget::OnCmdMsg¡£ÓÉÓÚµ±Ç°¶ÔÏóÊÇMFC±ß¿ò´°¿Ú¶ÔÏó£¬ËùÒÔ

OnCmdMsgº¯Êý½«ËÑË÷CMainFrameÀ༰ÆäËùÓлùÀàµÄÏûÏ¢Ó³ÉäÊý×飬ÊÔͼµÃµ½ÏàÓ¦µÄ´¦Àíº¯Êý¡£CWndûÓÐʵÏÖOnCmdMsgÈ´Ö¸¶¨ÒªÖ´ÐÐÆäOnCmdMsgº¯Êý£¬¿ÉÄÜÊÇΪÁËÒÔºóMFC¸øCWndʵÏÖÁËOnCmdMsgÖ®ºóÆäËû´úÂë²»Óøı䡣 ÕâÒ»²½ÊDZ߿ò´°¿Ú×Ô¼º³¢ÊÔ´¦ÀíÏûÏ¢¡£

2. Èç¹û±ß¿ò´°¿Ú¶ÔÏó²»´¦Àí£¬ÔòË͸øÓ¦ÓóÌÐò¶ÔÏó´¦Àí¡£µ÷ÓÃCTAppµÄOnCmdMsg£¬ÓÉÓÚʵ¼ÊÉÏCTApp¼°Æä»ùÀàCWinAppûÓи²¸ÇOnCmdMsg£¬¹Êʵ¼ÊÉϵ÷ÓÃÁ˺¯ÊýCCmdTarget::OnCmdMsg¡£ÓÉÓÚµ±Ç°¶ÔÏóÊÇMFCÓ¦ÓóÌÐò¶ÔÏó£¬ËùÒÔOnCmdMsgº¯Êý½«ËÑË÷CTAppÀ༰ÆäËùÓлùÀàµÄµÄÏûÏ¢Ó³ÉäÈë¿ÚÊý×飬ÊÔͼµÃµ½ÏàÓ¦µÄ´¦Àíº¯Êý

3. Èç¹ûÓ¦ÓóÌÐò¶ÔÏó²»´¦Àí£¬Ôò·µ»ØFALSE£¬±íÃ÷ûÓÐÃüÁîÄ¿±ê´¦Àíµ±Ç°µÄÃüÁîÏûÏ¢¡£ÕâÑù£¬º¯ÊýÖ𼶱𷵻أ¬OnCmdMsg¸æËßOnCommandÏûϢûÓб»´¦Àí£¬OnCommand¸æËßOnWndMsgÏûϢûÓб»´¦Àí£¬OnWndMsg¸æËß

WindowProcÏûϢûÓб»´¦Àí£¬ÓÚÊÇWindowProcµ÷ÓÃDefWindowProc½øÐÐȱʡ´¦Àí¡£ ±¾Àý×ÓÔÚµÚÁù²½ÖУ¬Ó¦ÓóÌÐò¶ÔID_APP_ABOUTÏûÏ¢×÷ÁË´¦Àí¡£ËüÕÒµ½´¦Àíº¯ÊýCTApp::OnAbout£¬Ê¹ÓÃDispatchCmdMsgÅÉ·¢ÏûÏ¢¸ø¸Ãº¯Êý´¦Àí¡£

Èç¹ûÊÇMDI±ß¿ò´°¿Ú£¬±ê×¼·¢ËÍ·¾¶»¹ÓÐÒ»¸ö»·½Ú£¬¸Ã»·½ÚºÍµÚ¶þ¡¢Èý¡¢ËIJ½ËùÉæ¼°µÄOnCmdMsgº¯Êý£¬½«ÔÚÏÂÁ½½ÚÔٴξßÌå·ÖÎö¡£

1. ÃüÁîÏûÏ¢µÄÅÉ·¢ºÍÏûÏ¢µÄ¶à´Î´¦Àí

1. ÃüÁîÏûÏ¢µÄÅÉ·¢

Èçǰ3.1ËùÊö£¬CCmdTargetµÄ¾²Ì¬³ÉÔ±º¯ÊýDispatchCmdMsgÓÃÀ´ÅÉ·¢ÃüÁîÏûÏ¢¸øÖ¸¶¨µÄÃüÁîÄ¿±êµÄÏûÏ¢´¦Àíº¯Êý¡£

static BOOL DispatchCmdMsg(CCmdTarget* pTarget, UINT nID, int nCode,

AFX_PMSG pfn, void* pExtra, UINT nSig, AFX_CMDHANDLERINFO* pHandlerInfo)

Ç°ÃæÔÚ½²CCmdTargetʱ£¬Ìáµ½Á˸ú¯Êý¡£ÕâÀï½²ÊöËüµÄʵÏÖ£º µÚÒ»¸ö²ÎÊýÖ¸Ïò´¦ÀíÏûÏ¢µÄ¶ÔÏó£»µÚ¶þ¸ö²ÎÊýÊÇÃüÁîID£»µÚÈý¸öÊÇ֪ͨÏûÏ¢µÈ£»µÚËĸöÊÇÏûÏ¢´¦Àíº¯ÊýµØÖ·£»µÚÎå¸ö²ÎÊýÓÃÓÚ´æ·ÅһЩÓÐÓõÄÐÅÏ¢£¬¸ù¾ÝnCodeµÄÖµ±íʾ²»Í¬µÄÒâÒ壬ÀýÈçµ±ÏûÏ¢ÊÇWM_NOFITY£¬Ö¸ÏòÒ»¸öNMHDR½á¹¹£¨¹ØÓÚWM_NOTIFY£¬²Î¼û4.4.4.2½Ú֪ͨÏûÏ¢µÄ´¦Àí£©£»µÚÁù¸ö²ÎÊý±êʶÏûÏ¢´¦Àíº¯ÊýÔ­ÐÍ£»µÚÆß¸ö²ÎÊýÊÇÒ»¸öÖ¸Õ룬ָÏò

AFX_CMDHANDLERINFO½á¹¹¡£Ç°Áù¸ö²ÎÊý£¨³ýÁ˵ÚÎå¸öÍ⣩¶¼ÊÇÏòº¯Êý´«µÝÐÅÏ¢£¬µÚÎå¸öºÍµÚÆß¸ö²ÎÊýÊÇË«ÏòµÄ£¬¼ÈÏòº¯Êý´«µÝÐÅÏ¢£¬Ò²¿ÉÒÔÏòµ÷ÓÃÕß·µ»ØÐÅÏ¢¡£

¹ØÓÚAFX_CMDHANDLERINFO½á¹¹£º struct AFX_CMDHANDLERINFO {

CCmdTarget* pTarget;

void (AFX_MSG_CALL CCmdTarget::*pmf)(void); };

µÚÒ»¸ö³ÉÔ±ÊÇÒ»¸öÖ¸ÏòÃüÁîÄ¿±ê¶ÔÏóµÄÖ¸Õ룬µÚ¶þ¸ö³ÉÔ±ÊÇÒ»¸öÖ¸ÏòCCmdTarget³ÉÔ±º¯ÊýµÄÖ¸Õë¡£ ¸Ãº¯ÊýµÄʵÏÖÁ÷³Ì¿ÉÒÔÈçÏÂÃèÊö£º

Ê×ÏÈ£¬Ëü¼ì²é²ÎÊýpHandlerInfoÊÇ·ñ¿Õ£¬Èç¹û²»¿Õ£¬ÔòÓÃpTargetºÍpfnÌîдÆäÖ¸ÏòµÄ½á¹¹£¬·µ»ØTRUE£»Í¨³£ÏûÏ¢´¦Àíʱ´«µÝÀ´µÄpHandlerInfo¿Õ£¬¶øÔÚʹÓÃOnCmdMsgÀ´²âÊÔij¸ö¶ÔÏóÊÇ·ñ´¦ÀíijÌõÃüÁîʱ£¬´«µÝÒ»¸ö·Ç¿ÕµÄpHandlerInfoÖ¸Õë¡£Èô·µ»ØTRUE£¬Ôò±íʾ¿ÉÒÔ´¦ÀíÄÇÌõÏûÏ¢¡£ Èç¹ûpHandlerInfo¿Õ£¬Ôò½øÐÐÏûÏ¢´¦Àíº¯ÊýµÄµ÷Óá£Ëü¸ù¾Ý²ÎÊýnSigµÄÖµ£¬°Ñ²ÎÊýpfnµÄÀàÐÍת»»ÎªÒªµ÷ÓõÄÏûÏ¢´¦Àíº¯ÊýµÄÀàÐÍ¡£ÕâÖÖÖ¸Õëת»»¼¼ÊõºÍÇ°Ãæ½²ÊöµÄWindowsÏûÏ¢µÄ´¦ÀíÊÇÒ»ÑùµÄ¡£ 2. ÏûÏ¢µÄ¶à´Î´¦Àí

Èç¹ûÏûÏ¢´¦Àíº¯Êý²»·µ»ØÖµ£¬ÔòDispatchCmdMsg·µ»ØTRUE£»·ñÔò£¬

DispatchCmdMsg·µ»ØÏûÏ¢´¦Àíº¯ÊýµÄ·µ»ØÖµ¡£Õâ¸ö·µ»ØÖµÑØ×ÅÏûÏ¢·¢ËÍÏà·´µÄ·¾¶Öð¼¶ÏòÉÏ´«µÝ£¬Ê¹µÃ¸÷¸ö»·½ÚµÄOnCmdMsgºÍOnCommandµÃµ½·µ»ØµÄ´¦Àí½á¹û£ºTRUE»òÕßFALSE£¬¼´³É¹¦»òÕßʧ°Ü¡£

ÕâÑù¾Í²úÉúÁËÒ»¸öÎÊÌ⣬Èç¹ûÏûÏ¢´¦Àíº¯ÊýÓÐÒâ·µ»ØÒ»¸öFALSE£¬ÄÇô²»¾Í´«µÝÁËÒ»¸ö´íÎóµÄÐÅÏ¢£¿ÀýÈ磬OnCmdMsgº¯ÊýµÃµ½FALSE·µ»ØÖµ£¬¾ÍÈÏΪÏûϢûÓÐ

±»´¦Àí£¬Ëü½«¼ÌÐø·¢ËÍÏûÏ¢µ½ÏÂÒ»»·½Ú¡£µÄÈ·ÊÇÕâÑùµÄ£¬µ«ÊÇÕâ²»ÊÇMFCµÄ©¶´£¬¶øÊÇÓÐÒâÕâôÉè¼ÆµÄ£¬ÓÃÀ´´¦ÀíÒ»Ð©ÌØ±ðµÄÏûÏ¢Ó³Éäºê£¬ÊµÏÖͬһ¸öÏûÏ¢µÄ¶à´Î´¦Àí¡£

ͨ³£µÄÃüÁî»òÕß֪ͨÏûÏ¢ÊÇûÓзµ»ØÖµµÄ£¨¼û4.4.2½ÚµÄÏûÏ¢Ó³Éäºê£©£¬½ö½öÒ»Ð©ÌØÊâµÄÏûÏ¢´¦Àíº¯Êý¾ßÓзµ»ØÖµ£¬ÕâÀàÏûÏ¢µÄÏûÏ¢´¦Àíº¯ÊýÊÇʹÓÃÀ©Õ¹ÏûÏ¢Ó³ÉäºêÓ³ÉäµÄ£¬ÀýÈ磺

ON_COMMAND¶ÔÓ¦µÄON_COMMAND_EX

À©Õ¹Ó³ÉäºêºÍ¶ÔÓ¦µÄÆÕͨӳÉäºêµÄ²ÎÊý¸öÊýÏàͬ£¬º¬ÒåÒ»Ñù¡£µ«ÊÇÀ©Õ¹Ó³ÉäºêµÄÏûÏ¢´¦Àíº¯ÊýµÄÔ­ÐͺͶÔÓ¦µÄÆÕͨӳÉäºêÏà±È£¬ÓÐÁ½¸ö²»Í¬Ö®´¦£ºÒ»ÊǶàÁËÒ»¸öUINTÀàÐ͵IJÎÊý£¬ÁíÍâ¾ÍÊÇÓзµ»ØÖµ£¨·µ»ØBOOLÀàÐÍ£©¡£»Ø¹Ë4.4.2Õ½ڣ¬·¶Î§Ó³ÉäºêON_COMMAND_RANGEµÄÏûÏ¢´¦Àíº¯ÊýÒ²ÓÐÒ»¸öÕâÑùµÄ²ÎÊý£¬¸Ã²ÎÊýÔÚÁ½´¦µÄº¬ÒåÊÇÒ»ÑùµÄ£¬ÀýÈ磺ÃüÁîÏûÏ¢À©Õ¹Ó³ÉäºêON_COMMAND_EX¶¨ÒåµÄÏûÏ¢´¦Àíº¯Êý½âÊ͸òÎÊýÊǵ±Ç°Òª´¦ÀíµÄÃüÁîÏûÏ¢ID¡£Óзµ»ØÖµµÄÒâÒåÔÚÓÚ£ºÈç¹ûÀ©Õ¹Ó³ÉäºêµÄÏûÏ¢´¦Àíº¯Êý·µ»ØFALSE£¬Ôòµ¼Öµ±Ç°ÏûÏ¢±»·¢Ë͸øÏûϢ·¾¶ÉϵÄÏÂÒ»¸öÏûϢĿ±ê´¦Àí¡£

×ÛºÏÀ´¿´£¬ON_COMMAND_EXºêÓÐÁ½¸ö¹¦ÄÜ£º

Ò»ÊÇ¿ÉÒ԰Ѷà¸öÃüÁîÏûÏ¢Ö¸¶¨¸øÒ»¸öÏûÏ¢´¦Àíº¯Êý´¦Àí¡£ÕâÀàËÆÓÚ

ON_COMMAND_RANGEºêµÄ×÷Óᣲ»¹ý£¬ÕâÀïµÄ¶àÌõÏûÏ¢µÄÃüÁîID»òÕß¿ØÖÆ×Ó´°¿ÚID¿ÉÒÔ²»Á¬Ðø£¬Ã¿ÌõÏûÏ¢¶¼ÐèÒªÒ»¸öON_COMMAND_EXºê¡£

¶þÊÇ¿ÉÒÔÈü¸¸öÏûϢĿ±ê´¦Àíͬһ¸öÃüÁî»òÕß֪ͨ»òÕß·´ÉäÏûÏ¢¡£Èç¹ûÏûÏ¢·¢ËÍ·¾¶ÉϽÏǰµÄÃüÁîÄ¿±ê²»´¦ÀíÏûÏ¢»òÕß´¦ÀíÏûÏ¢ºó·µ»ØFALSE£¬ÔòÏÂÒ»¸öÃüÁîÄ¿±ê½«¼ÌÐø´¦Àí¸ÃÏûÏ¢¡£

¶ÔÓÚ֪ͨÏûÏ¢¡¢·´ÉäÏûÏ¢£¬ËüÃÇÒ²ÓÐÀ©Õ¹Ó³Éäºê£¬¶øÇÒÉÏÊöÂÛ¶ÏÒ²ÊʺÏÓÚËüÃÇ¡£ÀýÈ磺

ON_NOTIFY¶ÔÓ¦µÄON_NOTIFY_EX ON_CONTROL¶ÔÓ¦µÄON_CONTROL_EX

ON_CONTROL_REFLECT¶ÔÓ¦µÄON_CONTROL_REFLECT_EX µÈµÈ¡£

·¶Î§ÏûÏ¢Ó³ÉäºêÒ²ÓжÔÓ¦µÄÀ©Õ¹Ó³Éäºê£¬ÀýÈ磺 ON_NOTIFY_RANGE¶ÔÓ¦µÄON_NOTIFY_EX_RANGE ON_COMMAND_RANGE¶ÔÓ¦µÄON_COMMAND_EX_RANGE

ʹÓÃÕâЩºêµÄÄ¿µÄÔÚÓÚÀûÓÃÀ©Õ¹ºêµÄµÚ¶þ¸ö¹¦ÄÜ£ºÊµÏÖÏûÏ¢µÄ¶à´Î´¦Àí¡£ ¹ØÓÚÀ©Õ¹ÏûÏ¢Ó³ÉäºêµÄÀý×Ó£¬²Î¼û13.2..4.4½ÚºÍ13.2.4.6½Ú¡£

1. һЩÏûÏ¢´¦ÀíÀàµÄOnCmdMsgµÄʵÏÖ

´ÓÒÔÉÏÂÛÊöÖªµÀ£¬OnCmdMsgÐéÄ⺯ÊýÔÚMFCÃüÁîÏûÏ¢µÄ·¢ËÍÖаçÑÝÁËÖØÒªµÄ½ÇÉ«£¬CFrameWndµÄOnCmdMsgʵÏÖÁËMFCµÄ±ê×¼ÃüÁîÏûÏ¢·¢ËÍ·¾¶¡£

ÄÇô£¬¾Í²úÉúÒ»¸öÎÊÌ⣺Èç¹ûÃüÁîÏûÏ¢²»Ë͸ø±ß¿ò´°¿Ú¶ÔÏó£¬ÄÇô¾Í²»»áÓа´±ê×¼ÃüÁî·¢ËÍ·¾¶·¢ËÍÏûÏ¢µÄ¹ý³Ì£¿´ð°¸Êǿ϶¨µÄ¡£ÀýÈçÒ»¸ö²Ëµ¥±»Ò»¸ö¶Ô»°¿ò´°¿ÚËùÓµÓУ¬ÄÇô£¬²Ëµ¥ÃüÁË͸øMFC¶Ô»°¿ò´°¿Ú¶ÔÏó´¦Àí£¬¶ø²»ÊÇMFC±ß¿ò´°¿Ú´¦Àí£¬µ±È»²»»áºÍCFrameWndµÄ´¦ÀíÁ÷³ÌÏàͬ¡£

µ«ÊÇ£¬ÓÐÒ»µãÐèÒªÖ¸³ö£¬Ò»°ã±ê×¼µÄSDIºÍMDIÓ¦ÓóÌÐò£¬Ö»ÓÐÖ÷±ß¿ò´°¿ÚÓµÓв˵¥ºÍ¹¤¾ßÌõµÈÓû§½Ó¿Ú¶ÔÏó£¬Ö»ÓÐÔÚÓû§ÓëÓû§½Ó¿Ú¶ÔÏó½øÐн»»¥Ê±£¬²Å²úÉúÃüÁ²úÉúµÄÃüÁî±ØÈ»ÊÇË͸øSDI»òÕßMDI³ÌÐòµÄÖ÷±ß¿ò´°¿Ú¶ÔÏó´¦Àí¡£ ÏÂÃæ£¬ÌÖÂÛ¼¸¸öMFCÀา¸ÇOnCmdMsgÐéÄ⺯ÊýʱµÄʵÏÖ¡£ÕâЩÀàµÄOnCmdMsg»òÕß¿ÉÄÜÊDZê×¼MFCÃüÁîÏûϢ·¾¶µÄÒ»¸ö»·½Ú£¬»òÕß¿ÉÄÜÊÇÒ»¸ö¶ÀÁ¢µÄ´¦Àí¹ý³Ì£¨¶ÔÓÚÆäÖеÄMFC´°¿ÚÀࣩ¡£ ´Ó·ÖÎöCViewµÄOnCmdMsgʵÏÖ¿ªÊ¼¡£

?

CViewµÄOnCmdMsg

CView::OnCmdMsg(UINT nID, int nCode, void* pExtra,

AFX_CMDHANDLERINFO* pHandlerInfo)

Ê×ÏÈ£¬µ÷ÓÃCWnd::OnCmdMsg£¬½á¹ûÊÇËÑË÷µ±Ç°ÊÓµÄÀàºÍ»ùÀàµÄÏûÏ¢Ó³ÉäÊý×飬ËÑË÷˳ÐòÊÇ´Óϲ㵽Éϲ㡣Èôijһ²ãʵÏÖÁ˶ÔÃüÁîÏûÏ¢nIDµÄ´¦Àí£¬Ôòµ÷ÓÃËüµÄʵÏÖº¯Êý£»·ñÔò£¬µ÷ÓÃm_pDocument->OnCmdMsg£¬°ÑÃüÁîÏûÏ¢Ë͸øÎĵµÀà´¦Àí¡£m_pDocumentÊǺ͵±Ç°ÊÓ¹ØÁªµÄÎĵµ¶ÔÏóÖ¸Õë¡£Èç¹ûÎĵµ¶ÔÏóÀàʵÏÖÁËOnCmdMsg£¬Ôòµ÷ÓÃËüµÄ¸²¸Çº¯Êý£»·ñÔò£¬µ÷ÓûùÀà(ÀýÈçCDocument)µÄOnCmdMsg¡£ ½Ó×Å£¬ÌÖÂÛCDocumentµÄʵÏÖ¡£

?

CDocumentµÄ OnCmdMsg

BOOL CDocument::OnCmdMsg(UINT nID, int nCode, void* pExtra,

AFX_CMDHANDLERINFO* pHandlerInfo)

Ê×ÏÈ£¬µ÷ÓÃCCmdTarget::OnCmdMsg£¬µ¼Öµ±Ç°¶ÔÏó(this)µÄÀàºÍ»ùÀàµÄÏûÏ¢Ó³ÉäÊý×é±»ËÑË÷£¬¿´ÊÇ·ñÓжÔÓ¦µÄÏûÏ¢´¦Àíº¯Êý¿ÉÓá£Èç¹ûÓУ¬¾Íµ÷ÓÃËü£»Èç¹ûû

ÓУ¬Ôòµ÷ÓÃÎĵµÄ£°åµÄOnCmdMsgº¯Êý£¨m_pTemplate->OnCmdMsg£©°ÑÏûÏ¢Ë͸øÎĵµÄ£°å´¦Àí¡£

MFCÎĵµÄ£°åûÓи²¸ÇOnCmdMsg£¬µ¼Ö»ùÀàCCmdTargetµÄOnCmdMsg±»µ÷Ó㬿´ÊÇ·ñÓÐÎĵµÄ£°åÀà»ò»ùÀàʵÏÖÁ˶ÔÏûÏ¢µÄ´¦Àí¡£Êǵϰ£¬µ÷ÓöÔÓ¦µÄÏûÏ¢´¦Àíº¯Êý£¬·ñÔò£¬·µ»ØFALSE¡£´ÓÇ°ÃæµÄ·ÖÎöÖªµÀ£¬CCmdTargetÀàµÄÏûÏ¢Ó³ÉäÊý×éÊǿյģ¬ËùÒÔÕâÀï·µ»ØFALSE¡£

?

CDialogµÄOnCmdMsg

BOOL CDialog::OnCmdMsg(UINT nID, int nCode, void* pExtra,

AFX_CMDHANDLERINFO* pHandlerInfo)

1. µ÷ÓÃCWnd::OnCmdMsg£¬ÈöԻ°¿ò»òÆä»ùÀà´¦ÀíÏûÏ¢¡£

2. Èç¹û»¹Ã»Óд¦Àí£¬¶øÇÒÊÇ¿ØÖÆÏûÏ¢»òϵͳÃüÁî»ò·ÇÃüÁî°´Å¥£¬Ôò·µ»ØFALSE£¬²»×÷½øÒ»²½´¦Àí¡£·ñÔò£¬µ÷Óø¸´°¿ÚµÄ

OnCmdmsg(GetParent()->OnCmdmsg)°ÑÏûÏ¢Ë͸ø¸¸´°¿Ú´¦Àí¡£ 3. Èç¹ûÈÔȻûÓд¦Àí£¬Ôòµ÷Óõ±Ç°Ï̵߳Ä

OnCmdMsg(GetThread()->OnCmdMsg)°ÑÏûÏ¢Ë͸øÏ̶߳ÔÏó´¦Àí¡£ 4. Èç¹û×îºóûÓд¦Àí£¬·µ»ØFALSE¡£

?

CMDIFrameWndµÄOnCmdMsg

¶ÔÓÚMDIÓ¦ÓóÌÐò£¬MDIÖ÷±ß¿ò´°¿ÚÊ×ÏÈÊǰÑÃüÁîÏûÏ¢·¢Ë͸ø»î¶¯µÄMDIÎĵµ±ß¿ò´°¿Ú½øÐд¦Àí¡£MDIÖ÷±ß¿ò´°¿Ú¶ÔOnCmdMsgµÄʵÏÖº¯ÊýµÄÔ­ÐÍÈçÏ£º BOOL CMDIFrameWnd::OnCmdMsg(UINT nID, int nCode, void* pExtra,

AFX_CMDHANDLERINFO* pHandlerInfo)

1. Èç¹ûÓ줻îµÄÎĵµ±ß¿ò´°¿Ú£¬Ôòµ÷ÓÃËüµÄOnCmdMsg(MDIGetActive£¨£©->OnCmdMsg)°ÑÏûÏ¢½»¸øËü½øÐд¦Àí¡£MFCµÄÎĵµ±ß¿ò´°¿ÚÀಢûÓи²¸ÇOnCmdMsgº¯Êý£¬ËùÒÔ»ùÀàCFrameWndµÄº¯Êý±»µ÷Ó㬵¼ÖÂÎĵµ±ß¿ò´°¿ÚµÄ»î¶¯ÊÓ¡¢Îĵµ±ß¿ò´°¿Ú±¾Éí¡¢Ó¦ÓóÌÐò¶ÔÏóÒÀ´ÎÀ´½øÐÐÏûÏ¢´¦Àí¡£ 2. Èç¹ûÎĵµ±ß¿ò´°¿ÚûÓд¦Àí£¬µ÷ÓÃCFrameWnd::OnCmdMsg°ÑÏûÏ¢°´±ê׼·¾¶·¢ËÍ£¬Öظ´µÚÒ»´ÎµÄ²½Ö裬²»¹ý¶ÔÓÚMDI±ß¿ò´°¿ÚÀ´Ëµ²»´æÔڻÊÓ£¬ËùÒÔʡȴÁËÈÃÊÓ´¦ÀíÏûÏ¢µÄ±ØÒª£»½Ó×ÅÈÃMDI±ß¿ò´°¿Ú±¾ÉíÀ´´¦ÀíÏûÏ¢£¬Èç¹ûËü»¹Ã»Óд¦Àí£¬ÔòÈÃÓ¦ÓóÌÐò¶ÔÏó½øÐÐÏûÏ¢´¦Àí©¤©¤ËäÈ»ÕâÊÇÒ»¸öÎÞÓõÄÖØ¸´¡£ ³ýÁËCView¡¢CDocumentºÍCMDIFrameWndÀ࣬»¹Óм¸¸öOLEÏà¹ØµÄÀา¸ÇÁËOnCmdMsgº¯Êý¡£OLEµÄ´¦Àí±¾ÊéÔݲ»Éæ¼°£¬CDialog::OnCmdMsg½«ÔÚ¶Ô»°¿òÕ½ÚרÏîÌÖÂÛÆä¾ßÌåʵÏÖ¡£

1. һЩÏûÏ¢´¦ÀíÀàµÄOnCommandµÄʵÏÖ

³ýÁËÐéÄ⺯ÊýOnCmdMsg£¬»¹ÓÐÒ»¸öÐéÄ⺯ÊýOnCommandÔÚÃüÁîÏûÏ¢µÄ·¢ËÍÖÐÕ¼ÓÐÖØÒªµØÎ»¡£ÔÚ´¦ÀíÃüÁî»òÕß֪ͨÏûϢʱ£¬OnCommand±»MFC´°¿Ú¹ý³Ìµ÷Óã¬È»ºóËüµ÷ÓÃOnCmdMsg°´Ò»¶¨Â·¾¶´«ËÍÏûÏ¢¡£³ýÁËCWndÀàºÍһЩOLEÏà¹ØÀàÍ⣬MFCÀïÖ÷Òª»¹ÓÐMDI±ß¿ò´°¿ÚʵÏÖÁËOnCommand¡£

BOOL CMDIFrameWnd::OnCommand(WPARAM wParam, LPARAM lParam)

µÚÒ»£¬Èç¹û´æÔڻµÄÎĵµ±ß¿ò´°¿Ú£¬ÔòʹÓÃAfxCallWndProcµ÷ÓÃËüµÄ´°¿Ú¹ý³Ì£¬°ÑÏûÏ¢Ë͸øÎĵµ±ß¿ò´°¿ÚÀ´´¦Àí¡£Õ⽫µ¼ÖÂÎĵµ±ß¿ò´°¿ÚµÄOnCmdMsg×÷ÈçϵĴ¦Àí£º

»î¶¯ÊÓ´¦ÀíÏûÏ¢¡úÓëÊÓ¹ØÁªµÄÎĵµ´¦ÀíÏûÏ¢¡ú±¾Îĵµ±ß¿ò´°¿Ú´¦ÀíÏûÏ¢¡úÓ¦ÓóÌÐò¶ÔÏó´¦ÀíÏûÏ¢¡úÎĵµ±ß¿ò´°¿Úȱʡ´¦Àí

ÈκÎÒ»¸ö»·½ÚÈç¹û´¦ÀíÏûÏ¢£¬Ôò²»ÔÙÏòÏ·¢ËÍÏûÏ¢£¬´¦ÀíÖÕÖ¹¡£Èç¹ûÏûÏ¢ÈÔȻûÓб»´¦Àí£¬¾ÍÖ»Óн»¸øÖ÷±ß¿ò´°¿ÚÁË¡£

µÚ¶þ£¬µÚÒ»²½Ã»Óд¦ÀíÃüÁ¼ÌÐøµ÷ÓÃCFrameWnd::OnCommand£¬½«µ¼ÖÂ

CMDIFrameWndµÄOnCmdMsg±»µ÷ÓᣴÓÇ°ÃæµÄ·ÖÎöÖªµÀ£¬½«ÔٴΰÑÏûÏ¢Ë͸øMDI±ß¿ò´°¿ÚµÄ»î¶¯Îĵµ±ß¿ò´°¿Ú£¬µÚÒ»²½µÄ¹ý³Ì³ýÁËÎĵµ±ß¿ò´°¿Úȱʡ´¦ÀíÍâ¶¼½«±»Öظ´¡£¾ßÌåµÄ´¦Àí¹ý³Ì¼ûǰÎĵÄCMDIFrameWnd::OnCmdMsgº¯Êý¡£ 3. ¶ÔÓÚMDIÏûÏ¢£¬Èç¹ûÖ÷±ß¿ò´°¿Ú»¹²»´¦ÀíµÄ»°£¬½»¸øCMDIFrameWndµÄDefWindowProc×÷ȱʡ´¦Àí¡£ 4. ÏûϢûÓд¦Àí£¬·µ»ØFALSE¡£

ÉÏÊö·ÖÎö×ÛºÏÁËOnCommandºÍOnCmdMsgµÄ´¦Àí£¬ËüÃÇÊÇÔÚMFCÄÚ²¿MDI±ß¿ò´°¿Ú´¦ÀíÃüÁîÏûÏ¢µÄÍêÕûµÄÁ÷³ÌºÍ±ê×¼µÄ²½Öè¡£Õû¸ö´¦Àí¹ý³ÌÔٴαíÃ÷Á˱߿ò´°¿ÚÔÚ´¦ÀíÃüÁîÏûϢʱµÄÖÐÐÄ×÷ÓᣴӳÌÐòÔ±µÄ½Ç¶ÈÀ´¿´£¬¿ÉÒÔÈÏΪÕû¸ö±ê×¼´¦Àí·¾¶ÈçÏ£º

»î¶¯ÊÓ´¦ÀíÏûÏ¢¡úÓëÊÓ¹ØÁªµÄÎĵµ´¦ÀíÏûÏ¢¡ú±¾Îĵµ±ß¿ò´°¿Ú´¦ÀíÏûÏ¢¡úÓ¦ÓóÌÐò¶ÔÏó´¦ÀíÏûÏ¢¡úÎĵµ±ß¿ò´°¿Úȱʡ´¦Àí¡úMDI±ß¿ò´°¿Ú´¦ÀíÏûÏ¢¡úMDI±ß¿ò´°¿Úȱʡ´¦Àí

ÈκÎÒ»¸ö»·½ÚÈç¹û´¦ÀíÏûÏ¢£¬²»ÔÙÏòÏ·¢ËÍÏûÏ¢£¬¼±´¦ÀíÖÕÖ¹¡£

1. ¶Ô¿ØÖÆÍ¨ÖªÏûÏ¢µÄ½ÓÊպʹ¦Àí

1. WM_COMMAND¿ØÖÆÍ¨ÖªÏûÏ¢µÄ´¦Àí

WM_COMMAND¿ØÖÆÍ¨ÖªÏûÏ¢µÄ´¦ÀíºÍWM_COMMANDÃüÁîÏûÏ¢µÄ´¦ÀíÀàËÆ£¬µ«ÊÇÒ²Óв»Í¬Ö®´¦¡£

Ê×ÏÈ£¬·ÖÎö´¦ÀíWM_COMMAND¿ØÖÆÍ¨ÖªÏûÏ¢ºÍÃüÁîÏûÏ¢µÄÏàËÆ´¦¡£ÈçǰËùÊö£¬ÃüÁîÏûÏ¢ºÍ¿ØÖÆÍ¨ÖªÏûÏ¢¶¼ÊÇÓÉ´°¿Ú¹ý³Ì¸øOnCommand´¦Àí£¨²Î¼ûCWnd::OnWndMsgµÄʵÏÖ£©£¬OnCommandͨ¹ýwParamºÍlParam²ÎÊýÇø·ÖÊÇÃüÁîÏûÏ¢»ò֪ͨÏûÏ¢£¬È»ºóË͸øOnCmdMsg´¦Àí£¨²Î¼ûCWnd::OnCommndµÄʵÏÖ£©¡£ Æä´Î£¬Á½ÕߵIJ»Í¬Ö®´¦ÊÇ£º

ÃüÁîÏûÏ¢Ò»°ãÊÇË͸øÖ÷±ß¿ò´°¿ÚµÄ£¬Õâʱ£¬±ß¿ò´°¿ÚµÄOnCmdMsg±»µ÷Óã»¶ø¿ØÖÆÍ¨ÖªÏûÏ¢Ë͸ø¿ØÖÆ×Ó´°¿ÚµÄ¸¸´°¿Ú£¬Õâʱ£¬¸¸´°¿ÚµÄOnCmdMsg±»µ÷Óá£

? OnCmdMsg´¦ÀíÃüÁîÏûϢʱ£¬Í¨¹ýÃüÁî·Ö·¢¿ÉÒÔÓɶàÖÖÃüÁîÄ¿±ê´¦Àí£¬°üÀ¨·Ç´°¿Ú¶ÔÏóÈçÎĵµ¶ÔÏóµÈ£»¶ø´¦Àí¿ØÖÆÍ¨ÖªÏûϢʱ£¬²»»áÓÐÏûÏ¢·Ö·¢µÄ¹ý³Ì£¬¿ØÖÆÍ¨ÖªÏûÏ¢×îÖտ϶¨ÊÇÓÉ´°¿Ú¶ÔÏó´¦ÀíµÄ¡£

?

²»¹ý£¬ÔÚijÖ̶ֳÈÉÏ¿ÉÒÔ˵£¬¿ØÖÆÍ¨ÖªÏûÏ¢ÓÉ´°¿Ú¶ÔÏó´¦ÀíÊÇÒ»ÖÖϰ¹ßºÍÔ¼¶¨¡£µ±Ê¹ÓÃClassWizard½øÐÐÏûÏ¢Ó³Éäʱ£¬Ëü²»Ìṩ°Ñ¿ØÖÆÍ¨ÖªÏûÏ¢Ó³Éäµ½·Ç´°¿Ú¶ÔÏóµÄ»ú»á¡£µ«ÊÇ£¬ÊÖ¹¤µØÌí¼ÓÏûÏ¢Ó³É䣬È÷Ǵ°¿Ú¶ÔÏó´¦Àí¿ØÖÆÍ¨ÖªÏûÏ¢µÄ¿ÉÄÜÊÇ´æÔڵġ£ÀýÈ磬¶ÔÓÚCFormView£¬Ò»·½ÃæËü¾ß±¸½ÓÊÜWM_COMMAND֪ͨÏûÏ¢µÄÌõ¼þ£¬ÁíÒ»·½Ã棬¾ß±¸°ÑWM_COMMANDÏûÏ¢ÅÉ·¢¸ø¹ØÁªÎĵµ¶ÔÏó´¦ÀíµÄÄÜÁ¦£¬ËùÒÔ¸øCFormViewµÄ֪ͨÏûÏ¢ÊÇ¿ÉÒÔÈÃÎĵµ¶ÔÏó´¦ÀíµÄ¡£

ÊÂʵÉÏ£¬BN_CLICKED¿ØÖÆÍ¨ÖªÏûÏ¢µÄ´¦ÀíºÍÃüÁîÏûÏ¢µÄ´¦ÀíÍêȫһÑù£¬ÒòΪ¸ÃÏûÏ¢µÄ֪ͨ´úÂëÊÇ0£¬ON_BN_CLICKED(id£¬memberfunction)ºÍON_COMMAND(id£¬memberfunction)ÊǵÈͬµÄ¡£

´ËÍ⣬MFCµÄ״̬¸üд¦Àí»úÖÆ¾ÍÊǽ¨Á¢ÔÚ֪ͨÏûÏ¢¿ÉÒÔ·¢Ë͸ø¸÷ÖÖÃüÁîÄ¿±êµÄ»ù´¡Ö®Éϵġ£¹ØÓÚMFCµÄ״̬¸üд¦Àí»úÖÆ£¬¼ûºóÃæ4.4.4.4½ÚµÄÌÖÂÛ¡£

?

¿ØÖÆÍ¨ÖªÏûÏ¢¿ÉÒÔ·´É䏸×Ó´°¿Ú´¦Àí¡£OnCommandÅж¨µ±Ç°ÏûÏ¢ÊÇ

WM_COMAND֪ͨÏûÏ¢Ö®ºó£¬Ê×ÏÈËü°ÑÏûÏ¢·´É䏸¿ØÖÆ×Ó´°¿Ú´¦Àí£¬Èç¹û×Ó´°¿Ú´¦ÀíÁË·´ÉäÏûÏ¢£¬OnCommand²»»á¼ÌÐøµ÷ÓÃOnCmdMsgÈø¸´°¿Ú¶ÔÏóÀ´´¦Àí֪ͨÏûÏ¢¡£

1. WM_NOTIFYÏûÏ¢¼°Æä´¦Àí:

£¨1£©WM_NOTIFYÏûÏ¢

»¹ÓÐÒ»ÖÖ֪ͨÏûÏ¢WM_NOTIFY£¬ÔÚWin32ÖÐÓÃÀ´´«µÝÐÅÏ¢¸´ÔÓµÄ֪ͨÏûÏ¢¡£WM_NOTIFYÏûÏ¢ÔõôÀ´´«µÝ¸´ÔÓµÄÐÅÏ¢ÄØ£¿WM_NOTIFYµÄÏûÏ¢²ÎÊýwParam°üº¬ÁË·¢ËÍ֪ͨÏûÏ¢µÄ¿ØÖÆ´°¿ÚID£¬ÁíÒ»¸ö²ÎÊýlParam°üº¬ÁËÒ»¸öÖ¸Õë¡£¸ÃÖ¸ÕëÖ¸ÏòÒ»¸öNMHDR½á¹¹£¬»òÕ߸ü´óµÄ½á¹¹£¬Ö»ÒªËüµÄµÚÒ»¸ö½á¹¹³ÉÔ±ÊÇNMHDR½á¹¹¡£ NMHDR½á¹¹£º

typedef struct tagNMHDR {

HWND hwndFrom; UINT idFrom; UINT code; } NMHDR;

ÉÏÊö½á¹¹ÓÐÈý¸ö³ÉÔ±£¬·Ö±ðÊÇ·¢ËÍ֪ͨÏûÏ¢µÄ¿ØÖÆ´°¿ÚµÄ¾ä±ú¡¢IDºÍ֪ͨÏûÏ¢´úÂë¡£

¾ÙÒ»¸ö¸ü´ó¡¢¸ü¸´ÔӵĽṹÀý×Ó£ºÁÐ±í¿ØÖÆ´°·¢ËÍLVN_KEYDOWN¿ØÖÆÍ¨ÖªÏûÏ¢£¬ÔòlParam°üº¬ÁËÒ»¸öÖ¸ÏòLV_KEYDOWN½á¹¹µÄÖ¸Õë¡£Æä½á¹¹ÈçÏ£º typedef struct tagLV_KEYDOWN { NMHDR hdr; WORD wVKey; UINT flags; }LV_KEYDOWN;

ËüµÄµÚÒ»¸ö½á¹¹³ÉÔ±hdr¾ÍÊÇNMHDRÀàÐÍ¡£ÆäËû³ÉÔ±°üº¬Á˸ü¶àµÄÐÅÏ¢£ºÄĸö¼ü±»°´Ï£¬ÄÄЩ¸¨Öú¼ü(SHIFT¡¢CTRL¡¢ALTµÈ)±»°´Ï¡£

£¨2£©WM_NOTIFYÏûÏ¢µÄ´¦Àí

ÔÚ·ÖÎöCWnd::OnWndMsgº¯Êýʱ£¬ÔøÖ¸³öµ±ÏûÏ¢ÊÇWM_NOTIFYʱ£¬Ëü°ÑÏûÏ¢´«µÝ¸øOnNotifyÐéÄ⺯Êý´¦Àí¡£ÕâÊÇÒ»¸öÐéÄ⺯Êý£¬ÀàËÆÓÚOnCommand£¬CWndºÍÅÉÉúÀà¶¼¿ÉÒÔ¸²¸Ç¸Ãº¯Êý¡£OnNotifyµÄº¯ÊýÔ­ÐÍÈçÏ£º

BOOL CWnd::OnNotify(WPARAM, LPARAM lParam, LRESULT* pResult)

²ÎÊý1ÊÇ·¢ËÍ֪ͨÏûÏ¢µÄ¿ØÖÆ´°¿ÚID£¬Ã»Óб»Ê¹Ó㻲ÎÊý2ÊÇÒ»¸öÖ¸Õ룻²ÎÊý3Ö¸ÏòÒ»¸ölongÀàÐ͵ÄÊý¾Ý£¬ÓÃÀ´·µ»Ø´¦Àí½á¹û¡£ WM_NOTIFYÏûÏ¢µÄ´¦Àí¹ý³ÌÈçÏ£º

1. ·´ÉäÏûÏ¢¸ø¿ØÖÆ×Ó´°¿Ú´¦Àí¡£

2. Èç¹û×Ó´°¿Ú²»´¦Àí·´ÉäÏûÏ¢£¬Ôò½»¸øOnCmdMsg´¦Àí¡£¸øOnCmdMsgµÄËĸö²ÎÊý·Ö±ðÈçÏ£ºµÚÒ»¸öÊÇÃüÁîÏûÏ¢ID£¬µÚËĸöΪ¿Õ£»µÚ¶þ¸ö¸ß½×wordÊÇWM_NOTIFY£¬µÍ½×wordÊÇ֪ͨÏûÏ¢£»µÚÈý¸ö²ÎÊýÊÇÖ¸ÏòAFX_NOTIFY½á¹¹µÄÖ¸Õë¡£µÚ¶þ¡¢Èý¸ö²ÎÊýÓбðÓÚOnCommandË͸øOnCmdMsgµÄ²ÎÊý¡£

AFX_NOTIFY½á¹¹£º struct AFX_NOTIFY {

LRESULT* pResult; NMHDR* pNMHDR; };

pNMHDRµÄÖµÀ´Ô´ÓÚ²ÎÊý2 lParam£¬¸Ã½á¹¹µÄÓòpResultÓÃÀ´±£´æ´¦Àí½á¹û£¬ÓòpNMHDRÓÃÀ´´«µÝÐÅÏ¢¡£

OnCmdMsgºóÐøµÄ´¦ÀíºÍWM_COMMAND֪ͨÏûÏ¢»ù±¾Ïàͬ£¬Ö»ÊÇÔÚÅÉ·¢ÏûÏ¢¸øÏûÏ¢´¦Àíº¯Êýʱ£¬DispatchMsdMsgµÄµÚÎå¸ö²ÎÊýpExtraÖ¸ÏòOnCmdMsg´«µÝ¸øËüµÄAFX_NOTIFYÀàÐ͵IJÎÊý£¬¶ø²»ÊÇ¿ÕÖ¸Õë¡£ÕâÑù£¬´¦Àíº¯Êý¾ÍµÃµ½Á˸´ÔÓµÄ֪ͨÏûÏ¢ÐÅÏ¢¡£

1. ÏûÏ¢·´Éä

£¨1£©ÏûÏ¢·´ÉäµÄ¸ÅÄî

Ç°ÃæÌÖÂÛ¿ØÖÆÍ¨ÖªÏûϢʱ£¬Ôø¾­¶à´ÎÌáµ½ÁËÏûÏ¢·´Éä¡£MFCÌṩÁËÁ½ÖÖÏûÏ¢·´Éä»úÖÆ£¬Ò»ÖÖÓÃÓÚOLE¿Ø¼þ£¬Ò»ÖÖÓÃÓÚWindows¿ØÖÆ´°¿Ú¡£ÕâÀïÖ»ÌÖÂÛºóÒ»ÖÖÏûÏ¢·´Éä¡£

Windows¿ØÖƳ£³£·¢ËÍ֪ͨÏûÏ¢¸øËüÃǵĸ¸´°¿Ú£¬Í¨³£¿ØÖÆÏûÏ¢Óɸ¸´°¿Ú´¦Àí¡£µ«ÊÇÔÚMFCÀïÍ·£¬¸¸´°¿ÚÔÚÊÕµ½ÕâЩÏûÏ¢ºó£¬»òÕß×Ô¼º´¦Àí£¬»òÕß·´ÉäÕâЩÏûÏ¢¸ø¿ØÖÆ´°¿Ú×Ô¼º´¦Àí£¬»òÕßÁ½Õß¶¼½øÐд¦Àí¡£Èç¹û³ÌÐòÔ±ÔÚ¸¸´°¿ÚÀา¸ÇÁË֪ͨÏûÏ¢µÄ´¦Àí£¨¼Ù¶¨²»µ÷ÓûùÀàµÄʵÏÖ£©£¬ÏûÏ¢½«²»»á·´É䏸¿ØÖÆ×Ó´°¿Ú¡£ÕâÖÖ·´Éä»úÖÆÊÇMFCʵÏֵ쬱ãÓÚ³ÌÐòÔ±´´½¨¿ÉÖØÓõĿØÖÆ´°¿ÚÀà¡£

MFCµÄCWndÀà´¦ÀíÒÔÏ¿ØÖÆÍ¨ÖªÏûϢʱ£¬±ØÒª»òÕß¿ÉÄܵϰ£¬°ÑËüÃÇ·´É䏸×Ó´°¿Ú´¦Àí£º WM_CTLCOLOR£¬

WM_VSCROLL£¬WM_HSCROLL£¬ WM_DRAWITEM£¬WM_MEASUREITEM£¬ WM_COMPAREITEM£¬WM_DELETEITEM£¬ WM_CHARTOITEM£¬WM_VKEYTOITEM£¬

WM_COMMAND¡¢WM_NOTIFY¡£

ÀýÈ磬¶ÔWM_VSCROLL¡¢WM_HSCROLLÏûÏ¢µÄ´¦Àí£¬ÆäÏûÏ¢´¦Àíº¯ÊýÈçÏ£º void CWnd::OnHScroll(UINT, UINT, CScrollBar* pScrollBar) {

//Èç¹ûÊÇÒ»¸ö¹ö¶¯Ìõ¿ØÖÆ£¬Ê×ÏÈ·´ÉäÏûÏ¢¸øËü´¦Àí

if (pScrollBar != NULL && pScrollBar->SendChildNotifyLastMsg())

return; //¿ØÖÆ´°¿Ú³É¹¦´¦ÀíÁ˸ÃÏûÏ¢

Default(); }

ÓÖÈ磺ÔÚÌÖÂÛOnCommandºÍOnNofityº¯Êý´¦Àí֪ͨÏûϢʱ£¬¶¼Ôø¾­Ö¸³ö£¬ËüÃÇÊ×Ïȵ÷ÓÃReflectLastMsg°ÑÏûÏ¢·´É䏸¿ØÖÆ´°¿Ú´¦Àí¡£

ΪÁËÀûÓÃÏûÏ¢·´ÉäµÄ¹¦ÄÜ£¬Ê×ÏÈÐèÒª´ÓÊʵ±µÄMFC´°¿ÚÅÉÉú³öÒ»¸ö¿ØÖÆ´°¿ÚÀ࣬ȻºóʹÓÃClassWizard¸øËüÌí¼ÓÏûÏ¢Ó³ÉäÌõÄ¿£¬Ö¸¶¨Ëü´¦Àí¸ÐÐËȤµÄ·´ÉäÏûÏ¢¡£ÏÂÃæ£¬ÌÖÂÛ·´ÉäÏûÏ¢Ó³Éäºê¡£

ÉÏÊöÏûÏ¢µÄ·´ÉäÏûÏ¢Ó³ÉäºêµÄÃüÃû×ñÑ­ÒÔϸñʽ£º¡°ON¡±Ç°×º+ÏûÏ¢Ãû+¡°REFLECT¡±ºó׺£¬ÀýÈ磺ÏûÏ¢WM_VSCROLLµÄ·´ÉäÏûÏ¢Ó³ÉäºêÊÇ

ON_WM_VSCROLL_REFECT¡£µ«ÊÇ֪ͨÏûÏ¢WM_COMMANDºÍWM_NOTIFYÊÇÀýÍ⣬·Ö±ðΪON_CONTROL_REFLECTºÍON_NOFITY_REFLECT¡£×´Ì¬¸üÐÂ֪ͨÏûÏ¢µÄ·´ÉäÏûÏ¢Ó³ÉäºêÊÇON_UPDATE_COMMAND_UI_REFLECT¡£

ÏûÏ¢´¦Àíº¯ÊýµÄÃû×ÖºÍÈ¥µô¡°WM_¡±Ç°×ºµÄÏûÏ¢ÃûÏàͬ £¬ÀýÈçWM_HSCROLL·´ÉäÏûÏ¢´¦Àíº¯ÊýÊÇHscroll¡£

ÏûÏ¢´¦Àíº¯ÊýµÄÔ­ÐÍÕâÀï²»Ò»Ò»ÁоÙÁË¡£

ÕâЩÏûÏ¢Ó³ÉäºêºÍÏûÏ¢´¦Àíº¯ÊýµÄÔ­ÐÍ¿ÉÒÔ½èÖúÓÚClassWizard×Ô¶¯µØÌí¼Óµ½³ÌÐòÖС£ClassWizardÌí¼ÓÏûÏ¢´¦Àíº¯Êýʱ£¬¿ÉÒÔ´¦ÀíµÄ·´ÉäÏûÏ¢Ç°ÃæÓÐÒ»¸öµÈºÅ£¬ÀýÈç´¦ÀíWM_HSCROLLµÄ·´ÉäÏûÏ¢£¬Ñ¡ÔñÓ³ÉäÏûÏ¢¡°=EN_HSC ROLL¡±¡£ClassWizard×Ô¶¯µÄÌí¼ÓÏûÏ¢Ó³ÉäºêºÍ´¦Àíº¯Êýµ½¿ò¼ÜÎļþ¡£ £¨2£©ÏûÏ¢·´ÉäµÄ´¦Àí¹ý³Ì

Èç¹û²»¿¼ÂÇÓÐOLE¿Ø¼þµÄÇé¿ö£¬ÏûÏ¢·´ÉäµÄ´¦ÀíÁ÷³ÌÈçÏÂͼËùʾ£º

Ê×ÏÈ£¬µ÷ÓÃCWndµÄ³ÉÔ±º¯ÊýSendChildNotifyLastMsg£¬Ëü´ÓÏß³Ì״̬µÃµ½±¾Ïß³Ì×î½üÒ»´Î»ñÈ¡µÄÏûÏ¢£¨¹ØÓÚÏß³Ì״̬£¬ºóÃæµÚ9Õ»áÏêϸ½éÉÜ£©ºÍÏûÏ¢²ÎÊý£¬²¢ÇÒ°ÑÕâЩ²ÎÊý´«µÝ¸øº¯ÊýOnChildNotify¡£×¢Ò⣬µ±Ç°µÄCWnd¶ÔÏó¾ÍÊÇMFC¿ØÖÆ×Ó´°¿Ú¶ÔÏó¡£

OnChlidNofifyÊÇCWnd¶¨ÒåµÄÐéÄ⺯Êý£¬²»¿¼ÂÇOLE¿ØÖƵϰ£¬Ëü½ö½öÖ»µ÷ÓÃReflectChildNotify¡£OnChlidNofify¿ÉÒÔ±»¸²¸Ç£¬ËùÒÔÈç¹û³ÌÐòԱϣÍû´¦Àíij¸ö¿ØÖƵÄ֪ͨÏûÏ¢£¬³ýÁ˲ÉÓÃÏûÏ¢Ó³ÉäµÄ·½·¨´¦Àí֪ͨ·´ÉäÏûÏ¢ÒÔÍ⣬»¹¿ÉÒÔ¸²¸ÇOnChlidNotifyÐéÄ⺯Êý£¬Èç¹û³É¹¦µØ´¦ÀíÁË֪ͨÏûÏ¢£¬Ôò·µ»ØTRUE¡£ ReflectChildNotifyÊÇCWndµÄ³ÉÔ±º¯Êý£¬Íê³É·´ÉäÏûÏ¢µÄÅÉ·¢¡£¶ÔÓÚWM_COMMAND£¬ËüÖ±½Óµ÷ÓÃCWnd::OnCmdMsgÅÉ·¢·´ÉäÏûÏ¢

WM_REFLECT_BASE+WM_COMMAND£»¶ÔÓÚWM_NOTIFY£¬ËüÖ±½Óµ÷ÓÃCWnd::OnCmdMsgÅÉ·¢·´ÉäÏûÏ¢WM_REFLECT_BASE+WM_NOFITY£»¶ÔÓÚÆäËûÏûÏ¢£¬ÔòÖ±½Óµ÷ÓÃCWnd::OnWndMsg£¨¼´CmdTarge::OnWndMsg£©ÅÉ·¢ÏàÓ¦µÄ·´ÉäÏûÏ¢£¬ÀýÈçWM_REFLECT_BASE+WM_HSCROLL¡£

×¢Ò⣺ReflectChildNotifyÖ±½Óµ÷ÓÃÁËCWndµÄOnCmdMsg»òOnWndMsg£¬ÕâÑù·´ÉäÏûÏ¢±»Ö±½ÓÅÉ·¢¸ø¿ØÖÆ×Ó´°¿Ú£¬Ê¡È´ÁËÏûÏ¢·¢Ë͵Ĺý³Ì¡£

½Ó×Å£¬¿ØÖÆ×Ó´°¿ÚÈç¹û´¦ÀíÁ˵±Ç°µÄ·´ÉäÏûÏ¢£¬Ôò·µ»Ø·´ÉäÏûÏ¢±»³ÉÔ±´¦ÀíµÄÐÅÏ¢¡£

£¨3£©Ò»¸öʾÀý

Èç¹ûÒª´´½¨Ò»¸ö±à¼­¿ò¿ØÖÆ£¬ÒªÇóËü±³¾°Ê¹ÓûÆÉ«£¬ÆäËûÌØÐÔ²»±ä£¬Ôò¿ÉÒÔ´ÓCEditÅÉÉúÒ»¸öÀàCYellowEdit£¬´¦Àí֪ͨÏûÏ¢WM_CTLCOLORµÄ·´ÉäÏûÏ¢¡£CYellowEditÓÐÈý¸öÊôÐÔ£¬¶¨ÒåÈçÏ£º CYellowEdit::CYellowEdit() {

m_clrText = RGB( 0, 0, 0 ); m_clrBkgnd = RGB( 255, 255, 0 );

m_brBkgnd.CreateSolidBrush( m_clrBkgnd ); }

ʹÓÃClassWizardÌí¼Ó·´ÉäÏûÏ¢´¦Àíº¯Êý£º º¯ÊýÔ­ÐÍ£º

afx_msg void HScroll(); ÏûÏ¢Ó³Éäºê£º

ON_WM_CTLCOLOR_REFLECT() º¯ÊýµÄ¿ò¼Ü

HBRUSH CYellowEdit::CtlColor(CDC* pDC, UINT nCtlColor) {

// TODO:Ìí¼Ó´úÂë¸Ä±äÉ豸ÃèÊö±íµÄÊôÐÔ

// TODO: Èç¹û²»ÔÙµ÷Óø¸´°¿ÚµÄ´¦Àí£¬Ôò·µ»ØÒ»¸ö·Ç¿ÕµÄË¢×Ó¾ä±ú return NULL; }

Ìí¼ÓһЩ´¦Àíµ½º¯ÊýCtlColorÖУ¬ÈçÏ£º pDC->SetTextColor( m_clrText );//ÉèÖÃÎı¾ÑÕÉ« pDC->SetBkColor( m_clrBkgnd );//ÉèÖñ³¾°ÑÕÉ« return m_brBkgnd; //·µ»Ø±³¾°Ë¢

ÕâÑù£¬Èç¹ûij¸öµØ·½ÐèҪʹÓûÆÉ«±³¾°µÄ±à¼­¿ò£¬Ôò¿ÉÒÔʹÓÃCYellowEdit¿ØÖÆ¡£

1. ¶Ô¸üÐÂÃüÁîµÄ½ÓÊպʹ¦Àí

Óû§½Ó¿Ú¶ÔÏóÈç²Ëµ¥¡¢¹¤¾ßÌõÓжàÖÖ״̬£¬ÀýÈ磺½ûÖ¹£¬¿É

Óã¬Ñ¡ÖУ¬Î´Ñ¡ÖУ¬µÈµÈ¡£ÕâÐ©×´Ì¬Ëæ×ÅÔËÐÐÌõ¼þµÄ±ä»¯£¬ÓɳÌÐòÀ´½øÐиüС£ËäÈ»³ÌÐòÔ±¿ÉÒÔ×Ô¼ºÀ´Íê³É¸üУ¬µ«ÊÇMFC¿ò¼ÜΪ×Ô¶¯¸üÐÂÓû§½Ó¿Ú¶ÔÏóÌṩÁËÒ»¸ö·½±ãµÄ½Ó¿Ú£¬Ê¹ÓÃËü¶Ô³ÌÐòÔ±À´Ëµ¿ÉÄÜÊÇÒ»¸öºÃµÄÑ¡Ôñ¡£ 1. ʵÏÖ·½·¨

ÿһ¸öÓû§½Ó¿Ú¶ÔÏó£¬Èç²Ëµ¥¡¢¹¤¾ßÌõ¡¢¿ØÖÆ´°¿ÚµÄ×Ó´°¿Ú£¬¶¼ÓÉΨһµÄIDºÅ±êʶ£¬Óû§ºÍËüÃǽ»»¥Ê±£¬

²úÉúÏàÓ¦IDºÅµÄÃüÁîÏûÏ¢¡£ÔÚMFCÀһ¸öÓû§½Ó¿Ú¶ÔÏ󻹿ÉÒÔÏìÓ¦CN_UPDATE_COMMAND_UI֪ͨÏûÏ¢¡£Òò´Ë£¬¶Ôÿ¸ö±êºÅIDµÄ½Ó¿Ú¶ÔÏ󣬿ÉÒÔÓÐÁ½¸ö´¦Àíº¯Êý£ºÒ»¸öÏûÏ¢´¦Àíº¯ÊýÓÃÀ´´¦Àí¸Ã¶ÔÏó²úÉúµÄÃüÁîÏûÏ¢ID£¬ÁíÒ»¸ö״̬¸üк¯ÊýÓÃÀ´´¦Àí¸ø¸Ã¶ÔÏóµÄCN_UPDATE_COMMAND_UIDµÄ֪ͨÏûÏ¢¡£ ʹÓÃClassWizard¿É°Ñ״̬¸üк¯Êý¼ÓÈ뵽ij¸öÏûÏ¢´¦ÀíÀ࣬Æä½á¹ûÊÇ£º

ÔÚÀàµÄ¶¨ÒåÖÐÉùÃ÷Ò»¸ö״̬º¯Êý£»

ÔÚÏûÏ¢Ó³ÉäÖÐʹÓÃON_UPDATE_COMMAND_UIºêÌí¼ÓÒ»¸öÓ³ÉäÌõÄ¿£»

ÔÚÀàµÄʵÏÖÎļþÖÐʵÏÖ״̬¸üк¯ÊýµÄ¶¨Òå¡£ ON_UPDATE_COMMAND_UI¸øÖ¸¶¨IDµÄÓû§¶ÔÏóÖ¸¶¨×´Ì¬¸üк¯Êý£¬ÀýÈ磺

ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)

Ó³Éä±êʶºÅIDΪID_EDIT_COPY²Ëµ¥µÄ֪ͨÏûÏ¢CN_UPDATE_COMMAND_UIµ½º¯ÊýOnUpdateEditCopy¡£ÓÃÓÚ¸øEDIT£¨±à¼­²Ëµ¥£©µÄ²Ëµ¥ÏîID_EDIT_COPY£¨¸´ÖÆ£©Ìí¼ÓÒ»¸ö״̬´¦Àíº¯ÊýOnUpdateEditCopy£¬Í¨¹ý´¦Àí֪ͨÏûÏ¢CN_UPDATE_COMMAND_UIʵÏָò˵¥ÏîµÄ״̬¸üС£

״̬´¦Àíº¯ÊýµÄÔ­ÐÍÈçÏ£º

afxmsg void

ClassName::OnUpdateEditPaste(CCmdUI* pCmdUI) CCmdUI¶ÔÏóÓÉMFC×Ô¶¯µØ¹¹Ôì¡£ÔÚÍêÉÆº¯ÊýµÄʵÏÖʱ£¬Ê¹ÓÃpCmdUI¶ÔÏóºÍCmdUIµÄ³ÉÔ±º¯ÊýʵÏֲ˵¥ÏîID_EDIT_COPYµÄ״̬¸üУ¬ÈÃËü±ä»Ò»òÕß±äÁÁ£¬Ò²¾ÍÊǽûÖ¹»òÕßÔÊÐíÓû§Ê¹Óøò˵¥Ïî¡£ 2. ״̬¸üÐÂÃüÁîÏûÏ¢

ÒªÌÖÂÛMFCµÄ״̬¸üд¦Àí£¬ÏȵÃÁ˽âÒ»ÌõÌØÊâµÄÏûÏ¢¡£MFCµÄÏûÏ¢Ó³Éä»úÖÆ³ýÁË´¦Àí¸÷ÖÖWindowsÏûÏ¢¡¢¿ØÖÆÍ¨ÖªÏûÏ¢¡¢ÃüÁîÏûÏ¢¡¢·´ÉäÏûÏ¢Í⣬»¹´¦ÀíÒ»ÖÖ

ÌØ±ðµÄ¡°Í¨ÖªÃüÁîÏûÏ¢¡±£¬²¢Í¨¹ýËüÀ´¸üв˵¥¡¢¹¤¾ßÀ¸£¨°üÀ¨¶Ô»°¿ò¹¤¾ßÀ¸£©µÈÃüÁîÄ¿±êµÄ״̬¡£ ÕâÖÖ¡°Í¨ÖªÃüÁîÏûÏ¢¡±ÊÇMFCÄÚ²¿¶¨ÒåµÄ£¬ÏûÏ¢IDÊÇWM_COMMAND£¬Í¨Öª´úÂëÊÇCN_UPDATE_COMMAND_UI£¨0XFFFFFFFF£©¡£

Ëü²»ÊÇÒ»¸öÕæÕýÒâÒåÉϵÄ֪ͨÏûÏ¢£¬ÒòΪûÓпØÖÆ´°¿Ú²úÉúÕâÑùµÄ֪ͨÏûÏ¢£¬¶øÊÇMFC×Ô¼ºÖ÷¶¯²úÉú£¬ÓÃÓÚË͸ø¹¤¾ßÌõ´°¿Ú»òÕßÖ÷±ß¿ò´°¿Ú£¬Í¨ÖªËüÃǸüÐÂÓû§½Ó¿Ú¶ÔÏóµÄ״̬¡£

ËüºÍ±ê×¼WM_COMMANDÃüÁîÏûÏ¢Ò²²»Ïàͬ£¬ÒòΪËüÓÐÌØ¶¨µÄ֪ͨ´úÂ룬¶øÃüÁîÏûϢ֪ͨ´úÂëÊÇ0¡£ µ«ÊÇ£¬´ÓÏûÏ¢µÄ´¦Àí½Ç¶È£¬¿ÉÒÔ°ÑËü¿´×÷ÊÇÒ»Ìõ֪ͨÏûÏ¢¡£Èç¹ûÊǹ¤¾ßÌõ´°¿Ú½ÓÊÕ¸ÃÏûÏ¢£¬ÔòÔÚ·¢ËÍ»úÖÆÉÏËüºÍWM_COMMAND¿ØÖÆÍ¨ÖªÏûÏ¢ÊÇÏàͬµÄ£¬Ï൱ÓÚÈù¤¾ßÌõ´°¿Ú´¦ÀíÒ»Ìõ֪ͨÏûÏ¢¡£Èç¹ûÊDZ߿ò´°¿Ú½ÓÊÕ¸ÃÏûÏ¢£¬ÔòÔÚÏûÏ¢µÄ·¢ËÍ»úÖÆÉÏËüºÍWM_COMMANDÃüÁîÏûÏ¢ÊÇÏàͬµÄ£¬¿ÉÒÔÈÃÈÎÒâÃüÁîÄ¿±ê´¦Àí¸ÃÏûÏ¢£¬Ò²¾ÍÊÇ˵±ß¿ò´°¿Ú¿ÉÒ԰ѸÃÌõ֪ͨÏûÏ¢·¢Ë͸øÈÎÒâÃüÁîÄ¿±ê´¦Àí¡£

´Ó³ÌÐòÔ±µÄ½Ç¶È£¬¿ÉÒÔ°ÑËü¿´×÷Ò»Ìõ¡°×´Ì¬¸üÐÂÃüÁîÏûÏ¢¡±£¬Ïñ´¦ÀíÃüÁîÏûÏ¢ÄÇÑù´¦Àí¸ÃÏûÏ¢¡£Ã¿ÌõÃüÁîÏûÏ¢¶¼¿ÉÒÔ¶ÔÓ¦ÓÐÒ»Ìõ¡°×´Ì¬¸üÐÂÃüÁîÏûÏ¢¡±¡£ClassWizardÒ²Ö§³ÖÈÃÈÎÒâÏûϢĿ±ê´¦Àí¡°×´Ì¬¸üÐÂÃüÁîÏûÏ¢¡±£¨°üÀ¨·Ç´°¿ÚÃüÁîÄ¿±ê£©£¬ÊµÏÖÓû§½Ó¿Ú״̬µÄ¸üС£

ÔÚÕâÌõÏûÏ¢·¢ËÍʱ£¬Í¨¹ýOnCmdMsgµÄµÚÈý¸ö²ÎÊýpExtra´«µÝһЩÐÅÏ¢£¬±íʾҪ¸üеÄÓû§½Ó¿Ú¶ÔÏó¡£pExtraÖ¸ÏòÒ»¸öCCmdUI¶ÔÏó¡£ÕâЩÐÅÏ¢½«´«µÝ¸ø×´Ì¬¸üÐÂÃüÁîÏûÏ¢µÄ´¦Àíº¯Êý¡£

ÏÂÃæÌÖÂÛÓÃÓÚ¸üÐÂÓû§½Ó¿Ú¶ÔÏó״̬µÄÀàCCmdUI¡£ 3. ÀàCCmdUI

CCmdUI²»ÊÇ´ÓCObjectÅÉÉú£¬Ã»ÓлùÀà¡£ 1. ³ÉÔ±±äÁ¿

m_nID Óû§½Ó¿Ú¶ÔÏóµÄID

m_nIndex Óû§½Ó¿Ú¶ÔÏóµÄindex m_pMenu Ö¸ÏòCCmdUI¶ÔÏó±íʾµÄ²Ëµ¥ m_pSubMenu Ö¸ÏòCCmdUI¶ÔÏó±íʾµÄ×Ӳ˵¥ m_pOther Ö¸ÏòÆäËû·¢ËÍ֪ͨÏûÏ¢µÄ´°¿Ú¶ÔÏó m_pParentMenu Ö¸ÏòCCmdUI¶ÔÏó±íʾµÄ×Ӳ˵¥ 2. ³ÉÔ±º¯Êý

Enable(BOOL bOn = TRUE ) ½ûÖ¹Óû§½Ó¿Ú¶ÔÏó»òÕßʹ֮¿ÉÓà SetCheck( int nCheck = 1) ±ê¼ÇÓû§½Ó¿Ú¶ÔÏóÑ¡ÖлòδѡÖÐ SetRadio(BOOL bOn = TRUE) SetText(LPCTSTR lpszText) ContinueRouting()

»¹ÓÐÒ»¸öMFCÄÚ²¿Ê¹ÓõijÉÔ±º¯Êý£º

DoUpdate(CCmdTarget* pTarget, BOOL bDisableIfNoHndler)

ÆäÖУ¬²ÎÊý1Ö¸Ïò´¦Àí½ÓÊÕ¸üÐÂ֪ͨµÄÃüÁîÄ¿±ê£¬Ò»°ãÊDZ߿ò´°¿Ú£»²ÎÊý2ָʾÈç¹ûûÓÐÌṩ´¦Àíº¯Êý£¨ÀýÈçij¸ö²Ëµ¥Ã»ÓжÔÓ¦µÄÃüÁî´¦Àíº¯Êý£©£¬ÊÇ·ñ½ûÖ¹Óû§¶ÔÏó¡£

DoUpdate×÷ÒÔÏÂÊÂÇ飺

Ê×ÏÈ£¬·¢ËÍ״̬¸üÐÂÃüÁîÏûÏ¢¸ø²ÎÊý1±íʾµÄÃüÁîÄ¿±ê£ºµ÷ÓÃpTarget->OnCmdMsg£¨m_nID, CN_UPDATE_COMMAND_UI, this, NULL)·¢ËÍm_nID¶ÔÏóµÄ֪ͨÏûÏ¢CN_UPDATE_COMMAND_UI¡£OnCmdMsgµÄ²ÎÊý3ȡֵthis£¬°üº¬Á˵±Ç°Òª¸üеÄÓû§½Ó¿Ú¶ÔÏóµÄÐÅÏ¢¡£

È»ºó£¬Èç¹û²ÎÊý2ΪTRUE£¬µ÷ÓÃpTarget->OnCmdMsg(m_nID, CN_COMMAND, this, &info)²âÊÔÃüÁîÏûÏ¢m_nIDÊÇ·ñ±»´¦Àí¡£Õâʱ£¬OnCmdMsgµÄµÚËĸö²ÎÊý·Ç¿Õ£¬±íʾ½ö½öÊDzâÊÔ£¬²»ÊÇÕæµÄÒªÅÉ·¢ÏûÏ¢¡£Èç¹ûûÓÐÌṩÃüÁîÏûÏ¢m_nIDµÄ´¦Àíº¯Êý£¬Ôò½ûÖ¹Óû§¶ÔÏóm_nID£¬·ñÔòʹ֮¿ÉÓá£

´ÓÉÏÃæµÄÌÖÂÛ¿ÉÒÔÖªµÀ£ºÍ¨¹ýÆä½á¹¹£¬Ò»¸öCCmdUI¶ÔÏó±êʶËü±íʾÁËÄÄÒ»¸öÓû§½Ó¿Ú¶ÔÏó£¬Èç¹ûÊDz˵¥½Ó¿Ú¶ÔÏó£¬pMenu±íʾÁËÒª¸üеIJ˵¥¶ÔÏó£»Èç¹ûÊǹ¤¾ßÌõ£¬pOther±íʾÁËÒª¸üÐµĹ¤¾ßÌõ´°¿Ú¶ÔÏó£¬nID±íʾÁ˹¤¾ßÌõ°´Å¥ID¡£

ËùÒÔ£¬ÓɲÎÊýÉÏ״̬¸üÐÂÏûÏ¢µÄÏûÏ¢´¦Àíº¯Êý¾ÍÖªµÀÒª¸üÐÂʲô½Ó¿Ú¶ÔÏóµÄ״̬¡£ÀýÈ磬µÚ1½ÚµÄº¯ÊýOnUpdateEditPaste£¬º¯Êý²ÎÊýpCmdUI±íʾһ¸ö²Ëµ¥¶ÔÏó£¬ÐèÒª¸üиò˵¥¶ÔÏóµÄ״̬¡£

ͨ¹ýÆä³ÉÔ±º¯Êý£¬Ò»¸öCCmdUI¿ÉÒÔ¸üС¢¸Ä±äÓû§½Ó¿Ú¶ÔÏóµÄ״̬¡£ÀýÈ磬CCmdUI¿ÉÒÔ¹ÜÀí²Ëµ¥ºÍ¶Ô»°¿ò¿ØÖƵÄ״̬£¬µ÷ÓÃEnable½ûÖ¹»òÕßÔÊÐí²Ëµ¥»òÕß¿ØÖÆ×Ó´°¿Ú£¬µÈµÈ¡£

ËùÒÔ£¬º¯ÊýOnUpdateEditPaste¿ÉÒÔÖ±½Óµ÷ÓòÎÊýµÄ³ÉÔ±º¯Êý£¨ÈçpCmdUI->Enable£©ÊµÏֲ˵¥¶ÔÏóµÄ״̬¸üС£

ÓÉÓÚ½Ó¿Ú¶ÔÏóµÄ¶àÑùÐÔ£¬ÆäËû½Ó¿Ú¶ÔÏ󽫴ÓCCmdUIÅÉÉú³ö¹ÜÀí×Ô¼ºµÄÀàÀ´£¬¸²¸Ç»ùÀàµÄÓйسÉÔ±º¯ÊýÈçEnableµÈ£¬Ìṩ¶Ô×ÔÉí״̬¸üÐµĹ¦ÄÜ¡£ÀýÈç¹ÜÀí״̬ÌõºÍ¹¤¾ßÀ¸¸üеÄCStatusCmdUIÀàºÍCToolCmdUIÀà¡£

1. ×Ô¶¯¸üÐÂÓû§½Ó¿Ú¶ÔÏó״̬µÄ»úÖÆ

MFCÌṩÁË·Ö±ðÓÃÓÚ¸üв˵¥ºÍ¹¤¾ßÌõµÄÁ½ÖÖ;¾¶¡£ 1. ¸üв˵¥×´Ì¬

µ±Óû§¶Ô²Ëµ¥ÈçFileµ¥»÷Êó±êʱ£¬¾Í²úÉúÒ»ÌõWM_INITMENUPOPUPÏûÏ¢£¬±ß¿ò´°¿ÚÔڲ˵¥ÏÂÀ­Ö®Ç°ÏìÓ¦¸ÃÏûÏ¢£¬´Ó¶ø¸üиò˵¥ËùÓÐÏîµÄ״̬¡£ ÔÚÓ¦ÓóÌÐò¿ªÊ¼ÔËÐÐʱ£¬±ß¿òÒ²»áÊÕµ½WM_INITMENUPOPUPÏûÏ¢¡£ 2. ¸üй¤¾ßÌõµÈ״̬

µ±Ó¦ÓóÌÐò½øÈë¿ÕÏд¦Àí״̬ʱ£¬½«·¢ËÍWM_IDLEUPDATECMDUIÏûÏ¢£¬µ¼ÖÂËùÓеŤ¾ßÌõÓû§¶ÔÏóµÄ״̬´¦Àíº¯Êý±»µ÷Ó㬴Ӷø¸Ä±äÆä״̬¡£WM_IDLEUPDATECMDUIÊÇMFC×Ô¼º¶¨ÒåºÍʹÓõÄÏûÏ¢¡£

ÔÚ´°¿Ú³õʼ»¯Ê±£¬¹¤¾ßÌõÒ²»áÊÕµ½WM_IDLEUPDATECMDUIÏûÏ¢¡£ 3. ²Ëµ¥×´Ì¬¸üеÄʵÏÖ

MFCÈñ߿ò´°¿ÚÀ´ÏìÓ¦WM_INITMENUPOPUPÏûÏ¢£¬ÏûÏ¢´¦Àíº¯ÊýÊÇOnInitMenuPopup£¬ÆäÔ­ÐÍÈçÏ£º

afx_msg void CFrameWnd::OnInitMenuPopup( CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu );

µÚÒ»¸ö²ÎÊýÖ¸ÏòÒ»¸öCMenu¶ÔÏó£¬Êǵ±Ç°°´»÷µÄ²Ëµ¥£»µÚ¶þ¸ö²ÎÊýÊDz˵¥Ë÷Òý£»µÚÈý¸ö²ÎÊý±íʾ×Ӳ˵¥ÊÇ·ñÊÇϵͳ¿ØÖƲ˵¥¡£

º¯ÊýµÄ´¦Àí£º

Èç¹ûÊÇϵͳ¿ØÖƲ˵¥£¬²»×÷´¦Àí£»·ñÔò£¬´´½¨CCmdUI¶ÔÏóstate£¬¸øËüµÄ¸÷¸ö³ÉÔ±Èç

m_pMenu£¬m_pParentMenu£¬m_pOtherµÈ¸³Öµ¡£ ¶Ô¸Ã²Ëµ¥µÄ¸÷¸ö²Ëµ¥Ïµ÷º¯Êýstate.DoUpdate£¬ÓÃCCmdUIµÄDoUpdateÀ´¸üÐÂ״̬¡£DoUpdateµÄµÚÒ»¸ö²ÎÊýÊÇthis£¬±íʾÃüÁîÄ¿±êÊDZ߿ò´°¿Ú£»ÔÚCFrameWndµÄ³ÉÔ±±äÁ¿m_bAutoMenuEnableΪTRUEʱ£¨±íʾÈç¹û²Ëµ¥m_nIDûÓжÔÓ¦µÄÏûÏ¢´¦Àíº¯Êý»ò״̬¸üк¯Êý£¬Ôò½ûÖ¹Ëü£©£¬°ÑDoUpdateµÄµÚ¶þ¸ö²ÎÊýbDisableIfNoHndlerÖÃΪTRUE¡£ ˳±ãÖ¸³ö£¬m_bAutoMenuEnableȱʡʱΪTRUE£¬ËùÒÔ£¬Ó¦ÓóÌÐòÆô¶¯Ê±²Ëµ¥¾­¹ý³õʼ»¯´¦Àí£¬Ã»ÓÐÌṩÏûÏ¢´¦Àíº¯Êý»ò״̬¸üк¯ÊýµÄ²Ëµ¥Ïî±»½ûÖ¹¡£

4. ¹¤¾ßÌõµÈ״̬¸üеÄʵÏÖ

ͼ4-5±íʾÁËÏûÏ¢¿ÕÏÐʱMFC¸üÐÂÓû§¶ÔÏó״̬µÄÁ÷³Ì£º

MFCÌṩµÄȱʡ¿ÕÏд¦ÀíÏò¶¥²ã´°¿Ú(¿ò¼Ü´°¿Ú)µÄËùÓÐ×Ó´°¿Ú·¢ËÍÏûÏ¢

WM_IDLEUPDATECMDUI£»MFCµÄ¿ØÖÆ´°¿Ú£¨¹¤¾ßÌõ¡¢×´Ì¬À¸µÈ£©ÊµÏÖÁ˶ԸÃÏûÏ¢µÄ´¦Àí£¬µ¼ÖÂÓû§¶ÔÏó״̬´¦Àíº¯ÊýµÄµ÷Óá£

ËäÈ»Á½ÖÖ;¾¶µ÷ÓÃÁËͬһ״̬´¦Àíº¯Êý£¬µ«ÊÇ´«µÝµÄ CCmdUI²ÎÊý´ÓÄÚ²¿¹¹³ÉÉÏÊDz»Ò»ÑùµÄ£ºµÚÒ»ÖÖ´«µÝµÄCCmdUI¶ÔÏó±íʾÁËÒ»²Ëµ¥¶ÔÏó£¬(pMenuÓò±»¸³Öµ)£»µÚ¶þÖÖ´«µÝÁËÒ»¸ö´°¿Ú¶ÔÏó(pOtherÓò±»¸³Öµ)¡£Í¬ÑùµÄ״̬¸Ä±ä¶¯×÷£¬Èç½ûÖ¹¡¢ÔÊÐí״̬µÄ¸Ä±ä£¬Ç°Õßµ÷ÓÃÁËCMenuµÄ³ÉÔ±º¯ÊýEnableMenuItem£¬ºóÕßʹÓÃÁËCWndµÄ³ÉÔ±º¯ÊýEnabelWindow¡£µ«ÊÇ£¬ÕâЩ²»Í¬ÓÉCCmdUI¶ÔÏóÄÚ²¿Çø·Ö¡¢´¦Àí£¬¶ÔÓû§ÊÇ͸Ã÷µÄ£º²»Â۲˵¥»¹ÊǶÔÓ¦µÄ¹¤¾ßÌõ£¬Óû§¶¼ÓÃͬһ¸ö״̬´¦Àíº¯ÊýʹÓÃͬÑùµÄÐÎʽÀ´´¦Àí¡£

ÕâÒ»½Ú·ÖÎöÁËÓû§½çÃæ¸üеÄÔ­ÀíºÍ»úÖÆ¡£ÔÚºóÃæµÚ13ÕÂÌÖÂÛ¹¤¾ßÌõºÍ״̬À¸Ê±£¬½«ÏêϸµÄ·ÖÎöÕâÖÖ»úÖÆµÄ¾ßÌåʵÏÖ¡£

1. ÏûÏ¢µÄÔ¤´¦Àí

µ½ÏÖÔÚΪֹ£¬ÏêϸµÄÌÖÂÛÁËMFCµÄÏûÏ¢Ó³Éä»úÖÆ¡£µ«ÊÇ£¬ÎªÁËÌá¸ßЧÂʺͼò»¯´¦Àí£¬MFCÌṩÁËÒ»ÖÖÏûÏ¢Ô¤´¦Àí»úÖÆ£¬Èç¹ûÒ»ÌõÏûÏ¢

ÔÚÔ¤´¦Àíʱ±»¹ýÂ˵ôÁË£¨±»´¦Àí£©£¬Ôò²»»á±»ÅÉ·¢¸øÄ¿µÄ´°¿ÚµÄ´°¿Ú¹ý³Ì£¬¸ü²»»á½øÈëÏûϢѭ»·ÁË¡£

ÏÔÈ»£¬Äܹ»½øÐÐÔ¤´¦ÀíµÄÏûÏ¢Ö»¿ÉÄÜÊǶÓÁÐÏûÏ¢£¬¶øÇÒ±ØÐëÔÚÏûÏ¢ÅÉ·¢Ö®Ç°½øÐÐÔ¤´¦Àí¡£Òò´Ë£¬MFCÔÚʵÏÖÏûϢѭ»·Ê±£¬¶ÔÓڵõ½µÄÿһÌõÏûÏ¢£¬Ê×ÏÈË͸øÄ¿µÄ´°¿Ú¡¢Æä¸¸´°¿Ú¡¢Æä׿¸¸´°¿ÚÄËÖÁ×î¶¥²ã¸¸´°¿Ú£¬ÒÀ´Î½øÐÐÔ¤´¦Àí£¬Èç¹ûûÓб»´¦Àí£¬Ôò½øÐÐÏûϢת»»ºÍÏûÏ¢ÅÉ·¢£¬Èç¹ûij¸ö´°¿ÚʵÏÖÁËÔ¤´¦Àí£¬ÔòÖÕÖ¹¡£ÓйØÊµÏÖ¼ûºóÃæ¹ØÓÚCWinThreadÏß³ÌÀàµÄÕ½ڣ¬CWinThreadµÄRunº¯ÊýºÍPreTranslateMessageº¯ÊýÒÔ¼°CWndµÄº¯Êý

WalkPreTranslateTreeʵÏÖÁËÉÏÊöÒªÇóºÍ¹¦ÄÜ¡£ÕâÀïÒªÌÖÂÛµÄÊÇMFC´°¿ÚÀàÈçºÎ½øÐÐÏûÏ¢Ô¤´¦Àí¡£

CWndÌṩÁËÐéÄ⺯ÊýPreTranslateMessageÀ´½øÐÐÏûÏ¢Ô¤´¦Àí¡£CWndµÄÅÉÉúÀà¿ÉÒÔ¸²¸Ç¸Ãº¯Êý£¬ÊµÏÖ×Ô¼ºµÄÔ¤´¦Àí¡£ÏÂÃæ£¬ÌÖÂÛ¼¸¸öµäÐ͵ÄÔ¤´¦Àí¡£ Ê×ÏÈ£¬ÊÇCWndµÄÔ¤´¦Àí£º Ô¤´¦Àíº¯ÊýµÄÔ­ÐÍΪ£º

BOOL CWnd::PreTranslateMessage(MSG* pMsg)

CWndÀàÖ÷ÒªÊÇ´¦ÀíºÍ¹ýÂËTooltipsÏûÏ¢¡£¹ØÓڸú¯ÊýµÄʵÏÖºÍTooltipsÏûÏ¢£¬¼ûºóÃæµÚ13Õ¹ØÓÚ¹¤¾ßÀ¸µÄÌÖÂÛ¡£ È»ºó£¬ÊÇCFrameWndµÄÔ¤´¦Àí£º

CFrameWnd³ýÁ˵÷ÓûùÀàCWndµÄʵÏÖ¹ýÂËTooltipsÏûÏ¢Ö®Í⣬»¹ÒªÅжϵ±Ç°ÏûÏ¢ÊÇ·ñÊǼüÅÌ¿ì½Ý¼ü±»°´Ï£¬Èç¹ûÊÇ£¬Ôòµ÷Óú¯

Êý::TranslateAccelerator(m_hWnd, hAccel, pMsg)´¦Àí¿ì½Ý¼ü¡£ ½Ó×Å£¬ÊÇCMDIChildWndµÄÔ¤´¦Àí£º

CMDIChildWndµÄÔ¤´¦Àí¹ý³ÌºÍCFrameWndµÄÒ»Ñù£¬µ«ÊDz»ÄÜÒÀ¿¿»ùÀàCFrameWndµÄʵÏÖ£¬±ØÐ븲¸ÇËü¡£ÒòΪMDI×Ó´°¿ÚûÓв˵¥£¬ËùÒÔËü±ØÐëÔÚMDI±ß¿ò´°¿ÚµÄÉÏÏÂÎÄÖÐÀ´´¦Àí¿ì½Ý¼ü£¬Ëüµ÷ÓÃÁ˺¯Êý::TranslateAccelerator(GetMDIFrame()->m_hWnd, hAccel, pMsg)¡£

ÌÖÂÛÁËMDI×Ó´°¿ÚµÄÔ¤´¦Àíºó£¬»¹ÒªÌÖÂÛMDI±ß¿ò´°¿Ú£º CMDIFrameWndµÄʵÏÖ³ýÁËCFrameWndµÄʵÏֵŦÄÜÍ⣬Ëü»¹Òª´¦ÀíMDI¿ì½Ý¼ü£¨±ê×¼MDI½çÃæÍ³Ò»Ê¹ÓõÄϵͳ¿ì½Ý¼ü£©¡£

ÔÚºóÃæ£¬»¹»áÌÖÂÛCDialog¡¢CFormView¡¢CToolBarµÈµÄÏûÏ¢Ô¤´¦Àí¼°ÆäʵÏÖ¡£

ÖÁÓÚCWnd::WalkPreTranslateTreeº¯Êý£¬Ëü´Ó½ÓÊÜÏûÏ¢µÄ´°¿Ú¿ªÊ¼£¬Öð¼¶Ïò¸¸´°»ØËÝ£¬ÖðÒ»¶Ô¸÷²ã´°¿Úµ÷ÓÃPreTranslateMessageº¯Êý£¬Ö±µ½ÏûÏ¢±»´¦Àí»òÕßµ½×î¶¥²ã´°¿ÚΪֹ¡£ 2. MFCÏûÏ¢Ó³ÉäµÄ»Ø¹Ë

´Ó´¦ÀíÃüÁîÏûÏ¢µÄ¹ý³Ì¿ÉÒÔ¿´³ö£¬WindowsÏûÏ¢ºÍ¿ØÖÆÏûÏ¢µÄ´¦ÀíÒª±ÈÃüÁîÏûÏ¢µÄ´¦Àí¼òµ¥£¬ÒòΪ²éÕÒÏûÏ¢´¦Àíº¯Êýʱ£¬ºóÕßÖ»ÒªËÑË÷µ±Ç°´°¿Ú¶ÔÏó(thisËùÖ¸)µÄÀà»òÆä»ùÀàµÄÏûÏ¢Ó³ÉäÈë¿Ú±í¡£µ«ÊÇ£¬ÃüÁîÏûÏ¢¾ÍÒª¸´ÔÓ¶àÁË£¬ËüÑØÒ»¶¨µÄ˳ÐòÁ´²éÕÒÁ´Éϵĸ÷¸öÃüÁîÄ¿±ê£¬Ã¿Ò»¸ö±»²éÕÒµÄÃüÁîÄ¿±ê¶¼ÒªËÑË÷ËüµÄÀà»ò»ùÀàµÄÏûÏ¢Ó³ÉäÈë¿Ú±í¡£

MFCͨ¹ýÏûÏ¢Ó³ÉäµÄÊֶΣ¬ÒÔÒ»ÖÖÀàËÆC++ÐéÄ⺯ÊýµÄ¸ÅÄîÏò³ÌÐòÔ±ÌṩÁËÒ»ÖÖ´¦ÀíÏûÏ¢µÄ·½Ê½¡£µ«ÊÇ£¬ÈôʹÓÃC++ÐéÄ⺯ÊýʵÏÖÖÚ¶àµÄÏûÏ¢£¬½«µ¼ÖÂÐéÄ⺯Êý±í¼«ÆäÅÓ´ó£»¶øÊ¹ÓÃÏûÏ¢Ó³É䣬Ôò½ö½ö¸ÐÐËȤµÄÏûÏ¢²Å¼ÓÈëÓ³Éä±í£¬ÕâÑù¾ÍÒª½ÚÊ¡×ÊÔ´¡¢Ìá¸ßЧÂÊ¡£ÕâÌ×ÏûÏ¢Ó³Éä»úÖÆµÄ»ù´¡°üÀ¨ÒÔϼ¸¸ö·½Ã棺

1. ÏûÏ¢Ó³ÉäÈë¿Ú±íµÄʵÏÖ£º²ÉÓÃÁËC++¾²Ì¬³ÉÔ±ºÍÐéÄ⺯ÊýµÄ·½·¨À´±íʾºÍµÃµ½Ò»¸öÏûÏ¢Ó³ÉäÀà(CCmdTarget»òÅÉÉúÀà)µÄÓ³Éä±í¡£

2. ÏûÏ¢²éÕÒµÄʵÏÖ£º´ÓµÍ²ãµ½¸ß²ãËÑË÷ÏûÏ¢Ó³ÉäÈë¿Ú±í£¬Ö±ÖÁ¸ùÀàCCmdTarget¡£ 3. ÏûÏ¢·¢Ë͵ÄʵÏÖ£ºÖ÷ÒªÒÔ¼¸¸öÐéÄ⺯ÊýΪ»ù´¡À´ÊµÏÖ±ê×¼MFCÏûÏ¢·¢ËÍ·¾¶£ºOnComamnd¡¢OnNotify¡¢OnWndMsgºÍOnCmdMsg¡£¡¢ OnWndMsgÊÇCWndÀà»òÆäÅÉÉúÀàµÄ³ÉÔ±º¯Êý£¬ÓÉ´°¿Ú¹ý³Ìµ÷Óá£Ëü´¦Àí±ê×¼µÄWindowsÏûÏ¢¡£

OnCommandÊÇCWndÀà»òÆäÅÉÉúÀàµÄ³ÉÔ±º¯Êý£¬ÓÉOnWndMsgµ÷ÓÃÀ´´¦Àí

WM_COMMANDÏûÏ¢£¬ÊµÏÖÃüÁîÏûÏ¢»òÕß¿ØÖÆÍ¨ÖªÏûÏ¢µÄ·¢ËÍ¡£Èç¹ûÅÉÉúÀา¸Ç¸Ãº¯Êý£¬Ôò±ØÐëµ÷ÓûùÀàµÄʵÏÖ£¬·ñÔò½«²»ÄÜ×Ô¶¯µÄ´¦ÀíÃüÁîÏûÏ¢Ó³É䣬¶øÇÒ±ØÐëʹÓøú¯Êý½ÓÊܵIJÎÊý£¨²»ÊdzÌÐòÔ±¸ø¶¨Öµ£©µ÷ÓûùÀàµÄOnCommand¡£ OnNotifyÊÇCWndÀà»òÆäÅÉÉúÀàµÄ³ÉÔ±º¯Êý£¬ÓÉOnWndMsgµ÷ÓÃÀ´´¦ÀíWM_NOTIFYÏûÏ¢£¬ÊµÏÖ¿ØÖÆÍ¨ÖªÏûÏ¢µÄ·¢ËÍ¡£

OnCmdMsgÊÇCCmdTargetÀà»òÆäÅÉÉúÀàµÄ³ÉÔ±º¯Êý¡£±»OnCommandµ÷Óã¬ÓÃÀ´ÊµÏÖÃüÁîÏûÏ¢·¢ËͺÍÅÉ·¢ÃüÁîÏûÏ¢µ½ÃüÁîÏûÏ¢´¦Àíº¯Êý¡£ ×Ô¶¯¸üÐÂÓû§¶ÔÏó״̬ÊÇͨ¹ýMFCµÄÃüÁîÏûÏ¢·¢ËÍ»úÖÆÊµÏֵġ£ ¿ØÖÆÏûÏ¢¿ÉÒÔ·´É䏸¿ØÖÆ´°¿Ú´¦Àí¡£

¶ÓÁÐÏûÏ¢ÔÚ·¢Ë͸ø´°¿Ú¹ý³Ì֮ǰ¿ÉÒÔ½øÐÐÏûÏ¢Ô¤´¦Àí£¬Èç¹ûÏûÏ¢±»MFC´°¿Ú¶ÔÏóÔ¤´¦ÀíÁË£¬Ôò²»»á½øÈëÏûÏ¢·¢Ë͹ý³Ì¡£