赞助

Duilib实现圆形头像控件

.h文件

 1 #ifndef __UIHEADICON_H__
 2 #define __UIHEADICON_H__
 3 
 4 
 5 /*
 6     名称:圆形头像控件(派生CButtonUI类)
 7 */
 8 
 9 
10 class  CHeadUI: public CButtonUI
11 {
12 public:
13 
14     CHeadUI();
15 
16     LPCTSTR GetClass() const;
17     LPVOID GetInterface(LPCTSTR pstrName);
18 
19     void SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue);
20 
21     void PaintBkImage(HDC hDC);
22 
23     void SetBkImage(LPCTSTR pStrImage);
24 
25     void SetDefaultBkImage(LPCTSTR pStrImage){ m_sDefaultBkImage = pStrImage; }
26     CDuiString GetDefaultBkImage(){ return m_sDefaultBkImage; }
27     void SetAutoPenColor(bool bAuto){ m_bAutoPenColor = bAuto; }
28     bool IsAutoPenColor() { return m_bAutoPenColor; }
29     void SetPenColor(DWORD dwColor){ m_dwPenColor = dwColor; }
30     DWORD GetPenColor(HDC hDC);
31     void SetPenWidth(int nPenWidth){ m_nPenWidth = nPenWidth; }
32     int GetPenWidth(){ return m_nPenWidth; }
33 
34     bool IsHeadImageExist(LPCTSTR pStrImage);
35 
36 private:
37 
38     CDuiString m_sDefaultBkImage;
39     bool m_bAutoPenColor;
40     DWORD m_dwPenColor;
41     int m_nPenWidth;
42 };
43 
44 #endif // __UIHEADICON_H__

.cpp文件

  1 #include "StdAfx.h"
  2 #include "UIHeadIcon.h"
  3 
  4 CHeadUI::CHeadUI()
  5 {
  6     m_sDefaultBkImage = _T("Head\\100_1.png");
  7     m_bAutoPenColor = false;
  8     m_dwPenColor = Color(255, 255, 255, 255).GetValue();
  9     m_nPenWidth = 2;
 10 }
 11 
 12 LPCTSTR CHeadUI::GetClass() const
 13 {
 14     return _T("HeadIconUI");
 15 }
 16 
 17 LPVOID CHeadUI::GetInterface(LPCTSTR pstrName)
 18 {
 19     if( _tcscmp(pstrName, _T("HeadIcon")) == 0 ) return static_cast<CHeadUI*>(this);
 20     return CControlUI::GetInterface(pstrName);
 21 }
 22 
 23 void CHeadUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
 24 {
 25     if (_tcscmp(pstrName, _T("defaultbkimage")) == 0) SetDefaultBkImage(pstrValue);
 26     else if (_tcscmp(pstrName, _T("bkimage")) == 0) SetBkImage(pstrValue);
 27     else if (_tcscmp(pstrName, _T("pencolor")) == 0) {
 28         while (*pstrValue > _T('\0') && *pstrValue <= _T(' ')) pstrValue = ::CharNext(pstrValue);
 29         if (*pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
 30         LPTSTR pstr = NULL;
 31         DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
 32         SetPenColor(clrColor);
 33     }
 34     else if (_tcscmp(pstrName, _T("autopencolor")) == 0) SetAutoPenColor(_tcscmp(pstrValue, _T("true")) == 0);
 35     else if (_tcscmp(pstrName, _T("penwidth")) == 0) SetPenWidth(_ttoi(pstrValue));
 36     else return CButtonUI::SetAttribute(pstrName, pstrValue);
 37 }
 38 
 39 void CHeadUI::SetBkImage(LPCTSTR pStrImage)
 40 {
 41     if (IsHeadImageExist(pStrImage))
 42     {
 43         m_sBkImage = pStrImage;
 44     }
 45     else
 46     {
 47         TCHAR tszModule[MAX_PATH + 1] = { 0 };
 48         ::GetModuleFileName(CPaintManagerUI::GetInstance(), tszModule, MAX_PATH);
 49         CDuiString sInstancePath = tszModule;
 50         int pos = sInstancePath.ReverseFind(_T('\\'));
 51         if (pos >= 0) sInstancePath = sInstancePath.Left(pos + 1);
 52         sInstancePath.Append(pStrImage);
 53 
 54         if (IsHeadImageExist(sInstancePath))
 55         {
 56             m_sBkImage = sInstancePath;
 57         }
 58         else
 59         {
 60             m_sBkImage = pStrImage;
 61         }
 62     }
 63 
 64     Invalidate();
 65 }
 66 
 67 void CHeadUI::PaintBkImage(HDC hDC)
 68 {
 69     //坐标
 70     POINT    pt = { m_rcItem.left, m_rcItem.top };
 71 
 72     //大小
 73     SIZE    sz = { m_rcItem.right - m_rcItem.left, m_rcItem.bottom - m_rcItem.top };
 74 
 75     Graphics    graphics(hDC);
 76     if (graphics.GetLastStatus() != Ok)
 77         return;
 78 
 79     //消除锯齿
 80     graphics.SetSmoothingMode(SmoothingModeHighQuality);    
 81 
 82     GraphicsPath graphicspath;
 83     if (graphicspath.GetLastStatus() != Ok)
 84         return;
 85 
 86     graphicspath.AddEllipse(pt.x, pt.y, sz.cx, sz.cy);
 87 
 88     //设置裁剪圆
 89     graphics.SetClip(&graphicspath, CombineModeReplace);
 90 
 91     Image image(GetBkImage());
 92     if (image.GetLastStatus() != Ok) 
 93         return;
 94 
 95     //绘制图像
 96     graphics.DrawImage(&image, pt.x, pt.y, sz.cx, sz.cy);
 97 
 98     //绘制一个1像素宽度的圆形,用于消除锯齿
 99     Pen    myPen(GetPenColor(hDC), GetPenWidth());
100     if (myPen.GetLastStatus() != Ok)
101         return;
102 
103     graphics.DrawEllipse(&myPen, pt.x, pt.y, sz.cx, sz.cy);
104 }
105 
106 DWORD CHeadUI::GetPenColor(HDC hDC)
107 {
108     if (IsAutoPenColor())
109     {
110         //像素值颜色取点( pt.x + 1, pt.y + 1)的值
111         RECT rc = GetPos();
112         COLORREF color = GetPixel(hDC, rc.left + 1, rc.top + 1);
113 
114         BYTE r = GetRValue(color);
115         BYTE g = GetGValue(color);
116         BYTE b = GetBValue(color);
117 
118         return Color(255, r, g, b).GetValue();
119     }
120 
121     return m_dwPenColor;
122 }
123 
124 bool CHeadUI::IsHeadImageExist(LPCTSTR pStrImage)
125 {
126     return GetFileAttributes(pStrImage) == -1 ? false : true;
127 }

 

xml里这样定义:

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

 

运行效果图:

 

demo:

Duilib圆形头像控件

posted @ 2016-10-17 21:31  车臣  阅读(2918)  评论(1编辑  收藏  举报