Ç°ÑÔ£º
×î½üÔÚ¿´ºî½ÝµÄ¡¶ÉîÈëdz³öMFC¡·£¬ÔÚÀí½âMFCʱ¾õµÃ±ØÐëÒªÕÆÎÕwin32£¬²ÅÄܸüºÃµÄÀí½âMFC£¬²ÅÄÜд³ö¸üÓÐÓõijÌÐò¡£ÓÚÊǽ«×Ô¼º¶Ôwin32³ÌÐòµÄÀí½âдÁËÏÂÀ´£¬¹©ÅóÓÑÃDzο¼¡£ÎÄÖеÄÁ½·ùͼÊÇ´ÓÊéÖнØÈ¡µÄ¡£
ÕýÎÄ£º
ÏÖÔÚÓкܶàÏë×ßVCÕâÌõ·µÄÅóÓÑ£¬Ò»¿ªÊ¼¾ÍÊÇMFC£¬ËäÈ»¿Ð¹ýÒ»¶Îʱ¼äÊéºó£¬ÄÜд³öһЩС³ÌÐò£¬µ«Ô½ÍùºóÄã¾Í»áÔ½¾õµÃÀ§ÄÑ¡£ÎÒµÄÒ»¸öÍøÓÑ×òÌì¸úÎÒÁÄÌìµÄʱºò˵¡°TMD£¬MFCÓõÄÔ½À´Ô½¾õµÃ²»Ë¬£¬ÔçÖªµÀ¿ªÊ¼Ñ§VCµÄʱºòÌýÅóÓÑ֮Ȱ£¬ÏÈѧWin32±à³ÌºÃÁË¡±¡£µÄÈ·£¬MFC°Ñ´ó²¿·Ö¾«»ª¶¼·â×°ÆðÀ´ÁË£¬ÎÒÃÇÖ»ÄÜÓÃËüÏֳɵĿ⣬»òÓÈÆäÅÉÉú³öһЩ×Ô¼ºµÄ¿â¡£Èç¹û²»Á˽âWin32±à³Ì£¬ÎÒÃǾͲ»¶®³ÌÐòµÄÔË×÷ÔÀí£¬ÄÇôÎÒÃÇÈçºÎ²Ù¿Ø³ÌÐò£¬ÎÒÃÇ»¹ÄÄÓÐ×ÔÖ÷Ȩ£¿
Èç¹ûÄã´ÓÀ´Ã»ÓÐÔÚʼþÇý¶¯£¨event driven£©ÏµÍ³ÖÐ׫д¹ýÒÔÏûϢΪ»ù´¡£¨message based£©µÄÓ¦ÓóÌÐò£¬¾ÍÏëÒ»²½¿çÈëMFC£¬Ö±½ÓÓÃApplication wizard¿ª·¢windows³ÌÐò£¬ÎÒ¾õµÃ²»´ó¿ÉÄÜ¡£ËäÈ»Äã¿ÉÒԼ̳ÐMFCÖеÄÀ࣬À´¿ª·¢³öÒ»¸öÆľ߹æÄ£µÄwindowsÓ¦ÓóÌÐò£¬µ«Èç¹ûÄã²»Á˽âwindows³ÌÐòµÄÔË×÷±¾ÖÊ£¨event driven£¬message based £©£¬ÊDz»¿ÉÄܽøÈë¸ßÉî¾³½çµÄ¡£ÕýÈçºî´óʦËùÑÔ£º¡°ÎðÔÚ¸¡É³Öþ¸ß̨¡±£¬²»»á×ßǧÍò±ðÏëÅÜ¡£
ÔÚÕýʽѧϰMFC֮ǰ£¬ÐèÒªÕÆÎյĶ«Î÷£¨¸öÈËÈÏΪ£©ÓУºÁ˽âwindows³ÌÐòµÄʼþÇý¶¯»úÖÆ£¨°üÀ¨ÏûÏ¢µÄ²úÉú¡¢²¶»ñ¡¢·ÖÅɺʹ¦ÀíµÈ£©¡£ÁíÍ⻹ÓÐÒ»¸ö±È½ÏÖØÒªµÄ¾ÍÊÇC++Öжà̬£¨polymorphism£©ºÍÐ麯ÊýµÄÀí½â£¨²»Ëµ¾«Í¨£¬ÆðÂëÒ²µÃ´ïµ½ÊìÁ·£©¡£
ÏÂÃæ¼òµ¥ËµÒ»ÏÂWin32³ÌÐò¿ò¼Ü¡£
Ò»¸öWin32³ÌÐòÊÇÓɳÌÐò´úÂëºÍUI(User Interface)Á½´ó²¿·Ö×é³É£¬µ±ÎÒÃDZ༺ÃÕâÁ½²¿·Öºó£¬ÔÙÓÉRC£¨resource compiler£©±àÒëÆ÷½«ÕâÁ½²¿·ÖÕûºÏ³ÉÒ»¸öEXEÎļþ¡£³ÌÐò´úÂë²»ÓÃ˵ÁË£¬UI×ÊÔ´Ö¸µÄÊÇһЩÈç²Ëµ¥¡¢¶Ô»°¿ò¡¢Î»Í¼¡¢Êó±êÖ¸Õ롢ͼ±êµÈ£¬ÎÒÃDZØÐëÔÚÒ»¸ö.rcÎļþÖÐÃèÊöËüÃÇ¡£
ÁíÍ⣬³ÌÐòÒªÏë³É¹¦±àÒëÔËÐУ¬»¹ÐèÒª¼ÓÈëһЩº¯Êý¿âºÍÍ·Îļþ¡£Windows³ÌÐòµ÷Óõĺ¯Êý¿ÉÒÔ·ÖΪC RuntimesºÍWindows APIÁ½ÖÖ£¬LIBC.LIBÊÇC RuntimesµÄ¾²Ì¬Á¬½Ó°æ£¬MSVCRT.LIBÊÇC RuntimesµÄ¶¯Ì¬Á¬½Ó°æ¡£GDI32.DLL¡¢USER32.DLLºÍKERNEL32.DLLÊÇ32λWindows APIµÄÈý´óº¯Êý¿â¡£ËùÓеÄwindows³ÌÐò¶¼Òª°üº¬windows.h£¬µ«windows.hÖ»°üº¬GDI32.DLL¡¢USER32.DLLºÍKERNEL32.DLLÖеĺ¯Êý£¬Èç¹ûÄ㻹Ïë¼ÓÈë±ðµÄdll£¬¾ÍÐèÒª¼ÓÈëÏàÓ¦µÄÍ·Îļþ¡£ÏÂÃæÊÇwin32³ÌÐò¿ª·¢µÄÁ÷³Ì£º
Windows³ÌÐòµÄÔËÐÐÊÇ¿¿ÍⲿʼþÀ´Çý¶¯µÄ£¬¾ÍÊÇ˵£¬³ÌÐòÒ»Ö±³öÓÚÒ»¸öµÈ´ýµÄ״̬£¬Èç¹ûÓÐÒ»¸öʼþ·¢Éú£¬³ÌÐò¾Í»áÅжÏÊÇʲôʼþ£¬È»ºó×ö³öÏàÓ¦µÄ´¦Àí¡£ÄÇôÿһ¸öwindows³ÌÐò¶¼±ØÐëÓÐÒ»¸ö»Ø·²ÅÄÜʵÏÖÒ»Ö±µÈ´ý¡£Õâ¸ö»Ø·ÈçÏ£º
MSG msg; while( GetMessage( &msg, NULL, 0, 0 ) ){ TranslateMessage( &msg ); DispatchMessage( &msg ); } MSG½á¹¹ÔÚwindowsÄÚ²¿ÊÇÕâÑù¶¨ÒåµÄ£º typedef struct tagMsg{ HWND hWnd; UINT message; WPARAM wParam; LPARAM lParam; DWORD time; POINT pt; |
ÆäÖÐmessage¾ÍÊÇÖ¸¸÷ÖÖÏûÏ¢£¬ÈçWM_MOUSEMOVE¡¢WM_DESTROYµÈ¡£ÄÇôËÀ´½ÓÊÕÕâ¸öÏûÏ¢²¢×ö³öÏàÓ¦´¦ÀíÄØ£¿¾ÍÊÇ´°¿Ú£¬Õâ¾ÍÐèÒªÎÒÃÇΪ´°¿ÚÉè¼ÆÒ»¸öº¯Êý£¬¼´ËùνµÄ´°¿Úº¯Êý¡£´°¿Úº¯ÊýÐÎÈ磺
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch ( message ) { case WM_LBUTTONDOWN: ¡ case WM_MOUSEMOVE: ¡ case WM_DESTROY: PostQuitMessage ( 0 ); default: return DefWindowProc ( hWnd, message, wParam, lParam ); } return ( 0 ); |
CALLBACKÊÇÒ»ÖÖº¯Êýµ÷ÓÃÏ°¹ß£¬±»¶¨ÒåΪ__stdcall£¬ËµÃ÷´Ëº¯ÊýΪ»Øµ÷º¯Êý£¬ÓÉϵͳ×Ô¶¯µ÷Óõģ¬µ±´°¿Ú½ÓÊÕµ½ÏûÏ¢²¢DispatchMessageÖ®ºó£¬ÏµÍ³¾Í×Ô¶¯µ÷Óô°¿Úº¯ÊýWndProcÁË¡£×¢Òâ´°¿Úº¯ÊýÖÐÏûÏ¢µÄ·ÖÖ§½á¹¹ÖÐdefault·ÖÖ§±ØÐëÊÇreturn DefWindowProc ( hWnd, message, wParam, lParam );ÒòΪ²»ÂÛʲôÏûÏ¢¶¼±ØÐë±»´¦Àí£¬DefWindowProcÊÇwindowsÄÚ²¿Ô¤ÉèµÄÏûÏ¢´¦Àíº¯Êý¡£ÒÔÉϾÍÊÇwindows³ÌÐòµÄ¾«ÒªËùÔÚ£¬Åª¶®ÁËÕâЩ£¬²ÅÄÜΪѧMFC×÷ºÃ×¼±¸¡£Win32³ÌÐòµÄÔËÐÐͼ½âÈçÏ£º
ÄÇô´°¿ÚµÄ²úÉúºÍÏÔʾÊÇÔõôʵÏÖµÄÄØ£¿ÏÂÃæÎÒÃǾÍÀ´Éú³ÉÒ»¸ö¼òµ¥µÄwin32SDK´°¿Ú£¬À´¿´Ò»ÏÂwindows³ÌÐòÊÇÈçºÎ°ÑÏûÏ¢»ñÈ¡¡¢·ÖÅɲ¢´¦ÀíµÄ£¬ÒÔ¼°Ð´Ò»¸öwin32´°¿Ú³ÌÐòµÄÖ÷Òª²½Ö裺
Ò»¡¢³ÌÐò½øÈëµã
windows³ÌÐòµÄ½øÈëµãÊÇWinMainº¯Êý£¬ËüÓÐËĸö²ÎÊý£¬ÐÎʽÈçÏ£º
int WINAPI WinMain ( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, |
²ÎÊý˵Ã÷£ºhInst Ϊµ±Ç°ÊµÀý¾ä±ú£¬Windows »·¾³ÏÂÓÃÓÚÇø±ðͬһӦÓóÌÐòµÄ²»Í¬ÊµÀý£»hPrevInstÓ¦ÓóÌÐòÏÈǰʵÀýµÄ¾ä±ú(Èç¹ûÓеĻ°)£¬·ñÔòΪ NULL£¬¿ÉÒÔÓÃÀ´È·¶¨µ±Ç°ÊµÀýÊÇ·ñΪӦÓóÌÐòµÄµÚÒ»¸öʵÀý£»lpCmdLineÊÇÒÔNULL½áβµÄÃüÁîÐÐ×Ö·û´®³¤Ö¸Õ룻nCmdShowÖ¸¶¨´°¿Ú³õʼÏÔʾ·½Ê½µÄÕûÐͳ£Á¿(1 = ͨ³££»7 = ×îС»¯) ¡£
¶þ¡¢×¢²á´°¿ÚÀà
Ò»¸ö´°¿ÚÔÚ½¨Á¢ÒÔÇ°£¬±ØÐë½øÐÐһЩ³õʼ»¯£¬±ÈÈç´°¿ÚµÄ´óС¡¢±êÌâ¼°±ß¿òµÄÑÕÉ«µÈ£¬Íê³ÉÕâЩ¹¤×÷ÎÒÃÇ»¹±ØÐëÒ»¸öAPIº¯ÊýRegisterClassÀ´×¢²á´°¿ÚÀࣺ
WNDCLASS wndclass; //³õʼ»¯´°¿ÚµÄÊôÐÔ ¡¡¡¡¡. //×¢²á´°¿ÚÀà if(!RegisterClass(&wndclass)) { MessageBeep(0); return FALSE; |
Èý¡¢´´½¨´°¿Ú
´°¿ÚÊôÐÔÉèÖúò¢ÇÒ×¢²áÁË´°¿ÚÀàÖ®ºó£¬ÎÒÃǾÍÒª½¨Á¢Ò»¸ö´°¿ÚÁË£¬CreateWindowº¯Êý»áÍê³ÉÕâ¸ö¹¤×÷£¬È磺
hwnd=CreateWindow( lpszClassName, lpszTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, |
½¨Á¢ºÃ´°¿ÚÖ®ºó¾ÍÒªÏÔʾ´°¿ÚÁË£¬ÈçÏ£º
//ÏÔʾ´°¿Ú ShowWindow(hwnd,nCmdShow); |
È»ºó½ø²½ÏûϢѻ·£¬³ÌÐò³öÓÚÒ»Ö±µÈ´ýµÄ״̬£¨³ý·ÇÄãÍ˳ö³ÌÐò£©¡£
//ÏûϢѻ· while(GetMessage(&Msg,NULL,0,0)) { TranslateMessage(&Msg); DispatchMessage(&Msg); |
Èý¡¢´°¿Úº¯Êý
ÕâÊÇ´°¿ÚµÄÉúÃüÖÐÊ࣬ÒòΪËüÊÇ´¦Àí¸÷ÖÖÏûÏ¢µÄµØ·½¡£ÉÏÃæÏûϢѻ·ÖÐDispatchMessage¾ÍÊǽ«ÏûÏ¢·ÖÅɵ½´°¿Ú´¦Àíº¯Êý£¬´°¿Ú´¦Àíº¯ÊýÇ°ÃæÒѾ×÷ÁË˵Ã÷£¬ÕâÀï²»ÔÙ׸Êö¡£
ÕâÑù£¬Ò»¸öwin32´°¿Ú³ÌÐò¾ÍдºÃÁË£¬¾ßÌåÏê¼û¸½¼Ó´úÂë¡£
ºó¼Ç£º
ÕâƪÎÄÕ¸ù¾Ý×Ô¼ºµÄÀí½âдµÄ£¬ÈçÓв»Ç¡µ±Ö®´¦£¬¿ÏÇëÖ¸Õý¡£×îºó£¬Ï£ÍûÕâƪÎÄÕ¶Դó¼ÒÄÜÓеã°ïÖú¡£
ÎÄÕÂÀ´Ô´ÓÚÁì²âÈí¼þ²âÊÔÍø https://www.ltesting.net/