Gemery

导航

GDI+绘制圆角矩形

1. 生成圆角矩形路径对象。

   1) 创建CGraphicsRoundRectPath对象objRoundPath。

   2) objRoundPath调用添加圆角矩形路径的函数AddRoundRect。

2. 使用Graphic::DrawPath绘制圆角矩形。

CGraphicsRoundRectPath的头文件:

1 class CGraphicsRoundRectPath: public Gdiplus::GraphicsPath
2 {
3 public:
4     CGraphicsRoundRectPath();
5     CGraphicsRoundRectPath(INT x, INT y, INT width, INT height, INT cornerX, INT cornerY);
6 
7 public:
8     void AddRoundRect(INT x, INT y, INT width, INT height, INT cornerX, INT cornerY);
9 };

CGraphicsRoundRectPath的源代码:

 1 CGraphicsRoundRectPath::CGraphicsRoundRectPath(INT x, INT y, INT width, INT height, INT cornerX, INT cornerY)
 2     :Gdiplus::GraphicsPath()
 3 {
 4     AddRoundRect(x,y,width,height,cornerX,cornerY);
 5 }
 6 void CGraphicsRoundRectPath::AddRoundRect(INT x, INT y, INT width, INT height, INT cornerX, INT cornerY)
 7 {
 8     INT elWid = 2*cornerX;
 9     INT elHei = 2*cornerY;
10 
11     AddArc(x,y,elWid,elHei,180,90); // 左上角圆弧
12     AddLine(x+cornerX,y,x+width-cornerX,y); // 上边
13 
14     AddArc(x+width-elWid,y, elWid,elHei,270,90); // 右上角圆弧
15     AddLine(x+width,y+cornerY, x+width,y+height-cornerY);// 右边
16 
17     AddArc(x+width-elWid,y+height-elHei, elWid,elHei,0,90); // 右下角圆弧
18     AddLine(x+width-cornerX,y+height, x+cornerX,y+height); // 下边
19 
20     AddArc(x,y+height-elHei, elWid,elHei,90,90); 
21     AddLine(x,y+cornerY, x, y+height-cornerY);
22 }

 

 

   

posted on 2012-07-11 17:16  Gemery  阅读(5720)  评论(0编辑  收藏  举报