.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. contentProvider

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...

  2. MVC权限管理系统dwpro项目分配按钮没有显示的问题

    问题如下: 修改如下: 或者(原因为这个两个地方名要一致,大小写也要注意): 效果图:

  3. java mvc控制器基本传值方式

    控制器----- @RequestMapping(value = "MatchDetail", method = RequestMethod.GET) public ModelAn ...

  4. c 语言 结构体

    一:结构体定义结构体类型变量 三种方式1st:先声明结构体类型,再定义该类型的变量struct student liming,zhangle;2nd:声明类型的同时定义变量struct student ...

  5. [slim] Slim - Faster, lightweight, a enginer for Ruby

    URL: http://slim-lang.com/ Example: doctype html html head title Slim Examples meta name="keywo ...

  6. Bonbo Git Server

    Install This page covers simple Bonobo Git Server installation. Be sure to check prerequisites page ...

  7. Test Android with QTP

    by Yaron Assa I have recently come across a plug-in to QTP that enables to automate tests on Android ...

  8. 关于UIWindow(转)

    (原文出自:http://www.cnblogs.com/wendingding/p/3770052.html,特别感谢) 一:[[UIScreen mainScreen] bounds] 和[UIS ...

  9. 使用Mysql ID自增长时 在Mapper的<insert>里添加对应的代码

    <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Long&quo ...

  10. telnet不通11211端口,防火墙

    问题描述: 按照官网步骤,虚拟机里安装并启动memcached, 虚拟机里自己telnet11211端口可以连接, 使用Xmanager22端口可以连接到虚拟机,但是始终telnet不同11211端口 ...