画笔SelectObject

博客介绍了SelectObject函数,其传回值是呼叫前设备内容中的画笔句柄,可通过它切换画笔。还阐述了使用画笔等GDI对象的三条规则,如最后删除自建对象等。此外,说明了通过GetObject获取LOGPEN结构值,以及用GetCurrentObject获取当前选进设备内容的画笔句柄。

SelectObject的传回值是此呼叫前设备内容中的画笔句柄。如果启动一个新的内容
并呼叫
hPen=SelectObject(hdc,GetStockObject(WHITE_PEN));
则设备内容中的目前画笔将为WHITE_PEN,变量hPen将会是BLACK_PEN的句柄
以后通过呼叫
SelectObject(hdc,hPen);
就能后将BLACK_PEN选进设备内容。

在使用画笔等GDI对象时,应该遵守以下三条规则:

1、最后要删除自己建立的所有GDI对象
 
2、当GDI对象正在一个有效的设备内容中使用时,不要删除它。
 
3、不要删除现有对象。

如果有一个画笔的句柄,就可以通过呼叫GetObject取得LOGPEN结构各个成员的值:

GetObject (hPen, sizeof (LOGPEN), (LPVOID) &logpen) ;
       
如果需要目前选进设备内容的画笔句柄,可以呼叫:

hPen = GetCurrentObject (hdc, OBJ_PEN) ;

void CDDAView::OnDraw(CDC* pDC) { CDDADoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here // 心形参数方程 double heartX(double t) { return 16 * pow(sin(t), 3); } double heartY(double t) { return 13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t); } // 窗口过程函数 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_PAINT: hdc = BeginPaint(hWnd, &ps); // 设置绘图属性 HPEN hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0)); // 红色画笔 SelectObject(hdc, hPen); HBRUSH hBrush = CreateSolidBrush(RGB(255, 150, 150)); // 浅红色填充 SelectObject(hdc, hBrush); // 计算心形点并绘制 POINT points[100]; // 存储心形点 const double scale = 10.0; // 缩放因子 const int offsetX = 200; // X偏移(居中) const int offsetY = 200; // Y偏移(居中) // 生成心形点 for (int i = 0; i < 100; i++) { double t = 2 * 3.1415926 * i / 100; // 0 到 2π points[i].x = static_cast<int>(heartX(t) * scale) + offsetX; points[i].y = static_cast<int>(-heartY(t) * scale) + offsetY; // Y轴翻转 } // 绘制填充心形 Polygon(hdc, points, 100); // 清理资源 DeleteObject(hPen); DeleteObject(hBrush); EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } // 主函数 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // 注册窗口类 WNDCLASS wc = {0}; wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.lpszClassName = "HeartWindowClass"; wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); RegisterClass(&wc); // 创建窗口 HWND hWnd = CreateWindow( "HeartWindowClass", "VC6 爱心绘制", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 500, NULL, NULL, hInstance, NULL ); if (!hWnd) return 0; ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); // 消息循环 MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } }为什么报错
09-20
#include <windows.h> #include <tchar.h> #include <math.h> #include <stdio.h> typedef struct Time { int hour; int min; int sec; }TimeStructure; HINSTANCE hInst; //表示当前程序的实例句柄 int flag = 1; //1/0 代表按钮不同状态 const char* weekstr[] = { "天","一","二","三","四","五","六", }; BOOLEAN InitWindowClass(HINSTANCE hInstance, int nCmdShow); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); void AdjustTime(TimeStructure* x); //根据second 调整hour、minute int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; if (!InitWindowClass(hInstance, nCmdShow)) { MessageBox(NULL, L"创建窗口失败!", _T("创建窗口"), NULL); return 1; } while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hDC; PAINTSTRUCT ps; HBRUSH hBrush; HPEN hPen; RECT clientRect; SYSTEMTIME t; //本地时间 static TCHAR szTextBuf[200]; //static 控件文本(缓冲区) static HWND hStatic; //文本框 控件句柄 static HWND hBtn; //按钮 控件句柄 static TimeStructure x; int wmId, wmEvent; //控件ID double sita = 0; //角度 int xOrg, yOrg, rSec, rMin, rHour, rClock, xBegin, xEnd, yBegin, yEnd; switch (message) { case WM_CREATE://创建窗口时,响应的消息 //设置定时器 SetTimer(hWnd, 9999, 1000, NULL); //设置按钮 hBtn = CreateWindow( TEXT("button"), TEXT("暂停"), WS_CHILD | WS_VISIBLE | WS_BORDER | BS_PUSHBUTTON, 1100/*X坐标*/, 150 /*Y坐标*/, 150 /*宽度*/, 50/*高度*/, hWnd, (HMENU)2 /*控件唯一标识符*/, hInst, NULL ); break; case WM_COMMAND: wmId = LOWORD(wParam); wmEvent = HIWORD(wParam); switch (wmId) { case 2: //按下按钮 //通过每次点击按钮,来回改变flag的值(0或1),从而控制开始和暂停 if (flag) { flag = 0; KillTimer(hWnd, 9999); //清除定时器 SetWindowText(hBtn, TEXT("开始")); //更改按钮内容 } else { flag = 1; SetTimer(hWnd, 9999, 1000, NULL); //设置定时器 SetWindowText(hBtn, TEXT("暂停")); //更改按钮内容 } break; default: //不处理的消息一定要交给 DefWindowProc 处理。 return DefWindowProc(hWnd, message, wParam, lParam); } break; case WM_PAINT: GetLocalTime(&t); //本地时间 x.sec = t.wSecond; x.min = t.wMinute; x.hour = t.wHour; AdjustTime(&x); //调用函数 根据s调整h、m hStatic = CreateWindow( L"static", //静态文本框的类名 L"时间", //控件的文本(可更改) WS_CHILD /*子窗口*/ | WS_VISIBLE /*创建时显示*/ | WS_BORDER /*带边框*/ | SS_CENTER /*水平居中*/ | SS_CENTERIMAGE /*垂直居中*/, 950 /*X坐标*/, 20 /*Y坐标*/, 300 /*宽度*/, 100 /*高度*/, hWnd, //父窗口句柄 (HMENU)1, //为控件指定一个唯一标识符 hInst, //当前程序实例句柄 NULL ); //设置 static 控件的文本 wsprintf(szTextBuf, TEXT("%d年%d月%d日 %d:%d:%d 星期%S"), t.wYear, t.wMonth, t.wDay, t.wHour, t.wMinute, t.wSecond, weekstr[t.wDayOfWeek]); SetWindowText(hStatic, szTextBuf); //开始绘图 hDC = BeginPaint(hWnd, &ps); GetClientRect(hWnd, &clientRect); // 获取用户区的尺寸 hPen = (HPEN)GetStockObject(BLACK_PEN);//设置画笔为系统预定定义的黑色画笔 hBrush = CreateSolidBrush(RGB(224,255,255)); SelectObject(hDC, hPen);//选择画笔 SelectObject(hDC, hBrush);//选择画刷 xOrg = (clientRect.left + clientRect.right) / 2; yOrg = (clientRect.top + clientRect.bottom) / 2;//计算屏幕中心的坐标,它也是钟表的中心 rClock = min(xOrg, yOrg) - 50;//钟表的半径 rSec = rClock * 6 / 7;//秒针的半径 rMin = rClock * 5 / 6;//分针的半径 rHour = rClock * 2 / 3;//时针的半径 Ellipse(hDC, xOrg - rClock, yOrg - rClock, xOrg + rClock, yOrg + rClock);//绘制表面圆 for (int i = 0; i < 60; i++)//绘制表面的刻度 { if (i % 5)// 绘制表面的非整点刻度 { hPen = CreatePen(PS_SOLID, 2, RGB(102, 205, 170)); SelectObject(hDC, hPen); xBegin = (int)(xOrg + rClock * sin(2 * 3.1415926 * i / 60)); yBegin = (int)(yOrg + rClock * cos(2 * 3.1415926 * i / 60)); MoveToEx(hDC, xBegin, yBegin, NULL); xEnd = (int)(xOrg + (rClock - 20) * sin(2 * 3.1415926 * i / 60)); yEnd = (int)(yOrg + (rClock - 20) * cos(2 * 3.1415926 * i / 60)); } else {//绘制表面的整点刻度 hPen = CreatePen(PS_SOLID, 5, RGB(139, 139, 122)); SelectObject(hDC, hPen); xBegin = (int)(xOrg + rClock * sin(2 * 3.1415926 * i / 60)); yBegin = (int)(yOrg + rClock * cos(2 * 3.1415926 * i / 60)); MoveToEx(hDC, xBegin, yBegin, NULL); xEnd = (int)(xOrg + (rClock - 25) * sin(2 * 3.1415926 * i / 60)); yEnd = (int)(yOrg + (rClock - 25) * cos(2 * 3.1415926 * i / 60)); } LineTo(hDC, xEnd, yEnd); DeleteObject(hPen); } //秒针 hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0)); SelectObject(hDC, hPen); sita = 2 * 3.1415926 * x.sec / 60; //下面求秒针的起点坐标,它的位置在秒针的最末端 xBegin = xOrg + (int)(rSec * sin(sita)); yBegin = yOrg - (int)(rSec * cos(sita)); //下面求秒针的终点坐标,它的位置在秒针的反方向的长度为秒针的/8 xEnd = xOrg + (int)(rClock * sin(sita + 3.1415926) / 8); yEnd = yOrg - (int)(rClock * cos(sita + 3.1415926) / 8); MoveToEx(hDC, xBegin, yBegin, NULL); LineTo(hDC, xEnd, yEnd);//绘制 //分针 hPen = CreatePen(PS_SOLID, 5, RGB(255, 193, 193)); SelectObject(hDC, hPen); sita = 2 * 3.1415926 * x.min / 60; xBegin = xOrg + (int)(rMin * sin(sita)); yBegin = yOrg - (int)(rMin * cos(sita));//分针的起点 xEnd = xOrg + (int)(rClock * sin(sita + 3.1415926) / 8); yEnd = yOrg - (int)(rClock * cos(sita + 3.1415926) / 8);// 分针的终点 MoveToEx(hDC, xBegin, yBegin, NULL); LineTo(hDC, xEnd, yEnd);//绘制 //时针 hPen = CreatePen(PS_SOLID, 10, RGB(238, 180, 34)); SelectObject(hDC, hPen); sita = 2 * 3.1415926 * x.hour / 12; xBegin = xOrg + (int)(rHour * sin(sita)); yBegin = yOrg - (int)(rHour * cos(sita)); xEnd = xOrg + (int)(rClock * sin(sita + 3.1415926) / 8); yEnd = yOrg - (int)(rClock * cos(sita + 3.1415926) / 8); MoveToEx(hDC, xBegin, yBegin, NULL); LineTo(hDC, xEnd, yEnd);//绘制 DeleteObject(hPen); DeleteObject(hBrush); //结束绘图 EndPaint(hWnd, &ps); break; case WM_TIMER://响应定时器发出的定时消息 if (wParam == 9999)//判断是否是设置的定时器发出的消息 InvalidateRect(hWnd, NULL, 1); //刷新屏幕 break; case WM_SIZE://窗口尺寸改变时,刷新窗口 InvalidateRect(hWnd, NULL, 1); break; case WM_DESTROY: PostQuitMessage(0); // 调用PostQuitMessage发出WM_ QUIT 消息 KillTimer(hWnd, 9999); break; default: return DefWindowProc(hWnd, message, wParam, lParam); break; } return 0; } void AdjustTime(TimeStructure* x) { if (x->sec == 60) { x->sec = 0; x->min++; if (x->min == 60) { x->min = 0; x->hour++; if (x->hour == 12) x->hour = 0; } } } BOOLEAN InitWindowClass(HINSTANCE hInstance, int nCmdShow) { WNDCLASSEX wcex; HWND hWnd; TCHAR szWindowClass[] = L"窗口示例"; TCHAR szTit1e[] = L"模拟时钟"; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = 0; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION)); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wcex.lpszMenuName = NULL; wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION)); if (!RegisterClassEx(&wcex)) return FALSE; hWnd = CreateWindow(szWindowClass, szTit1e, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL ); if (!hWnd) return FALSE; ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return TRUE; }适当修改代码,使时钟刻度显示数字,优化按钮中的字体并填充颜色
最新发布
10-28
#include <Windows.h> #include <string> #include <vector> #include <sstream> // 定义控件ID #define ID_EDIT_AX 101 #define ID_EDIT_AY 102 #define ID_EDIT_BX 103 #define ID_EDIT_BY 104 #define ID_BUTTON_DRAW 105 #define ID_BUTTON_CLEAR 106 // 存储点坐标的结构体 struct Point { float x = 0.0f; float y = 0.0f; bool valid = false; }; // 全局变量存储点A和B Point g_pointA; Point g_pointB; // 窗口过程函数 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_PAINT: { PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); // 获取客户区尺寸 RECT rect; GetClientRect(hWnd, &rect); int width = rect.right - rect.left; int height = rect.bottom - rect.top; // 设置坐标系原点在左下角 int originX = 50; // 左边距 int originY = height - 50; // 下边距 // 创建白色画笔用于坐标轴 HPEN hAxisPen = CreatePen(PS_SOLID, 2, RGB(255, 255, 255)); HPEN hOldPen = (HPEN)SelectObject(hdc, hAxisPen); // 绘制X轴(带箭头) MoveToEx(hdc, originX, originY, NULL); LineTo(hdc, width - 20, originY); // X轴主线段 // X轴箭头 LineTo(hdc, width - 30, originY - 5); MoveToEx(hdc, width - 20, originY, NULL); LineTo(hdc, width - 30, originY + 5); // 绘制Y轴(带箭头) MoveToEx(hdc, originX, originY, NULL); LineTo(hdc, originX, 20); // Y轴主线段 // Y轴箭头 LineTo(hdc, originX - 5, 30); MoveToEx(hdc, originX, 20, NULL); LineTo(hdc, originX + 5, 30); // 创建虚线画笔用于网格线 HPEN hGridPen = CreatePen(PS_DOT, 1, RGB(100, 100, 100)); SelectObject(hdc, hGridPen); // 绘制网格线(每50像素一条) for (int x = originX + 50; x < width - 20; x += 50) { MoveToEx(hdc, x, originY, NULL); LineTo(hdc, x, 20); } for (int y = originY - 50; y > 20; y -= 50) { MoveToEx(hdc, originX, y, NULL); LineTo(hdc, width - 20, y); } // 恢复原始画笔 SelectObject(hdc, hOldPen); DeleteObject(hAxisPen); DeleteObject(hGridPen); // 设置文本颜色为白色 SetTextColor(hdc, RGB(255, 255, 255)); SetBkColor(hdc, RGB(0, 0, 0)); // 黑色背景 // 绘制刻度标记 for (int i = 1; i <= 10; i++) { int xPos = originX + i * 50; int yPos = originY - i * 50; // X轴刻度 MoveToEx(hdc, xPos, originY - 5, NULL); LineTo(hdc, xPos, originY + 5); // Y轴刻度 MoveToEx(hdc, originX - 5, yPos, NULL); LineTo(hdc, originX + 5, yPos); // 绘制刻度值 std::wstring label = std::to_wstring(i); TextOut(hdc, xPos - 5, originY + 10, label.c_str(), label.length()); TextOut(hdc, originX - 25, yPos - 8, label.c_str(), label.length()); } // 绘制坐标轴标签 TextOut(hdc, width - 15, originY - 15, L"X", 1); TextOut(hdc, originX - 15, 15, L"Y", 1); TextOut(hdc, originX - 10, originY + 10, L"0", 1); // 如果点A和点B有效,绘制它们并连接 if (g_pointA.valid && g_pointB.valid) { // 转换为屏幕坐标 int screenAx = originX + static_cast<int>(g_pointA.x * 50); int screenAy = originY - static_cast<int>(g_pointA.y * 50); int screenBx = originX + static_cast<int>(g_pointB.x * 50); int screenBy = originY - static_cast<int>(g_pointB.y * 50); // 创建红色画笔用于直线 HPEN hLinePen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0)); SelectObject(hdc, hLinePen); // 绘制直线 MoveToEx(hdc, screenAx, screenAy, NULL); LineTo(hdc, screenBx, screenBy); // 创建红色实心画笔用于点 HBRUSH hRedBrush = CreateSolidBrush(RGB(255, 0, 0)); HBRUSH hOldBrush = (HBRUSH)SelectObject(hdc, hRedBrush); // 绘制点A Ellipse(hdc, screenAx - 5, screenAy - 5, screenAx + 5, screenAy + 5); TextOut(hdc, screenAx + 10, screenAy - 10, L"A", 1); // 绘制点B Ellipse(hdc, screenBx - 5, screenBy - 5, screenBx + 5, screenBy + 5); TextOut(hdc, screenBx + 10, screenBy - 10, L"B", 1); // 计算并显示距离 float distance = sqrtf(powf(g_pointB.x - g_pointA.x, 2) + powf(g_pointB.y - g_pointA.y, 2)); std::wstring distText = L"距离: " + std::to_wstring(distance).substr(0, 4); TextOut(hdc, width - 150, 30, distText.c_str(), distText.length()); // 恢复并删除对象 SelectObject(hdc, hOldBrush); DeleteObject(hLinePen); DeleteObject(hRedBrush); } EndPaint(hWnd, &ps); break; } case WM_CREATE: { // 创建输入框和标签 CreateWindow(L"STATIC", L"A点 X:", WS_CHILD | WS_VISIBLE | SS_CENTER, 10, 10, 50, 20, hWnd, NULL, NULL, NULL); CreateWindow(L"EDIT", L"", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_NUMBER, 65, 10, 50, 20, hWnd, (HMENU)ID_EDIT_AX, NULL, NULL); CreateWindow(L"STATIC", L"Y:", WS_CHILD | WS_VISIBLE | SS_CENTER, 120, 10, 20, 20, hWnd, NULL, NULL, NULL); CreateWindow(L"EDIT", L"", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_NUMBER, 145, 10, 50, 20, hWnd, (HMENU)ID_EDIT_AY, NULL, NULL); CreateWindow(L"STATIC", L"B点 X:", WS_CHILD | WS_VISIBLE | SS_CENTER, 10, 40, 50, 20, hWnd, NULL, NULL, NULL); CreateWindow(L"EDIT", L"", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_NUMBER, 65, 40, 50, 20, hWnd, (HMENU)ID_EDIT_BX, NULL, NULL); CreateWindow(L"STATIC", L"Y:", WS_CHILD | WS_VISIBLE | SS_CENTER, 120, 40, 20, 20, hWnd, NULL, NULL, NULL); CreateWindow(L"EDIT", L"", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_NUMBER, 145, 40, 50, 20, hWnd, (HMENU)ID_EDIT_BY, NULL, NULL); // 创建按钮 CreateWindow(L"BUTTON", L"绘制", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 210, 10, 80, 25, hWnd, (HMENU)ID_BUTTON_DRAW, NULL, NULL); CreateWindow(L"BUTTON", L"清除", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 210, 40, 80, 25, hWnd, (HMENU)ID_BUTTON_CLEAR, NULL, NULL); // 设置默认值 SetDlgItemText(hWnd, ID_EDIT_AX, L"2"); SetDlgItemText(hWnd, ID_EDIT_AY, L"3"); SetDlgItemText(hWnd, ID_EDIT_BX, L"7"); SetDlgItemText(hWnd, ID_EDIT_BY, L"8"); break; } case WM_COMMAND: { int wmId = LOWORD(wParam); if (wmId == ID_BUTTON_DRAW) { // 获取输入的坐标值 wchar_t text[32]; GetDlgItemText(hWnd, ID_EDIT_AX, text, 32); g_pointA.x = _wtof(text); GetDlgItemText(hWnd, ID_EDIT_AY, text, 32); g_pointA.y = _wtof(text); GetDlgItemText(hWnd, ID_EDIT_BX, text, 32); g_pointB.x = _wtof(text); GetDlgItemText(hWnd, ID_EDIT_BY, text, 32); g_pointB.y = _wtof(text); // 验证坐标是否在有效范围内(0-10) g_pointA.valid = (g_pointA.x >= 0 && g_pointA.x <= 10 && g_pointA.y >= 0 && g_pointA.y <= 10); g_pointB.valid = (g_pointB.x >= 0 && g_pointB.x <= 10 && g_pointB.y >= 0 && g_pointB.y <= 10); // 重绘窗口 InvalidateRect(hWnd, NULL, TRUE); } else if (wmId == ID_BUTTON_CLEAR) { // 清除点 g_pointA.valid = false; g_pointB.valid = false; // 重绘窗口 InvalidateRect(hWnd, NULL, TRUE); } break; } case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; } // 应用程序入口点 int WINAPI wWinMain( _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) { // 1. 注册窗口类 const wchar_t CLASS_NAME[] = L"CartesianWindowClass"; WNDCLASSEXW wcex = { sizeof(WNDCLASSEX) }; wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = WndProc; wcex.hInstance = hInstance; wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); // 黑色背景 wcex.lpszClassName = CLASS_NAME; if (!RegisterClassExW(&wcex)) { MessageBox(NULL, L"窗口类注册失败!", L"错误", MB_ICONERROR); return 1; } // 2. 计算窗口尺寸(包含边框和标题栏) int clientWidth = 600; int clientHeight = 600; RECT rect = { 0, 0, clientWidth, clientHeight }; AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE); int windowWidth = rect.right - rect.left; int windowHeight = rect.bottom - rect.top; // 3. 计算居中位置 int screenWidth = GetSystemMetrics(SM_CXSCREEN); int screenHeight = GetSystemMetrics(SM_CYSCREEN); int posX = (screenWidth - windowWidth) / 2; int posY = (screenHeight - windowHeight) / 2; // 4. 创建窗口 HWND hWnd = CreateWindowExW( 0, // 无扩展样式 CLASS_NAME, L"笛卡尔坐标系 - 绘制点A和点B", WS_OVERLAPPEDWINDOW & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX, // 禁用调整大小和最大化 posX, posY, // 位置 windowWidth, // 宽度 windowHeight, // 高度 nullptr, nullptr, hInstance, nullptr); if (!hWnd) { MessageBox(NULL, L"窗口创建失败!", L"错误", MB_ICONERROR); return 1; } // 5. 显示窗口 ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); // 6. 消息循环 MSG msg; while (GetMessage(&msg, nullptr, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (int)msg.wParam; } 将以上代码接入我发的代码中,并给我全部无删减的完整代码
07-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值