MFC怎么绘制一个圆角矩形
MFC怎么绘制一个圆角矩形,我知道新建一个类继承CButton,然后添加DrawItem函数,在里面添加什么代码可以使按钮变成圆角矩形啊?求代码...
MFC怎么绘制一个圆角矩形, 我知道新建一个类 继承CButton,
然后添加DrawItem函数,在里面添加什么代码 可以使按钮变成圆角矩形啊?
求代码 展开
然后添加DrawItem函数,在里面添加什么代码 可以使按钮变成圆角矩形啊?
求代码 展开
3个回答
展开全部
参考代码如下:
#define OFFSET_X 5
#define OFFSET_Y 5
void DrawRoundRectange(Graphics &g,Color pens,int x,int y,int width,int height)
{
//设置画图时的滤波模式为消除锯齿现象
g.SetSmoothingMode(SmoothingModeAntiAlias);
//创建一个红色的画笔
Pen *pen = new Pen(pens,1);
//画矩形上面的边
g.DrawLine(pen,x+OFFSET_X,y,width-OFFSET_X,y);
//画矩形下面的边
g.DrawLine(pen,x+OFFSET_X,y+height,width-OFFSET_X,y+height);
//画矩形左面的边
g.DrawLine(pen,x,y+OFFSET_Y,x,y+height-OFFSET_Y);
//画矩形右面的边
g.DrawLine(pen,x+width,y+OFFSET_Y,x+width,y+height-OFFSET_Y);
//画矩形左上角的圆角
g.DrawArc(pen,x,y,OFFSET_X*2,OFFSET_Y*2,180,90);
//画矩形右下角的圆角
g.DrawArc(pen,x+width-OFFSET_X*2,y+height-OFFSET_Y*2,OFFSET_X*2,OFFSET_Y*2,360,90);
//画矩形右上角的圆角
g.DrawArc(pen,x+width-OFFSET_X*2,y,OFFSET_X*2,OFFSET_Y*2,270,90);
//画矩形左下角的圆角
g.DrawArc(pen,x,y+height-OFFSET_Y*2,OFFSET_X*2,OFFSET_Y*2,90,90);
delete pen;
}
/* 填充颜色 */
void FillRoundRectangle(Graphics &g,Color color,int x,int y,int width,int height)
{
//矩形填充的步骤:
//1、把圆角矩形画分为四个圆角上分成四个同等的扇形外加三个直角矩形
//2、先填充三个直角矩形
//3、然后填充四个角上的扇形
//创建画刷
Brush *brush = new SolidBrush(color);
//填充三个直角矩形
g.FillRectangle(brush,x,y+OFFSET_Y,width,height-OFFSET_Y*2);
g.FillRectangle(brush,x+OFFSET_X,y,width-OFFSET_X*2,height);
//填充四个角上的扇形区
//填充左上角扇形
#define PIE_OFFSET 2
g.FillPie(brush,x,y,OFFSET_X*2+PIE_OFFSET,OFFSET_Y*2+PIE_OFFSET,180,90);
//填充右下角的扇形
g.FillPie(brush,x+width-(OFFSET_X*2+PIE_OFFSET),y+height-(OFFSET_Y*2+PIE_OFFSET),OFFSET_X*2+PIE_OFFSET,OFFSET_Y*2+PIE_OFFSET,360,90);
//填充右上角的扇形
g.FillPie(brush,x+width-(OFFSET_X*2+PIE_OFFSET),y,(OFFSET_X*2+PIE_OFFSET),(OFFSET_Y*2+PIE_OFFSET),270,90);
//填充左下角的扇形
g.FillPie(brush,x,y+height-(OFFSET_X*2+PIE_OFFSET),(OFFSET_X*2+PIE_OFFSET),(OFFSET_Y*2+PIE_OFFSET),90,90);
delete brush;
}
#define OFFSET_X 5
#define OFFSET_Y 5
void DrawRoundRectange(Graphics &g,Color pens,int x,int y,int width,int height)
{
//设置画图时的滤波模式为消除锯齿现象
g.SetSmoothingMode(SmoothingModeAntiAlias);
//创建一个红色的画笔
Pen *pen = new Pen(pens,1);
//画矩形上面的边
g.DrawLine(pen,x+OFFSET_X,y,width-OFFSET_X,y);
//画矩形下面的边
g.DrawLine(pen,x+OFFSET_X,y+height,width-OFFSET_X,y+height);
//画矩形左面的边
g.DrawLine(pen,x,y+OFFSET_Y,x,y+height-OFFSET_Y);
//画矩形右面的边
g.DrawLine(pen,x+width,y+OFFSET_Y,x+width,y+height-OFFSET_Y);
//画矩形左上角的圆角
g.DrawArc(pen,x,y,OFFSET_X*2,OFFSET_Y*2,180,90);
//画矩形右下角的圆角
g.DrawArc(pen,x+width-OFFSET_X*2,y+height-OFFSET_Y*2,OFFSET_X*2,OFFSET_Y*2,360,90);
//画矩形右上角的圆角
g.DrawArc(pen,x+width-OFFSET_X*2,y,OFFSET_X*2,OFFSET_Y*2,270,90);
//画矩形左下角的圆角
g.DrawArc(pen,x,y+height-OFFSET_Y*2,OFFSET_X*2,OFFSET_Y*2,90,90);
delete pen;
}
/* 填充颜色 */
void FillRoundRectangle(Graphics &g,Color color,int x,int y,int width,int height)
{
//矩形填充的步骤:
//1、把圆角矩形画分为四个圆角上分成四个同等的扇形外加三个直角矩形
//2、先填充三个直角矩形
//3、然后填充四个角上的扇形
//创建画刷
Brush *brush = new SolidBrush(color);
//填充三个直角矩形
g.FillRectangle(brush,x,y+OFFSET_Y,width,height-OFFSET_Y*2);
g.FillRectangle(brush,x+OFFSET_X,y,width-OFFSET_X*2,height);
//填充四个角上的扇形区
//填充左上角扇形
#define PIE_OFFSET 2
g.FillPie(brush,x,y,OFFSET_X*2+PIE_OFFSET,OFFSET_Y*2+PIE_OFFSET,180,90);
//填充右下角的扇形
g.FillPie(brush,x+width-(OFFSET_X*2+PIE_OFFSET),y+height-(OFFSET_Y*2+PIE_OFFSET),OFFSET_X*2+PIE_OFFSET,OFFSET_Y*2+PIE_OFFSET,360,90);
//填充右上角的扇形
g.FillPie(brush,x+width-(OFFSET_X*2+PIE_OFFSET),y,(OFFSET_X*2+PIE_OFFSET),(OFFSET_Y*2+PIE_OFFSET),270,90);
//填充左下角的扇形
g.FillPie(brush,x,y+height-(OFFSET_X*2+PIE_OFFSET),(OFFSET_X*2+PIE_OFFSET),(OFFSET_Y*2+PIE_OFFSET),90,90);
delete brush;
}
展开全部
CRect rect = lpDrawItemStruct->rcItem;
CDC *pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
POINT pt ;
//画按钮的外边框,它是一个半径为5的圆角矩形
pt.x =5;
pt.y =5;
CPen* hOldPen = pDC->SelectObject(&m_BoundryPen);
pDC->RoundRect(&rect, pt);
pDC->SelectObject(hOldPen);
rect.DeflateRect(CSize(GetSystemMetrics(SM_CXEDGE), GetSystemMetrics(SM_CYEDGE)));
CBrush brush(m_color);
pDC->FillRect(rect,&brush );
brush.DeleteObject();
CDC *pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
POINT pt ;
//画按钮的外边框,它是一个半径为5的圆角矩形
pt.x =5;
pt.y =5;
CPen* hOldPen = pDC->SelectObject(&m_BoundryPen);
pDC->RoundRect(&rect, pt);
pDC->SelectObject(hOldPen);
rect.DeflateRect(CSize(GetSystemMetrics(SM_CXEDGE), GetSystemMetrics(SM_CYEDGE)));
CBrush brush(m_color);
pDC->FillRect(rect,&brush );
brush.DeleteObject();
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
CRect rect = lpDrawItemStruct->rcItem;
CDC *pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
POINT pt ;
//画按钮的外边框,它是一个半径为5的圆角矩形
pt.x =5;
pt.y =5;
CPen* hOldPen = pDC->SelectObject(&m_BoundryPen);
pDC->RoundRect(&rect, pt);
pDC->SelectObject(hOldPen);
rect.DeflateRect(CSize(GetSystemMetrics(SM_CXEDGE), GetSystemMetrics(SM_CYEDGE)));
CBrush brush(m_color);
pDC->FillRect(rect,&brush );
brush.DeleteObject();
CDC *pDC=CDC::FromHandle(lpDrawItemStruct->hDC);
POINT pt ;
//画按钮的外边框,它是一个半径为5的圆角矩形
pt.x =5;
pt.y =5;
CPen* hOldPen = pDC->SelectObject(&m_BoundryPen);
pDC->RoundRect(&rect, pt);
pDC->SelectObject(hOldPen);
rect.DeflateRect(CSize(GetSystemMetrics(SM_CXEDGE), GetSystemMetrics(SM_CYEDGE)));
CBrush brush(m_color);
pDC->FillRect(rect,&brush );
brush.DeleteObject();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询