首页 技术 正文
技术 2022年11月13日
0 收藏 408 点赞 4,690 浏览 3700 个字

.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圆形头像控件

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,817
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,900