图形编程:
SetPixel: 设置指定点的颜色
COLORREF SetPixel(HDC hDC,int X,int Y,COLORREF crColor);
hDC: 绘制点的DC
X,Y: 坐标位置
crColor: 设置的颜色
返回值为设置颜色之前,点(x,y)的颜色
SetPixel(hdc,100,200,RGB(255,0,0));
COLORREF GetPixel(HDC hDC,int X,int Y): 获取点的颜色
MoveToEx设置线的绘制起点:
MoveToEx(HDC hDC,int X,int Y,LPPOINT(lpPoint));
参数lpPoint就是返回旧的起点坐标,参数为NULL时,表示不返回旧的起点坐标
LineTo:画线
LineTo(HDC hDC,int X,int Y);
示例绘制一条从(100,200)到(300,400)的直线:
::MoveToEx(hdc,100,200,NULL);
::LineTo(hdc,300,400);
Rectangle: 绘制矩形
Rectangle(HDC hdc,int left,int top,int right,int bottom);
在GDI中绘制空心矩形:
::SelectObject(hdc,GetStockObject(NULL_BRUSH));
Rectangle(hdc,100,200,100+300,200+400);
Ellipse: 绘制椭圆
Ellipse(HDC hdc,int left,int top,int right,int bottom);
示例代码:
SelectObject(hdc,GetStockObject(NULL_BRUSH));
Ellipse(hdc,100,100,300,300);
Pie: 绘制饼
BOOL Pie(
HDC hdc,
int nLeftRect,
int nTopRect,
int nRightRect,
int nBottomRect,
int nXRadial1,
int nYRadial1,
int nXRadial2,
int nYRadial2
);
示例代码:
SelectObject(hdc,GetStockObject(NULL_BRUSH));
Pie(hdc,470,440,690,690,692,550,582,441);
库存画笔的选用:
SelectObject(hdc,GetStockObject(WHITE_PEN));
获取并选进宽度为1,颜色为白色,样式为实线的画笔
SelectObject(hdc,GetStockObject(BLACK_PEN));
获取并选进宽度为1,颜色为黑色,样式为实线的画笔
SelectObject(hdc,GetStockObject(NULL_PEN));
获取并选进空画笔
CreatePen: 创建画笔
HPEN CreatePen(int fnPenStyle,int nWidth,COLORREF crColor);
fnPenStyle:画笔样式
PS_SOLID: 实心笔
PS_DASH: 短线式, 要求width<=1
PS_DOT: 点式笔,
PS_DASHDOT
PS_DASHDOTDOT
PS_NULL, 笔不可见
PS_INSIDEFRAME 实心笔,但笔宽向里扩展
创建、使用和销毁自定义画笔:
{
// 创建自定义画笔
HPEN hPen=CreatePen(PS_SOLID,1,RGB(42,187,203));
// 使用自定义画笔
HPEN hPenOld=(HPEN)SelectObject(hdc,hPen);
MoveToEx(hdc,78,315,NULL);
LineTo(hdc,150,170);
// 销毁自定义画笔
SelectObject(hdc,hPenOld);
DeleteObject(hPen);
}
库存画刷:
SelectObject(hdc,GetStockObject(WHITE_BRUSH));
LTGRAY_BRUSH: 亮灰色画刷
GRAY_BRUSH: 灰色画刷
DKGRAY_BRUSH: 暗灰色画刷
BLACK_BRUSH: 黑色画刷
NULL_BRUSH: 空画刷,透明画刷
CreateSolidBrush: 创建单色画刷
HBRUSH CreateSolidBrush(COLORREF color);
示例:使用蓝色画刷填充指定矩形:
{
// 创建自定义单色画刷
HBRUSH hbr = CreateSolidBrush(RGB(0,0,255));
// 选进自定义单色画刷并备份旧画刷
HBRUSH hBrushOld=(HBRUSH)SelectObject(hdc,hbr);
// 填充指定矩形
Rectangle(hdc,480,170,520,316);
SelectObject(hdc,hBrushOld);
DeleteObject(hbr);
}
CreateHatchBrush: 创建带有阴影图案的画刷
HBRUSH CreateHatchBrush(
_int fnStyle,
_COLORREF clrref
);
CreatePatternBrush: 创建画刷
HBRUSH CreatePatternBrush(HBITMAP hbm);
SetPixel: 设置指定点的颜色
COLORREF SetPixel(HDC hDC,int X,int Y,COLORREF crColor);
hDC: 绘制点的DC
X,Y: 坐标位置
crColor: 设置的颜色
返回值为设置颜色之前,点(x,y)的颜色
SetPixel(hdc,100,200,RGB(255,0,0));
COLORREF GetPixel(HDC hDC,int X,int Y): 获取点的颜色
MoveToEx设置线的绘制起点:
MoveToEx(HDC hDC,int X,int Y,LPPOINT(lpPoint));
参数lpPoint就是返回旧的起点坐标,参数为NULL时,表示不返回旧的起点坐标
LineTo:画线
LineTo(HDC hDC,int X,int Y);
示例绘制一条从(100,200)到(300,400)的直线:
::MoveToEx(hdc,100,200,NULL);
::LineTo(hdc,300,400);
Rectangle: 绘制矩形
Rectangle(HDC hdc,int left,int top,int right,int bottom);
在GDI中绘制空心矩形:
::SelectObject(hdc,GetStockObject(NULL_BRUSH));
Rectangle(hdc,100,200,100+300,200+400);
Ellipse: 绘制椭圆
Ellipse(HDC hdc,int left,int top,int right,int bottom);
示例代码:
SelectObject(hdc,GetStockObject(NULL_BRUSH));
Ellipse(hdc,100,100,300,300);
Pie: 绘制饼
BOOL Pie(
HDC hdc,
int nLeftRect,
int nTopRect,
int nRightRect,
int nBottomRect,
int nXRadial1,
int nYRadial1,
int nXRadial2,
int nYRadial2
);
示例代码:
SelectObject(hdc,GetStockObject(NULL_BRUSH));
Pie(hdc,470,440,690,690,692,550,582,441);
库存画笔的选用:
SelectObject(hdc,GetStockObject(WHITE_PEN));
获取并选进宽度为1,颜色为白色,样式为实线的画笔
SelectObject(hdc,GetStockObject(BLACK_PEN));
获取并选进宽度为1,颜色为黑色,样式为实线的画笔
SelectObject(hdc,GetStockObject(NULL_PEN));
获取并选进空画笔
CreatePen: 创建画笔
HPEN CreatePen(int fnPenStyle,int nWidth,COLORREF crColor);
fnPenStyle:画笔样式
PS_SOLID: 实心笔
PS_DASH: 短线式, 要求width<=1
PS_DOT: 点式笔,
PS_DASHDOT
PS_DASHDOTDOT
PS_NULL, 笔不可见
PS_INSIDEFRAME 实心笔,但笔宽向里扩展
创建、使用和销毁自定义画笔:
{
// 创建自定义画笔
HPEN hPen=CreatePen(PS_SOLID,1,RGB(42,187,203));
// 使用自定义画笔
HPEN hPenOld=(HPEN)SelectObject(hdc,hPen);
MoveToEx(hdc,78,315,NULL);
LineTo(hdc,150,170);
// 销毁自定义画笔
SelectObject(hdc,hPenOld);
DeleteObject(hPen);
}
库存画刷:
SelectObject(hdc,GetStockObject(WHITE_BRUSH));
LTGRAY_BRUSH: 亮灰色画刷
GRAY_BRUSH: 灰色画刷
DKGRAY_BRUSH: 暗灰色画刷
BLACK_BRUSH: 黑色画刷
NULL_BRUSH: 空画刷,透明画刷
CreateSolidBrush: 创建单色画刷
HBRUSH CreateSolidBrush(COLORREF color);
示例:使用蓝色画刷填充指定矩形:
{
// 创建自定义单色画刷
HBRUSH hbr = CreateSolidBrush(RGB(0,0,255));
// 选进自定义单色画刷并备份旧画刷
HBRUSH hBrushOld=(HBRUSH)SelectObject(hdc,hbr);
// 填充指定矩形
Rectangle(hdc,480,170,520,316);
SelectObject(hdc,hBrushOld);
DeleteObject(hbr);
}
CreateHatchBrush: 创建带有阴影图案的画刷
HBRUSH CreateHatchBrush(
_int fnStyle,
_COLORREF clrref
);
CreatePatternBrush: 创建画刷
HBRUSH CreatePatternBrush(HBITMAP hbm);