標籤:cat idt 函數 pen cal rsa span chm menu
#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