C语言圆角矩形画法,GDI+ 如何绘制圆角矩形(vc++)

圆角矩形在GDI+中没有现成的函数,所以我们需要自行绘制,采用路径绘制方法,我已经封装成类

效果图

0818b9ca8b590ca3270a3433284dd417.png

使用方法:

1、创建路径对象

CGraphicsRoundRectPath RoundRectPath;//创建圆角矩形路径对象2、添加矩形区域到路径

RoundRectPath.AddRoundRect(rect.X,rect.Y,rect.Width,rect.Height,5,5);

3、使用Graphics绘制路径

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);

};

CPP文件

#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);

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值