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

运行效果图:

demo:

Duilib圆形头像控件

Duilib实现圆形头像控件的更多相关文章

  1. duilib进阶教程 -- Container控件的bug (14)

    在<duilib进阶教程 -- TreeView控件的bug (9)>里,Alberl发现了两个bug,并解决了其中一个,现在教程已经接近尾声啦,所以Alberl就解决了另外一个bug. ...

  2. 基于wke封装的duilib的webkit浏览器控件,可以c++与js互交,源码及demo下载地址

    转载请说明原出处,谢谢~~ 前些日子用wke内核封装了duilib的webkit浏览器控件,好多群里朋友私聊我希望可以我公布源码,今天把这个控件的源码和使用demo公布.其实这个控件封装起来没什么难度 ...

  3. duilib+cef自定义浏览器控件编译错误

    新版博客已经搭建好了,有问题请访问 htt://www.crazydebug.com 公司二期好主播项目,决定用duilib开发界面,且从ie内核换成谷歌内核 再用duilib自定义一个Browser ...

  4. 基于PhotoView的头像/圆形裁剪控件

    常见的图片裁剪有两种,一种是图片固定,裁剪框移动放缩来确定裁剪区域,早期见的比较多,缺点在于不能直接预览裁剪后的效果:还有一种现在比较普遍了,就是裁剪框固定,直接拖动缩放图片,便于预览裁剪结果. 我做 ...

  5. duilib进阶教程 -- 各种控件的响应 (10)

    到上一个教程为止,界面显示的代码就都介绍完啦,现在开始介绍控件的响应,其实在<2013 duilib入门简明教程 -- 事件处理和消息响应 (17)>里已经列出了duilib自己定义的所有 ...

  6. duilib 的IE浏览器控件去边框和去滚动条的代码

    近些天在duilib群里经常有朋友问起,怎么让duilib的IE控件可以去边框,去滚动条的问题,或者是如何去控件IE控件的行为.为了避免重复的回答,我就写一篇博文,把处理方法说明一下. duilib中 ...

  7. Duilib 鼠标在某控件例如按钮上悬停后,对目标控件操作

    其实对WM_MOUSEHOVER消息的处理,因为WindowImplBase基类中对此消息未处理,所以在自己的窗口类中实现: .h文件中加入 LRESULT OnMouseHover( UINT uM ...

  8. duilib relativepos属性导致控件错误的bug修复

    转载请说明出处,谢谢~~ 我在仿酷狗音乐播放器的开发日志系列里,曾经提到了这个bug,文章地址为:http://blog.csdn.net/zhuhongshu/article/details/381 ...

  9. 将VLC库封装为duilib的万能视频播放控件

    转载请说明出处,谢谢~~ 昨天封装好了基于webkit的浏览器控件,修复了duilib的浏览器功能的不足,而我的仿酷狗播放器项目中不光需要浏览器,同时也需要视频播放功能,也就是完成MV的功能.所以我打 ...

随机推荐

  1. 移动widget开发

    发现Oracle----php连接有很多bug无法解决,只好转向php--连接mysql数据库,并装载了mysql两个文件,跟客户端NAVICAT_FOR_MYSQL,然后直接建表,用于测试,能够连通 ...

  2. Maven打jar包

    <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactI ...

  3. 夺命雷公狗---DEDECMS----19dedecms栏目列表页的完成

    我们的栏目页表页的模版如果不清楚叫什么名,可以通过: 这里很清楚的记录着,我们来看下他: 他长得和我们以前做首页时候是不是很像呢?其实就是一样的,我们改下即可,如下所示: {dede:channel ...

  4. 一个C#序列化时循环引用的问题

    以前一直没搞懂为什么C#在做对象序列化时(Json序列化,XML序列化等)有时候会出现循环引用的问题,下面写了个例子,类People有一个属性引用了类Child,而类Child也有一个属性引用了类Pe ...

  5. Angularjs之表单实例(三)

    正确引用js css文件后可运行 <!DOCTYPE html> <html ng-app='myApp'> <head> <title>Bootstr ...

  6. MySQL5.5半同步模式

    MySQL5.5支持半同步的复制模式,什么是半同步的? 1. MySQL5.5之前的Master-SLave的复制模式是异步的,这里的文档有详细的说明;

  7. python any()和all()用法

    #any(x)判断x对象是否为空对象,如果都为空.0.false,则返回false,如果不都为空.0.false,则返回true #all(x)如果all(x)参数x对象的所有元素不为0.''.Fal ...

  8. linux 下安装gsl

    访问 http://ftp.club.cc.cmu.edu/pub/gnu/gsl/下载最新版本的,现在最新的是gsl-1.16.tar.gz,已经是2013年更新的了.然后下载 安装 简便的安装过程 ...

  9. 理解Linux中断 (1)【转】

    转自:http://blog.csdn.net/tommy_wxie/article/details/7425685 版权声明:本文为博主原创文章,未经博主允许不得转载. 一直认为,理解中断是理解内核 ...

  10. rotate array 旋转数组

    class Solution {public: void rotate(vector<int>& nums, int k) { int n=nums.size(); int i=0 ...