gdi+绘制矩形
时间: 2023-06-28 07:14:41 浏览: 140
GDI.rar_GDI+绘制矩形_gdi
您可以使用GDI+中的Graphics对象的DrawRectangle方法来绘制矩形。可以通过以下代码片段来实现:
```c++
#include <windows.h>
#include <gdiplus.h>
using namespace Gdiplus;
void DrawRectangle(HDC hdc, int x, int y, int width, int height)
{
Graphics graphics(hdc);
Pen pen(Color(255, 0, 0, 0), 2);
graphics.DrawRectangle(&pen, x, y, width, height);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
HWND hWnd = CreateWindowW(L"STATIC", L"GDI+ Rectangle", WS_VISIBLE | WS_OVERLAPPEDWINDOW, 0, 0, 640, 480, NULL, NULL, hInstance, NULL);
HDC hdc = GetDC(hWnd);
DrawRectangle(hdc, 50, 50, 200, 100);
MSG msg;
while (GetMessageW(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
ReleaseDC(hWnd, hdc);
GdiplusShutdown(gdiplusToken);
return 0;
}
```
在上面的代码中,我们首先创建了一个Graphics对象,然后创建了一个红色的Pen对象,最后使用Graphics对象的DrawRectangle方法来绘制矩形。您可以调整Pen对象的颜色和宽度以满足您的需求。
阅读全文