首先是在我的VS2008里面配置GDI+库,以及初始化GDI+Lib,参见http://blog.sina.com.cn/s/blog_51e67b250100ebkp.html
然后进行一些比较基础的GDI+画笔、图像处理(详细请见注释)
void CGDIAppDlg::DrawImage1(CDC* pDC)
{
//CDC* pDC=GetDC();
Graphics g(pDC->m_hDC);
Bitmap bm1(L"1.jpg");
int width=bm1.GetWidth();
int height=bm1.GetHeight();
Bitmap* pBm=bm1.Clone(0,0,width,height,PixelFormatDontCare);
g.DrawImage(pBm,0,0,width,height);
g.DrawImage(&bm1,width+10,0,width,height);
//进行毛玻璃的透明设置
ImageAttributes imageAtt;
ColorMatrix colorMatrix={
1.0f,0.0f,0.0f,0.0f,0.0f,
0.0f,0.1f,0.0f,0.0f,0.0f,
0.0f,0.0f,1.0f,0.0f,0.0f,
0.0f,0.0f,0.0f,1.0f,0.0f,
0.0f,0.0f,0.0f,0.0f,1.0f
};
imageAtt.SetColorMatrix(&colorMatrix,ColorMatrixFlagsDefault,ColorAdjustTypeBitmap);
g.DrawImage(&bm1,
Rect(width*2+20,0,width,height),
0,0,width,height,
UnitPixel,
&imageAtt);
///
//绘制文字
WCHAR str[256];
wcscpy(str,L"Hello GDI+");
Font ft(L"Arial",30);
StringFormat format;
format.SetAlignment(StringAlignmentCenter);
format.SetLineAlignment(StringAlignmentCenter);
SolidBrush black(Color(255,255,0,0));
RectF rc(10.0f,300.0f,300.0f,50.0f);
g.DrawString(
str,
wcslen(str),
&ft,
rc,
&format,
&black);
g.DrawRectangle(&Pen(Color(155,0,200),5),rc);
Image bmp(L"1.bmp");
TextureBrush textBrush(&bmp);//构造纹理画刷
Pen textPen(&textBrush,30);
//textPen.SetAlignment(PenAlignmentInset);
g.DrawEllipse(&textPen,100,300,100,200);
Pen red(Color(255,0,0),10);
Pen green(Color(0,255,0),10);
red.SetAlignment(PenAlignmentInset);
g.DrawEllipse(&red,Rect(340,240,100,40));
g.DrawEllipse(&green,Rect(340,240,100,40));
/
//画笔的缩放
Pen p(Color(155,0,200));
p.SetWidth(5);
Matrix matrix(1,0,0,2,0,0);
//p.SetTransform(&matrix);
p.ScaleTransform(1,3);
g.DrawRectangle(&p,Rect(300,500,100,40));
//画笔的旋转
Pen pen(Color::Blue,5);
pen.ScaleTransform(1,6);//垂直方向扩充六倍,水平方向不变
g.DrawEllipse(&pen,50,350,200,200);
//依次旋转画笔
pen.RotateTransform(60);
g.DrawEllipse(&pen,50,50,200,200);
pen.RotateTransform(120);
g.DrawEllipse(&pen,260,50,200,200);
pen.RotateTransform(180);
g.DrawEllipse(&pen,260,350,200,200);
//自定义画笔的画帽
GraphicsPath sPath,ePath;
//在路径中添加一个矩形
sPath.AddRectangle(Rect(-10,-5,20,10));
ePath.AddLine(0,-20,10,0);
ePath.AddLine(0,-20,-10,0);
ePath.AddLine(0,-10,10,0);
ePath.AddLine(0,-10,-10,0);
Rect rc1(10,-5,20,10);
Pen p1(Color(255,0,0,255),3);
CustomLineCap sCap(NULL,&sPath),eCap(NULL,&ePath);
g.DrawLine(&p,420,30,300,30);
for(float j=0.0f;j<6.28f;j+=0.15f)
{
g.DrawLine(&p1,600.0f,600.0f,600+200.0f*cos(j),600+200.0f*sin(j));
}
//
//this->RedrawWindow();
// enum LineJoin{斜接LineJoinMiter=0,斜切LineJoinBevel=1,圆形LineJoinRound=2,剪裁斜切LineJoinMiterClipped=3};
Pen p2(Color(255,0,0,255),15);
p2.SetLineJoin(LineJoinBevel);
g.DrawRectangle(&p2,450,300,100,40);
p2.SetLineJoin(LineJoinRound);
p2.SetColor(Color(255,255,0,0));
g.DrawRectangle(&p2,600,300,100,40);
p2.SetLineJoin(LineJoinMiter);
p2.SetColor(Color(255,155,0,200));
g.DrawRectangle(&p2,750,300,100,40);
p2.SetLineJoin(LineJoinMiterClipped);
p2.SetColor(Color(255,0,255,0));
g.DrawRectangle(&p2,900,300,100,40);
///
Point pt[4]={
Point(500,10),
Point(550,60),
Point(650,110),
Point(500,170),
};
g.SetSmoothingMode(SmoothingModeHighQuality);
p2.SetLineJoin(LineJoinMiter);
g.DrawLines(&p2,pt,4);
/
pt[0].X+=100;
pt[1].X+=100;
pt[2].X+=100;
pt[3].X+=100;
p2.SetColor(Color(255,200,0,155));
p2.SetLineJoin(LineJoinBevel);
g.DrawLines(&p2,pt,4);
//
pt[0].X+=100;
pt[1].X+=100;
pt[2].X+=100;
pt[3].X+=100;
p2.SetColor(Color(255,0,200,155));
p2.SetLineJoin(LineJoinRound);
g.DrawLines(&p2,pt,4);
///
pt[0].X+=100;
pt[1].X+=100;
pt[2].X+=100;
pt[3].X+=100;
p2.SetColor(Color(255,100,200,155));
p2.SetLineJoin(LineJoinMiterClipped);
g.DrawLines(&p2,pt,4);
}
这个函数的参数最好使用CPaintDC类型,以免绘制图像后刷新区域后消失
因此,我把这个函数放在CXXDlg::OnPaint()函数中
最后运行效果如下:(按照绘制的先后顺序,开始画的被覆盖住了)