Duilib实现圆形头像控件

.h文件

 #ifndef __UIHEADICON_H__
#define __UIHEADICON_H__ /*
名称:圆形头像控件(派生CButtonUI类)
*/ class CHeadUI: public CButtonUI
{
public: CHeadUI(); LPCTSTR GetClass() const;
LPVOID GetInterface(LPCTSTR pstrName); void SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue); void PaintBkImage(HDC hDC); void SetBkImage(LPCTSTR pStrImage); void SetDefaultBkImage(LPCTSTR pStrImage){ m_sDefaultBkImage = pStrImage; }
CDuiString GetDefaultBkImage(){ return m_sDefaultBkImage; }
void SetAutoPenColor(bool bAuto){ m_bAutoPenColor = bAuto; }
bool IsAutoPenColor() { return m_bAutoPenColor; }
void SetPenColor(DWORD dwColor){ m_dwPenColor = dwColor; }
DWORD GetPenColor(HDC hDC);
void SetPenWidth(int nPenWidth){ m_nPenWidth = nPenWidth; }
int GetPenWidth(){ return m_nPenWidth; } bool IsHeadImageExist(LPCTSTR pStrImage); private: CDuiString m_sDefaultBkImage;
bool m_bAutoPenColor;
DWORD m_dwPenColor;
int m_nPenWidth;
}; #endif // __UIHEADICON_H__

.cpp文件

 #include "StdAfx.h"
#include "UIHeadIcon.h" CHeadUI::CHeadUI()
{
m_sDefaultBkImage = _T("Head\\100_1.png");
m_bAutoPenColor = false;
m_dwPenColor = Color(, , , ).GetValue();
m_nPenWidth = ;
} LPCTSTR CHeadUI::GetClass() const
{
return _T("HeadIconUI");
} LPVOID CHeadUI::GetInterface(LPCTSTR pstrName)
{
if( _tcscmp(pstrName, _T("HeadIcon")) == ) return static_cast<CHeadUI*>(this);
return CControlUI::GetInterface(pstrName);
} void CHeadUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
{
if (_tcscmp(pstrName, _T("defaultbkimage")) == ) SetDefaultBkImage(pstrValue);
else if (_tcscmp(pstrName, _T("bkimage")) == ) SetBkImage(pstrValue);
else if (_tcscmp(pstrName, _T("pencolor")) == ) {
while (*pstrValue > _T('\0') && *pstrValue <= _T(' ')) pstrValue = ::CharNext(pstrValue);
if (*pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
LPTSTR pstr = NULL;
DWORD clrColor = _tcstoul(pstrValue, &pstr, );
SetPenColor(clrColor);
}
else if (_tcscmp(pstrName, _T("autopencolor")) == ) SetAutoPenColor(_tcscmp(pstrValue, _T("true")) == );
else if (_tcscmp(pstrName, _T("penwidth")) == ) SetPenWidth(_ttoi(pstrValue));
else return CButtonUI::SetAttribute(pstrName, pstrValue);
} void CHeadUI::SetBkImage(LPCTSTR pStrImage)
{
if (IsHeadImageExist(pStrImage))
{
m_sBkImage = pStrImage;
}
else
{
TCHAR tszModule[MAX_PATH + ] = { };
::GetModuleFileName(CPaintManagerUI::GetInstance(), tszModule, MAX_PATH);
CDuiString sInstancePath = tszModule;
int pos = sInstancePath.ReverseFind(_T('\\'));
if (pos >= ) sInstancePath = sInstancePath.Left(pos + );
sInstancePath.Append(pStrImage); if (IsHeadImageExist(sInstancePath))
{
m_sBkImage = sInstancePath;
}
else
{
m_sBkImage = pStrImage;
}
} Invalidate();
} void CHeadUI::PaintBkImage(HDC hDC)
{
//坐标
POINT pt = { m_rcItem.left, m_rcItem.top }; //大小
SIZE sz = { m_rcItem.right - m_rcItem.left, m_rcItem.bottom - m_rcItem.top }; Graphics graphics(hDC);
if (graphics.GetLastStatus() != Ok)
return; //消除锯齿
graphics.SetSmoothingMode(SmoothingModeHighQuality); GraphicsPath graphicspath;
if (graphicspath.GetLastStatus() != Ok)
return; graphicspath.AddEllipse(pt.x, pt.y, sz.cx, sz.cy); //设置裁剪圆
graphics.SetClip(&graphicspath, CombineModeReplace); Image image(GetBkImage());
if (image.GetLastStatus() != Ok)
return; //绘制图像
graphics.DrawImage(&image, pt.x, pt.y, sz.cx, sz.cy); //绘制一个1像素宽度的圆形,用于消除锯齿
Pen myPen(GetPenColor(hDC), GetPenWidth());
if (myPen.GetLastStatus() != Ok)
return; graphics.DrawEllipse(&myPen, pt.x, pt.y, sz.cx, sz.cy);
} DWORD CHeadUI::GetPenColor(HDC hDC)
{
if (IsAutoPenColor())
{
//像素值颜色取点( pt.x + 1, pt.y + 1)的值
RECT rc = GetPos();
COLORREF color = GetPixel(hDC, rc.left + , rc.top + ); BYTE r = GetRValue(color);
BYTE g = GetGValue(color);
BYTE b = GetBValue(color); return Color(, r, g, b).GetValue();
} return m_dwPenColor;
} bool CHeadUI::IsHeadImageExist(LPCTSTR pStrImage)
{
return GetFileAttributes(pStrImage) == - ? false : true;
}

xml里这样定义:

<HeadIcon name="photo" bkimage="photo.png" autopencolor="true" float="true" pos="260,10,0,0" width="70" height="60"/>
 CControlUI* CLoginWnd::CreateControl( LPCTSTR pstrClassName )
{
if (_tcsicmp(pstrClassName, _T("HeadIcon")) == )
{
return new CHeadUI;
}
return NULL;
}

运行效果图:

Duilib实现圆形头像控件

demo:

Duilib圆形头像控件

上一篇:修复android 5.0 Xutils的框架问题retry error, curr request is null


下一篇:hadoop2.6.0理论:hdfs、yarn、mapreduce的架构