本文主要是介绍圆角矩形button自绘,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
使用GDI+的函数DrawPath与FillPath画圆角矩形并填充
GdiPlusBtn .h
class CGdiPlusBtn : public CButton
{DECLARE_DYNAMIC(CGdiPlusBtn)
public:
CGdiPlusBtn();
virtual ~CGdiPlusBtn();
protected:
DECLARE_MESSAGE_MAP()
virtual void PreSubclassWindow();
public:
virtual void DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/);
};
GdiPlusBtn .cpp
void CGdiPlusBtn::PreSubclassWindow()
{
// TODO: 在此添加专用代码和/或调用基类
ModifyStyle (0, BS_OWNERDRAW);
CButton::PreSubclassWindow();
}
static GraphicsPath* CreateRoundedRectangle(CRect rect, int r,bool bFilled)
{
GraphicsPath* path = new GraphicsPath();
int r2 = (int)r / 2;
if (!bFilled)
{
rect.right-=1;
rect.bottom-=1;
}
else
{
rect.DeflateRect(2,2,2,2);
}
path->AddArc(rect.left, rect.top, r, r, 180, 90);
path->AddLine(rect.left + r2, rect.top, rect.right - r2, rect.top);
path->AddArc(rect.right - r , rect.top, r, r, 270, 90);
path->AddLine(rect.right, rect.top + r2, rect.right, rect.bottom - r2);
path->AddArc(rect.right - r , rect.bottom - r, r, r, 0, 90);
path->AddLine(rect.right - r2, rect.bottom, rect.left + r2, rect.bottom);
path->AddArc(rect.left, rect.bottom - r, r, r, 90, 90);
path->AddLine(rect.left, rect.bottom - r2, rect.left, rect.top + r2);
return path;
}
void CGdiPlusBtn::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: 添加您的代码以绘制指定项
CDC* pDC =CDC::FromHandle(lpDrawItemStruct->hDC);
CRect rect = lpDrawItemStruct->rcItem;
CRect rcText = rect;
UINT itmeState = lpDrawItemStruct->itemState;
Graphics g(pDC->m_hDC);
Pen* pen = new Pen(Color(255,255,0,0),1);
g.DrawPath(pen, CreateRoundedRectangle(rect, 20,false));
LinearGradientBrush* pBrush = new LinearGradientBrush(Rect(0,0,rect.Width(),rect.Height()),Color(255,0,255,0) ,Color(255,0,0,255) ,90);
g.FillPath(pBrush, CreateRoundedRectangle(rect, 20,true));
CString str;
GetWindowText(str);
if (str.IsEmpty()) return;
LOGFONT logfont;
GetObject(GetStockObject(SYSTEM_FONT),sizeof(LOGFONT),&logfont);
logfont.lfItalic = false;
logfont.lfHeight = 30;
logfont.lfWeight = 0;
logfont.lfCharSet = GB2312_CHARSET;
logfont.lfStrikeOut = false;
wsprintf(logfont.lfFaceName,TEXT("%s"),TEXT("微软雅黑"));
HFONT hfont = CreateFontIndirect(&logfont);
HFONT hOldFont = (HFONT)SelectObject(pDC->m_hDC,hfont);
COLORREF clrOldText = SetTextColor(pDC->m_hDC,RGB(255,0,0));
SetBkMode(pDC->m_hDC,TRANSPARENT);
pDC->DrawText(str, &rcText,DT_CENTER | DT_VCENTER | DT_SINGLELINE);
SetTextColor(pDC->m_hDC,clrOldText);
SelectObject(pDC->m_hDC,hOldFont);
DeleteObject(hfont);
}
这篇关于圆角矩形button自绘的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!