14.windows绘图(一)
1.绘图相关
绘图设备DC(Device Context),绘图上下文/绘图描述表
HDC - DC句柄,表示绘图设备
GDI - Windows Graphics Device Interface,win32提供绘图的API
2.颜色
(1)颜色的表示
电脑使用红、绿、蓝:R - 0 ~ 255
G - 0 ~ 255
B - 0 ~ 255
每一个颜色点是3个字节24位,保存0 - 2^24-1
16位:5, 5, 6 - 红、绿、蓝
32位:8, 8, 8, 8 - 红、绿、蓝、透明度
(2)颜色的使用
COLORREF - 实际为DWORD,例如:COLORREF nColor = 0;
赋值使用RGB宏,例如:nColor = RGB(0, 0, 255);
获取RGB值,GetRValue/GetGValue/GetBValue,例如:BYTE nRed = GetRValue(nColor);
3.点的使用
(1)GetPixel获取指定点的颜色
COLORREF GetPixel( HDC hdc, //DC句柄
int nXPos, //x坐标
int nYPos); //y坐标
(2)SetPixel设置指定的颜色
COLORREF SetPixel( HDC hdc, //DC句柄
int X, //x坐标
int Y, //y坐标
COLORREF crColor); //颜色值
返回点原来的颜色
4.线的使用(直线)
//移动窗口的当前点到它指定的点,并将它指定的点作为窗口的当前点
BOOL MoveToEx( HDC hdc, //DC句柄
int X, //指定点x坐标
int Y, //指定点y坐标
LPPOINT lpPoint); //原来的坐标,可以为NULL
//从窗口当前点到它指定点的点绘制一条直线,并将它指定的点作为窗口的当前点
BOOL LineTo( HDC hdc, //DC句柄
int nXEnd, //指定点的x坐标
int nYEnd); //指定点的y坐标
5.封闭图形:能够画刷填充的图形
//绘制直角矩形
BOOL Rectangle( HDC hdc, //DC句柄
int nLeftRect, //左上角x坐标
int nTopRect, //左上角y坐标
int nRightRect, //右下角x坐标
int nBottomRect); //右下角y坐标
//绘制圆角矩形
BOOL RoundRect( HDC hdc, //DC句柄
int nLeftRect, //左上角x坐标
int nTopRect, //左上角y坐标
int nRightRect, //右下角x坐标
int nBottomRect, //右下角y坐标
int nWidth, //弧的宽度
int nHeight); //弧的高度
//绘制圆形
BOOL Ellipse( HDC hdc, //DC句柄
int nLeftRect, //左上角x坐标
int nTopRect, //左上角y坐标
int nRightRect, //右下角x坐标
int nBottomRect); //右下角y坐标
6.弧线
//绘制弧线
BOOL Arc( HDC hdc, //DC矩形
int nLeftRect, //左上角x坐标
int nTopRect, //左上角y坐标
int nRightRect, //右下角x坐标
int nBottomRect, //右下角y坐标
int nXStartArc, //起点x坐标
int nYStartArc, //起点y坐标
int nXEndArc, //终点x坐标
int nYEndArc); //终点y坐标
相关实例代码:

#include "stdafx.h" HINSTANCE g_hInstance = 0; //接收当前程序实例句柄 int g_kind = 0;//标志量 void OnCommand(HWND hWnd, WPARAM wParam) { g_kind = LOWORD(wParam); switch (LOWORD(wParam)) { case ID_PT: case ID_LINE: case ID_RECT: case ID_ELL: case ID_ARC: InvalidateRect(hWnd, NULL, TRUE); break; } } void DrawPoint(HDC hdc) { for (int i = 0; i < 256; i++) { for (int j = 0; j < 256; j++) { SetPixel(hdc, i, j, RGB(i, j, 0)); } } } void DrawLine(HDC hdc) { MoveToEx(hdc, 100, 100, NULL); LineTo(hdc, 300, 300); LineTo(hdc, 0, 300); LineTo(hdc, 100, 100); } void DrawRec(HDC hdc) { Rectangle(hdc, 100, 100, 300, 300); RoundRect(hdc, 300, 100, 500, 300, 200, 200); } void DrawEll(HDC hdc) { Ellipse(hdc, 100, 100, 300, 300); Ellipse(hdc, 300, 100, 600, 300); } void DrawArc(HDC hdc) { SetArcDirection(hdc, AD_CLOCKWISE);//顺时针取弧 Arc(hdc, 100, 100, 300, 300, 100, 100, 300, 300); } void OnPaint(HWND hWnd) { PAINTSTRUCT pt = { 0 }; HDC hdc = BeginPaint(hWnd, &pt); //当前DC,在窗口中画图 HPEN hPen = CreatePen(PS_DASH, 1, RGB(255, 0, 0)); HGDIOBJ nOldPen = SelectObject(hdc, hPen); switch (g_kind) { case ID_ARC: DrawArc(hdc);//绘制弧线 break; case ID_ELL: DrawEll(hdc);//绘制圆 break; case ID_RECT: DrawRec(hdc);//绘制矩形 break; case ID_LINE: DrawLine(hdc);//绘制直线 break; case ID_PT: DrawPoint(hdc);//绘制点 break; } SelectObject(hdc, nOldPen); DeleteObject(hPen); EndPaint(hWnd, &pt); } //窗口处理函数 LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_PAINT: OnPaint(hWnd); break; case WM_COMMAND: OnCommand(hWnd, wParam); break; case WM_DESTROY: PostQuitMessage(0); break; } return DefWindowProc(hWnd, msg, wParam, lParam); } //注册窗口类 BOOL Register(LPSTR lpClassName, WNDPROC wndProc) { WNDCLASSEX wce = { 0 }; wce.cbSize = sizeof(wce); wce.cbClsExtra = 200; wce.cbClsExtra = 200; wce.hbrBackground = (HBRUSH)(COLOR_WINDOW + 3); wce.hCursor = NULL; wce.hIcon = NULL; wce.hIconSm = NULL; wce.hInstance = g_hInstance; wce.lpfnWndProc = wndProc; wce.lpszClassName = lpClassName; wce.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1); wce.style = CS_HREDRAW | CS_VREDRAW; ATOM nAtom = RegisterClassEx(&wce); if (0 == nAtom) { return FALSE; } return TRUE; } //创建主窗口 HWND CreateMainWindow(LPSTR lpClassName, LPSTR lpWndName) { HWND hWnd = CreateWindowEx(0, lpClassName, lpWndName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, g_hInstance, NULL); return hWnd; } //显示窗口 void Display(HWND hWnd) { ShowWindow(hWnd, SW_SHOW); UpdateWindow(hWnd); } //消息循环 void Message() { MSG nMsg = { 0 }; while (GetMessage(&nMsg, NULL, 0, 0)) { TranslateMessage(&nMsg); DispatchMessage(&nMsg); } } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { g_hInstance = hInstance; Register("Main", WndProc); HWND hWnd = CreateMainWindow("Main", "window"); Display(hWnd); Message(); return 0; }
运行结果:
(1)绘制点
(2)绘制直线
(3)绘制矩形和圆
(4)绘制弧线
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步