【Win32】绘图入门.doc
文本预览下载声明
绘图
绘图基础
绘图设备DC(device context),即设备上下文
HDC,DC句柄,表示绘图设备句柄
GDI-Windows graphics device interface Windows绘图的API接口,封装在gdi32.dll动态链接库中
绘图颜色
计算机使用红绿蓝RGB表示,通常为24位,每种颜色8位,另外:
16位: 5, 5, 6
32位: 8, 8, 8, 8 颜色加透明度(用于3D)
颜色的使用:
COLOREF—— DWORD 无符号长整型(32位)
定义:COLOREF nColor;
1.直接使用数值(不建议)
2.使用宏RGB,RGBA
nColor = RGB(0, 0, 0);
nColor = RGBA(0, 0, 0, 0);
获取RGB值,分别获得RGB的颜色值:GetRValue / GetRValue / GetRValue,例如:
BYTE nRed = GetRValue(nColor); 获取红色的值
绘图步骤:
绘图发生在WM_PAINT消息中:
PAINTSTRUCT ps = {0}; //存储应用程序相关信息,用于绘制应用程序窗口的客户区
HDC hdc = BeginPaint(hWnd,ps);
创建画笔、画刷等,并应用。
绘制图形
SetPixel(hdc,100,100,RGB(255,0,0)); //绘制点
Ellipse(hdc, 100, 100, 500, 500); //绘制圆形
Rectangle(hdc,100, 100, 500, 500); //绘制矩形
……
取回画笔画刷,恢复系统默认画笔画刷,并释放创建的画笔画刷。
结束绘图:EndPaint(hWnd,ps);
点的绘制
GetPixel 获取指定点的颜色
COLORREF GetPixel(HDC hdc, int nXPos, int nYPos);
SetPixel 设置指定点的颜色
COLORREF SetPixel(HDC hdc, int X, int Y, COLORREF crColor);
返回值基本不用,一般情况下,返回的是设置的颜色值。
线的绘制(直线,弧线)
a.绘制直线:
MoveToEx 移动窗口的当前点到指定点,并将它指定的点作为窗口的当前点
BOOL MoveToEx(
HDC hdc, // DC句柄
int X, // 指定点的X坐标
int Y, //指定点的X坐标
LPPOINT lpPoint); // 原来的坐标,一般为NULL
LineTo 从窗口的当前点到指定点绘制一条直线,并将它指定的点作为窗口的当前点
BOOL LineTo(
HDC hdc, // DC句柄
int nXEnd, // 指定点的X坐标
int nYEnd ); //指定点的X坐标
b.绘制弧线:起点向终点 逆时针 方向,划分前两个坐标点确定的圆形。
BOOL Arc(
HDC hdc, // handle to device context
int nLeftRect, // x-coord of rectangles upper-left corner
int nTopRect, // y-coord of rectangles upper-left corner
int nRightRect, // x-coord of rectangles lower-right corner
int nBottomRect, // y-coord of rectangles lower-right corner
int nXStartArc, // 起点X坐标
int nYStartArc, // y-coord of first radial ending point
int nXEndArc, // 终点X坐标
int nYEndArc); // y-coord of second radial ending point
注:SetArcDirection(hdc,AD_CLOCKWISE); 可将方向改为顺时针
AD_COUNTERCLOCKWISE 逆时针 AD_CLOCKWISE 顺时针
绘制封闭图形
矩形 Rectangle 圆角矩形 RoundRect 圆形 Ellipse
a.绘制直角矩形
BOOL Rectangle(
HDC hdc,
显示全部