c++在矩形窗口中插入数字
时间: 2023-06-20 15:09:42 浏览: 57
您可以使用图形库来在矩形窗口中插入数字。以下是一个使用 WinAPI 的示例代码:
```c++
#include <windows.h>
#include <iostream>
#include <string>
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int ncmdshow)
{
WNDCLASSW wc = {0};
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hInstance = hInst;
wc.lpszClassName = L"myWindowClass";
wc.lpfnWndProc = WindowProcedure;
if(!RegisterClassW(&wc))
return -1;
HWND hwnd = CreateWindowW(L"myWindowClass", L"Window Title", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 500, 500, NULL, NULL, NULL, NULL);
MSG msg = {0};
while(GetMessage(&msg, NULL, NULL, NULL))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// 选择字体和颜色
HFONT hFont = CreateFont(16, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, L"Arial");
SelectObject(hdc, hFont);
SetTextColor(hdc, RGB(0, 0, 0));
// 绘制矩形框
RECT rect = {50, 50, 250, 150};
DrawEdge(hdc, &rect, EDGE_RAISED, BF_RECT);
// 插入数字
std::wstring text = L"123";
DrawTextW(hdc, text.c_str(), -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
EndPaint(hwnd, &ps);
}
break;
default:
return DefWindowProcW(hwnd, msg, wp, lp);
}
return 0;
}
```
该代码将创建一个带有矩形框和数字的窗口。您可以更改 `text` 变量的值来插入不同的数字。请注意,这仅是一个示例代码,您可以根据需要进行修改和扩展。