首先介绍GDI里面的API
绘制线条:
HPEN hPen;//分配画笔句柄
hPen=CreatePen(PS_SOLID,10,RGB(0,100,10));//创建画笔
SelectObject(hdc,hPen);//将画笔选入设备
MoveToEx(hdc, 0, 0,NULL);
LineTo(hdc, 10, 10);// MoveToEx和LineTo要配套使用,一个起点,一个终点
绘制圆:
HPEN hPen;
hPen=CreatePen(PS_SOLID,10,RGB(0,0,255));
SelectObject(hdc,hPen);
Ellipse (hdc, 0, 0, 20, 30) ;
绘制矩形:
HPEN hPen;
hPen=CreatePen(PS_SOLID,10,RGB(100,0,0));
SelectObject(hdc,hPen);
Rectangle (hdc, 0, 0, 30, 30) ;
GDI+里面的API:
绘制线条:
Graphics graphics(hdc);
Pen pen(Color(255, 0, 0, 255));
graphics.DrawLine(&pen, 0, 0, 10, 10);
绘制矩形:
Graphics graphics(hdc);
Pen blackPen(Color(255, 0, 0, 0), 5);
graphics.DrawRectangle(&blackPen, 0, 0, 10, 10);
绘制具有线帽的线条:
Graphics graphics(hdc);
Pen pen(Color(255, 0, 0, 255), 8);
pen.SetStartCap(LineCapArrowAnchor);
pen.SetEndCap(LineCapRoundAnchor);
graphics.DrawLine(&pen,0, 0, 10, 10);
绘制自定义虚线:
Graphics graphics(hdc);
REAL dashValues[4] = {5, 2, 15, 4};
Pen blackPen(Color(255, 0, 0, 0), 5);
blackPen.SetDashPattern(dashValues, 4);
graphics.DrawLine(&blackPen,Point(2,2), Point(10, 10));
绘制用纹理填充的线条:
Graphics graphics(hdc);
static Image *gimg=NULL;
if (gimg==NULL)
{
gimg=Image::FromFile(_T("my.jpg"));//这里的"my.jpg"是图片资源的名称,在调、//试时你应该将图片资源放在工程目录下,在外部执行EXE文件时应该放在exe
//文件一起,这是默认的,你也可以在编译器中设置路径。
}
TextureBrush tBrush(gimg);
Pen texturedPen(&tBrush, 30);
graphics.DrawImage(gimg, 0, 0, gimg->GetWidth()-600, gimg->GetHeight());
graphics.DrawEllipse(&texturedPen,0,0,12, 12);
用纯色填充形状:
Graphics graphics(hdc);
SolidBrush solidBrush(Color(255, 255, 0, 0));
graphics.FillEllipse(&solidBrush, 0, 0, 12, 12);
用阴影图案填充形状:
Graphics graphics(hdc);
HatchBrush hBrush(HatchStyleHorizontal, Color(255, 255, 0, 0),
Color(255, 128, 255, 255));
graphics.FillEllipse(&hBrush, 0, 0, 12, 12);
用渐变色填充形状:
Graphics graphics(hdc);
LinearGradientBrush linGrBrush(
Point(0,0),
Point(12, 12),
Color(255, 255, 0, 0),
Color(255, 0, 0, 255));
Pen pen(&linGrBrush);
graphics.FillEllipse(&linGrBrush,0, 0, 12, 12);
用阴影图案纹理填充形状:
Graphics graphics(hdc);
static Image * gimg=NULL;
if (gimg==NULL)
{
gimg=Image::FromFile(_T("2011011434435349.jpg"));
}
TextureBrush tBrush(gimg);
tBrush.SetTransform(&Matrix(75.0/640.0, 0.0f, 0.0f,75.0/480.0, 0.0f, 0.0f));
graphics.FillEllipse(&tBrush,Rect( 0, 0, 20, 30));
在形状中平铺图像:
Graphics graphics(hdc);
static Image * gimg=NULL;
if (gimg==NULL)
{
gimg=Image::FromFile(_T("my.jpg"));
}
TextureBrush tBrush(gimg);
Pen blackPen(Color(255, 0, 0, 255));
graphics.FillEllipse(&tBrush, Rect(0, 0, 23, 23));
graphics.DrawRectangle(&blackPen, Rect( 40, 40, 100, 100));