圆角矩形在GDI+中没有现成的函数,所以我们需要自行绘制,采用路径绘制方法,我已经封装成类
效果图
使用方法:
1、创建路径对象
CGraphicsRoundRectPath RoundRectPath;//创建圆角矩形路径对象
2、添加矩形区域到路径
RoundRectPath.AddRoundRect(rect.X,rect.Y,rect.Width,rect.Height,5,5);
Graphics g;
g.DrawPath(&myPen,&RoundRectPath);
g.FillPath(&SolidBrush(Color(0,0,0)),&RoundRectPath);
头文件
#pragma once
#include
class CGraphicsRoundRectPath: public GraphicsPath
{
public:
CGraphicsRoundRectPath();
CGraphicsRoundRectPath(INT x, INT y, INT width, INT height, INT cornerX, INT cornerY);
public:
void AddRoundRect(INT x, INT y, INT width, INT height, INT cornerX, INT cornerY);
};
#include "StdAfx.h"
#include "GraphicsRoundRect.h"
CGraphicsRoundRectPath::CGraphicsRoundRectPath(void)
:Gdiplus::GraphicsPath()
{
}
CGraphicsRoundRectPath::CGraphicsRoundRectPath(INT x, INT y, INT width, INT height, INT cornerX, INT cornerY)
:Gdiplus::GraphicsPath()
{
AddRoundRect(x,y,width,height,cornerX,cornerY);
}
void CGraphicsRoundRectPath::AddRoundRect(INT x, INT y, INT width, INT height, INT cornerX, INT cornerY)
{
INT elWid = 2*cornerX;
INT elHei = 2*cornerY;
AddArc(x,y,elWid,elHei,180,90); // 左上角圆弧
AddLine(x+cornerX,y,x+width-cornerX,y); // 上边
AddArc(x+width-elWid,y, elWid,elHei,270,90); // 右上角圆弧
AddLine(x+width,y+cornerY, x+width,y+height-cornerY);// 右边
AddArc(x+width-elWid,y+height-elHei, elWid,elHei,0,90); // 右下角圆弧
AddLine(x+width-cornerX,y+height, x+cornerX,y+height); // 下边
AddArc(x,y+height-elHei, elWid,elHei,90,90);
AddLine(x,y+cornerY, x, y+height-cornerY);
}