GDI+ 如何繪製圓角矩形(vc++)

圓角矩形在GDI+中沒有現成的函數,所以我們需要自行繪製,採用路徑繪製方法,我已經封裝成類

效果圖


使用方法:

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 <Gdiplus.h>

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




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章