視窗與訊息(Window and Message)

debut.cis.nctu.edu.tw
  • No tags were found...

視窗與訊息(Window and Message)

(Window and Message)


立 child windowCreateWindow 立 識– (,), , , 列 ,


MessageMessage: message OSmessage– 立 理 – Window class 了 理 WndProc( ) function window class 理


MessageLoaderMessage Queue 行 MessageLoop 讀 MSG msg;…while(GetMessage(&msg,NULL,0,0){TranslateMessage(&msg);DispatchMessage(&msg);}message loop


Create window #include LRESULT CALLBACK WndProc( HWND,UINT,WPARAM,LPARAM);int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE …){1. 1. Window Class ClassRegisterClass(&wndclass)2. 2. 立 hwnd=CreateWindow(…)3. 3. Message loop loopMSG msg;…while(GetMessage(&msg,NULL,0,0){TranslateMessage(&msg);DispatchMessage(&msg);}}4. 4. 理 LRESULT CALLBACK WndProc(HWND hwnd, hwnd, …) …)


1. Window ClassWNDCLASS wndclass;wcx.cbSize = sizeof(wcx); // // size size of of structurewcx.style = CS_HREDRAW | | CS_VREDRAW; // // redraw redraw if if size size changeswcx.lpfnWndProc = MainWndProc; // // points points to to window window procedurewcx.cbClsExtra = 0; 0; // // no no extra extra class class memorywcx.cbWndExtra = 0; 0; // // no no extra extra window window memorywcx.hInstance = hinstance; // // handle handle to to instancewcx.hIcon = LoadIcon(NULL,IDI_APPLICATION); // // predefined app. app. icon iconwcx.hCursor = LoadCursor(NULL,IDC_ARROW); // // predefined arrow arrowwcx.hbrBackground =(HBRUSH) GetStockObject(WHITE_BRUSH);wcx.lpszMenuName = NULL; NULL; // // name name of of menu menu resourcewcx.lpszClassName = "MainWClass";RegisterClass(&wndclass) windows class name class name


2. 立 hwnd hwnd = CreateWindow ("MainWClass", // // window window class class name nameTEXT TEXT ("The ("The Hello Hello Program"), // // window caption captionWS_OVERLAPPEDWINDOW, // // window window style styleCW_USEDEFAULT, // // initial initial x positionCW_USEDEFAULT, // // initial initial y positionCW_USEDEFAULT, // // initial initial x size sizeCW_USEDEFAULT, // // initial initial y size sizeNULL, NULL, // // parent parent window handle handleNULL, NULL, // // window menu menu handle handlehInstance, // // program instance handle handleNULL) NULL) ;; // // creation parameters數 WinMainWinMain參 數 ShowWindow (hwnd, (hwnd, iCmdShow) ;;UpdateWindow (hwnd) (hwnd);; windows class class name name來*WS_XXX Window style * CW_ XXX Create Window


理 –WinProc( )LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){HDC hdc ;PAINTSTRUCT ps ;RECT rect ;switch (message) {case WM_CREATE: windowwindow立 ,,PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;return 0 ;case WM_PAINT:hdc = BeginPaint (hwnd, &ps) ;GetClientRect (hwnd, &rect) ; windowwindow,,DrawText (hdc, TEXT ("Hello, Windows 2000!"), -1, &rect,DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;EndPaint (hwnd, &ps) ;return 0 ;}case WM_DESTROY:PostQuitMessage (0) ;return 0 ;}return DefWindowProc (hwnd, message, wParam, lParam) ; windowwindow,,


Getting a Handle on HandlesIdentifier MeaningHINSTANCE Handle to an "instance"—the program itselfHWND Handle to a windowHDC Handle to a device contextHandle – 數– – Windows 來 handle 料 DC


Hungarian Notation ((利 ) 念 Microsoft Charles Simonyi 數 – 料 szCmdLine 0 串hInstance hPrevInstance h HandleiCmdShow i intstructure 數– structure name prefix 數 msg MSG structurecndclass WNDCLASSES structureps PAINTSTRUCT structurerect RECT structure 利 參 料 :http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvsgen/html/hunganotat.asp


錄 : #include #include // // 理 prototypeprototypeLRESULT LRESULT CALLBACK CALLBACK WndProc WndProc (HWND, (HWND, UINT, UINT, WPARAM, WPARAM, LPARAM) LPARAM) ;;// // Windows Windows WinMain(...)int intWINAPI WinMain WinMain (HINSTANCE (HINSTANCE hInstance, hInstance, HINSTANCE HINSTANCE hPrevInstance,hPrevInstance,PSTR PSTR szCmdLine, szCmdLine, int intiCmdShow)iCmdShow){{static static TCHAR TCHAR szAppName[] szAppName[] = TEXT TEXT ("HelloWin") ("HelloWin");;HWND HWND hwnd hwnd ;;MSG MSG msg msg ;;// // ===============Step 1: 1: 立 Window Class Class ==========================WNDCLASS WNDCLASS wndclass wndclass ;;wndclass.style wndclass.style = CS_HREDRAW CS_HREDRAW | | CS_VREDRAW CS_VREDRAW ;;wndclass.lpfnWndProc= WndProc WndProc ;; // //


理 LRESULT CALLBACK WndProc (HWND hwnd, hwnd, UINT UINT message, WPARAM wParam,LPARAM lParam){{ HDC HDC hdc hdc ;;PAINTSTRUCT ps ps ;;RECT RECT rect rect ;;switch switch (message) {{case case WM_CREATE:// //


錄 FileDebug 錄HelloWin.vcprojHelloWin.slnHelloWin.c ( cpp)Description 行 Visual C++ .Net .vcproj solution fileSource file.rcResource Script fileHellWin.ncb InteliSense Database file


不 PlaySoundA import library winmm.libProject HelloWin Properties …Step 2: Addition Dependence winmm.libwinmm.libStep 1: Input


立 simple windows application


錄 :(MFC ) 理 Button Control Events tab childwindow() event 理 !, 若 更 , ?( Control Events tab )


ms-help://MS.MSDNQTR.2004JAN.1033/vclib/html/_mfc_wm__message_handlers.3a_.l_.2d_.m.htm message override L-M滑 滑


理 滑 CButton, 說 , overrideafx_msg void OnLButtonDown( UINT nFlags, CPointpoint ); 類 testDlg.h


理 立 , Dialog testDlg.htestDlg.cpp

More magazines by this user
Similar magazines