///'#include ///'iostream///'//n#include ///'Windows.h///'//n//nLRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);//n//nint WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)//n{//n const char CLASS_NAME[] = ///'Sample Window Class///';//n//n WNDCLASS wc = {};//n//n wc.lpfnWndProc = WndProc;//n wc.hInstance = hInstance;//n wc.lpszClassName = CLASS_NAME;//n//n RegisterClass(&wc);//n//n HWND hwnd = CreateWindowEx(//n 0,//n CLASS_NAME,//n ///'Window Title///',  //n WS_OVERLAPPEDWINDOW,//n CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,//n NULL,//n NULL,//n hInstance,//n NULL//n );//n//n if (hwnd == NULL)//n {//n return 0;//n }//n//n ShowWindow(hwnd, nCmdShow);//n//n MSG msg = {};//n//n while (GetMessage(&msg, NULL, 0, 0))//n {//n TranslateMessage(&msg);//n DispatchMessage(&msg);//n }//n//n return 0;//n}//n//nLRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)//n{//n switch (msg)//n {//n case WM_PAINT://n {//n PAINTSTRUCT ps;//n HDC hdc = BeginPaint(hwnd, &ps);//n//n RECT rect;//n GetClientRect(hwnd, &rect);//n//n int rectangleWidth = rect.right * 3 / 4; // 长方形宽度为窗口宽度的3/4//n int rectangleHeight = rect.bottom; // 长方形高度与窗口高度相同//n//n int rectangleX = (rect.right - rectangleWidth) / 2; // 长方形左上角X坐标//n int rectangleY = (rect.bottom - rectangleHeight) / 2; // 长方形左上角Y坐标//n//n RECT rectangleRect = { rectangleX, rectangleY, rectangleX + rectangleWidth, rectangleY + rectangleHeight };//n//n FillRect(hdc, &rectangleRect, (HBRUSH)(COLOR_WINDOW + 1));//n//n EndPaint(hwnd, &ps);//n break;//n }//n case WM_DESTROY://n PostQuitMessage(0);//n break;//n default://n return DefWindowProc(hwnd, msg, wParam, lParam);//n }//n//n return 0;//n}//n//n///'这段代码创建了一个窗口,并在窗口的客户区绘制了一个长方形。长方形的宽度为窗口宽度的3/4,高度与窗口高度相同。长方形的位置居中于窗口。在WM_PAINT消息处理函数中,使用FillRect函数绘制长方形,并使用GetClientRect函数获取窗口客户区的矩形区域。///

C++ Windows.h 绘制窗口和长方形 - 详细代码示例

原文地址: https://www.cveoy.top/t/topic/pIj5 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录