#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; }
,