C++ drawlines
时间: 2023-07-03 11:13:47 浏览: 98
设备上下文中的绘画函数-wt901c使用说明书v1.0
在 C++ 中,可以使用不同的图形库来绘制直线,如 WinAPI、OpenGL、Qt、SDL 等等。这里以 WinAPI 为例,介绍如何在 C++ 中使用 WinAPI 绘制直线。
1. 包含 Windows.h 头文件:
```c++
#include <Windows.h>
```
2. 定义窗口过程:
```c++
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
HPEN hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0)); // 创建一支红色的画笔
HPEN hOldPen = (HPEN)SelectObject(hdc, hPen); // 选择画笔
MoveToEx(hdc, 100, 100, NULL); // 移动画笔到起点
LineTo(hdc, 200, 200); // 绘制直线
SelectObject(hdc, hOldPen); // 恢复原来的画笔
DeleteObject(hPen); // 删除画笔
EndPaint(hWnd, &ps);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
```
3. 创建窗口:
```c++
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
const char* CLASS_NAME = "MyWindowClass";
WNDCLASS wc = {};
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
HWND hWnd = CreateWindowEx(
0,
CLASS_NAME,
"My Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
NULL,
NULL,
hInstance,
NULL
);
if (hWnd == NULL)
return 0;
ShowWindow(hWnd, nCmdShow);
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
```
4. 在窗口过程中处理 WM_PAINT 消息,调用 WinAPI 绘制直线:
```c++
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
HPEN hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0)); // 创建一支红色的画笔
HPEN hOldPen = (HPEN)SelectObject(hdc, hPen); // 选择画笔
MoveToEx(hdc, 100, 100, NULL); // 移动画笔到起点
LineTo(hdc, 200, 200); // 绘制直线
SelectObject(hdc, hOldPen); // 恢复原来的画笔
DeleteObject(hPen); // 删除画笔
EndPaint(hWnd, &ps);
break;
}
```
完整的代码如下:
```c++
#include <Windows.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
HPEN hPen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0)); // 创建一支红色的画笔
HPEN hOldPen = (HPEN)SelectObject(hdc, hPen); // 选择画笔
MoveToEx(hdc, 100, 100, NULL); // 移动画笔到起点
LineTo(hdc, 200, 200); // 绘制直线
SelectObject(hdc, hOldPen); // 恢复原来的画笔
DeleteObject(hPen); // 删除画笔
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)
{
const char* CLASS_NAME = "MyWindowClass";
WNDCLASS wc = {};
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
HWND hWnd = CreateWindowEx(
0,
CLASS_NAME,
"My Window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
NULL,
NULL,
hInstance,
NULL
);
if (hWnd == NULL)
return 0;
ShowWindow(hWnd, nCmdShow);
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
```
该程序会创建一个窗口,并在窗口中绘制一条从 (100, 100) 到 (200, 200) 的红色直线。
阅读全文