wingdi使用例子
时间: 2024-04-23 12:17:55 浏览: 136
win GDI 画刷的使用
Wingdi是Windows图形设备接口(Windows Graphics Device Interface)的缩写,它是Windows操作系统中用于绘制图形和处理图像的API。下面是一个使用Wingdi的简单例子:
```c++
#include <windows.h>
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// 创建窗口类
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = "WingdiExample";
// 注册窗口类
RegisterClass(&wc);
// 创建窗口
HWND hwnd = CreateWindowEx(
0,
"WingdiExample",
"Wingdi Example",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
// 显示窗口
ShowWindow(hwnd, nCmdShow);
// 消息循环
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd, &ps);
// 使用Wingdi绘制一个矩形
RECT rect = { 50, 50, 200, 200 };
HBRUSH hBrush = CreateSolidBrush(RGB(255, 0, 0));
FillRect(hdc, &rect, hBrush);
DeleteObject(hBrush);
EndPaint(hwnd, &ps);
return 0;
}
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
```
这个例子演示了如何使用Wingdi在Windows窗口中绘制一个红色的矩形。在`WindowProc`函数中,我们处理了窗口的消息,当收到`WM_PAINT`消息时,我们使用`FillRect`函数绘制了一个矩形。
阅读全文