#include <windows.h>
// 画直线
void DrawLine(HDC hdc, int x1, int y1, int x2, int y2, COLORREF color)
{
HPEN pen = CreatePen(PS_SOLID, 2, color); // 创建画笔
HPEN oldPen = SelectObject(hdc, pen); // 选入画笔
MoveToEx(hdc, x1, y1, NULL); // 起点
LineTo(hdc, x2, y2); // 终点
SelectObject(hdc, oldPen);
DeleteObject(pen);
}
// 画空心矩形(正方形只要宽高相等)
void DrawRect(HDC hdc, int x, int y, int w, int h, COLORREF color)
{
HPEN pen = CreatePen(PS_SOLID, 2, color);
HPEN oldPen = SelectObject(hdc, pen);
Rectangle(hdc, x, y, x+w, y+h);
SelectObject(hdc, oldPen);
DeleteObject(pen);
}
// 画实心矩形
void DrawFillRect(HDC hdc, int x, int y, int w, int h, COLORREF color)
{
HBRUSH brush = CreateSolidBrush(color);
HBRUSH oldBrush = SelectObject(hdc, brush);
Rectangle(hdc, x, y, x+w, y+h);
SelectObject(hdc, oldBrush);
DeleteObject(brush);
}
// 画空心圆
void DrawCircle(HDC hdc, int x, int y, int r, COLORREF color)
{
HPEN pen = CreatePen(PS_SOLID, 2, color);
HPEN oldPen = SelectObject(hdc, pen);
Ellipse(hdc, x-r, y-r, x+r, y+r); // 圆心(x,y) 半径r
SelectObject(hdc, oldPen);
DeleteObject(pen);
}
// 画实心圆
void DrawFillCircle(HDC hdc, int x, int y, int r, COLORREF color)
{
HBRUSH brush = CreateSolidBrush(color);
HBRUSH oldBrush = SelectObject(hdc, brush);
Ellipse(hdc, x-r, y-r, x+r, y+r);
SelectObject(hdc, oldBrush);
DeleteObject(brush);
}
// 画空心三角形
void DrawTriangle(HDC hdc, int x1,int y1, int x2,int y2, int x3,int y3, COLORREF color)
{
HPEN pen = CreatePen(PS_SOLID, 2, color);
HPEN oldPen = SelectObject(hdc, pen);
MoveToEx(hdc, x1, y1, NULL);
LineTo(hdc, x2, y2);
LineTo(hdc, x3, y3);
LineTo(hdc, x1, y1);
SelectObject(hdc, oldPen);
DeleteObject(pen);
}
// 画实心三角形
void DrawFillTriangle(HDC hdc, int x1,int y1, int x2,int y2, int x3,int y3, COLORREF color)
{
POINT pts[3] = {{x1,y1},{x2,y2},{x3,y3}};
HBRUSH brush = CreateSolidBrush(color);
HBRUSH oldBrush = SelectObject(hdc, brush);
Polygon(hdc, pts, 3);
SelectObject(hdc, oldBrush);
DeleteObject(brush);
}