學習windows編程 day1
阿新 • • 發佈:2018-03-03
col erl 信息 行高 lpstr ins i++ color AI
#include <windows.h> #include <strsafe.h> /* 任務:去掉標題欄和邊框 */ //#define LineHeight 15 這是自己猜測的行高,不要這樣做 LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {static TCHAR szClassName[] = TEXT("MyWindows2"); HWND hwnd; MSG msg; //註冊一個窗口類 //註冊窗口類 WNDCLASS wndclass; wndclass.hInstance = hInstance; wndclass.lpszClassName = szClassName; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.lpfnWndProc = WndProc; wndclass.lpszMenuName= NULL; wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.style = CS_HREDRAW; if (!RegisterClass(&wndclass)) { MessageBox(NULL, L"Error", L"Error", MB_ICONERROR); return 0; } //創建窗口 hwnd = CreateWindow( szClassName, TEXT("MyFirstPractice"), //方法一 //WS_OVERLAPPEDWINDOW &~WS_CAPTION &~WS_SYSMENU &~WS_SIZEBOX, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL ); ShowWindow(hwnd, nShowCmd); UpdateWindow(hwnd); while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); //此處分發消息,調用消息回調函數, //同時消息回調函數的返回值作為DIspatchMessage的返回值返回, //一般講這個返回值忽略 DispatchMessage(&msg); /* The return value specifies the value returned by the window procedure. Although its meaning depends on the message being dispatched, the return value generally is ignored. 返回值指定窗口過程返回的值。盡管它的含義依賴於發送的消息,但是返回值通常被忽略。 */ } return msg.wParam; } /* WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) 和 typedef struct tagMSG { HWND hwnd; UINT message; WPARAM wParam; LPARAM lParam; DWORD time; POINT pt; } MSG, *PMSG; 前四個參數一致 操作系統直接使用msg前四位作為參數傳入窗口回調函數 */ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hdc; PAINTSTRUCT ps; RECT rect; TCHAR szBuffer[128]; size_t iTarget; TEXTMETRIC tm; static int cxChar, cyChar; switch (message) { case WM_CREATE: //在創建時就設置為靜態全局變量,在後面的消息觸發是就可以直接使用了 hdc = GetDC(hwnd); //獲得當前字體的尺寸 GetTextMetrics(hdc,&tm); //系統獲取行距等信息,不要自己猜測 cxChar = tm.tmAveCharWidth; cyChar = tm.tmHeight + tm.tmExternalLeading; ReleaseDC(hwnd,hdc); case WM_SIZE: ; LONG_PTR s = GetWindowLongPtr(hwnd, GWL_STYLE); //s = s &~WS_CAPTION &~WS_SYSMENU&~WS_SIZEBOX; s = s&~WS_SIZEBOX; SetWindowLongPtr(hwnd,GWL_STYLE, s); break; case WM_LBUTTONDOWN: if (IDYES == MessageBox(NULL, L"Are you sure to quit?", L"Info", MB_YESNO)){ PostQuitMessage(0); } break; case WM_NCLBUTTONUP: MessageBox(NULL, L"Clicked on Not client Area", L"Info", NULL); break; case WM_PAINT: hdc = BeginPaint(hwnd, &ps); GetClientRect(hwnd, &rect); //SetTextAlign(hdc, GetTextAlign(hdc) | TA_CENTER); int i = 0; for (; i < 10;i++) { //使用安全的字符串方法,防止溢出等原因 StringCchPrintf(szBuffer, 128, TEXT("%d: %s"),i+1,TEXT("I love C++")); //wsprintf(szBuffer, TEXT("%d: %s"),i+1, TEXT("I love C++")); StringCchLength(szBuffer, 128, &iTarget); //取代了lstrlen //TextOut(hdc, 0,i*LineHeight, szBuffer, lstrlen(szBuffer)); TextOut(hdc, cxChar, i*cyChar, szBuffer, iTarget); } //TextOut(hdc, 400, 300, L"this is my second program", 20); //DrawText(hdc, L"this is my second program", -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE); EndPaint(hwnd, &ps); break; case WM_CLOSE: if (IDYES == MessageBox(NULL, L"Are you sure to quit?", L"Info", MB_YESNO)) { DestroyWindow(hwnd); } break; case WM_DESTROY: MessageBox(NULL, L"GUI is close,deal sql before quit", L"Info", MB_OK); PostQuitMessage(0); break; case WM_QUIT: break; default: return DefWindowProc(hwnd, message, wParam, lParam); } return 0; }
學習windows編程 day1