windows應用程式【二】建立視窗
阿新 • • 發佈:2018-12-27
我們在建立視窗時需要的過程
關於訊息迴圈
因為處理器同時只能執行一個程式 因此我們需要作業系統去排程程式 因此我們只是將訊息傳給作業系統 等待迴應從而由作業系統來顯示我們需要的視窗
wndproc()
視窗過程負責用來響應某一類視窗收到的各種Windows訊息
#include <windows.h> LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static char szWndClassName[] = "hellowin"; HWND hwnd ; MSG msg ; WNDCLASS wndclass ; wndclass.style = CS_HREDRAW | CS_VREDRAW ; wndclass.lpfnWndProc = WndProc ; wndclass.cbClsExtra = 0 ; wndclass.cbWndExtra = 0 ; wndclass.hInstance = hInstance ; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ; wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = NULL ; wndclass.lpszClassName = szWndClassName ; if (!RegisterClass (&wndclass)) { MessageBox (NULL, "註冊失敗", "錯誤", MB_ICONERROR) ; return 0 ; } hwnd = CreateWindow (szWndClassName, // window class name "視窗標題", // window caption WS_OVERLAPPEDWINDOW, // window style CW_USEDEFAULT, // initial x position CW_USEDEFAULT, // initial y position CW_USEDEFAULT, // initial x size CW_USEDEFAULT, // initial y size NULL, // parent window handle NULL, // window menu handle hInstance, // program instance handle NULL) ; // creation parameters ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ; while (GetMessage (&msg, NULL, 0, 0)) //訊息佇列 { TranslateMessage (&msg) ; DispatchMessage (&msg) ; } return msg.wParam ; //WM_QUIT } LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc ; PAINTSTRUCT ps ; RECT rect ; switch (message) { case WM_PAINT: hdc = BeginPaint (hwnd, &ps) ; GetClientRect (hwnd, &rect) ; DrawText (hdc, TEXT ("在螢幕中心輸出文字"), -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) ; }
最後對於我們為什要對一個視窗定義很多遍
第一遍第一類時類似於定義了一個基類 也就像確定了你是大學生具有所有大學生的特點
但是第二次建立視窗時 會說明你是哪個專業的 性格是什麼