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;
}
运行效果图:

demo:
Duilib实现圆形头像控件的更多相关文章
- duilib进阶教程 -- Container控件的bug (14)
在<duilib进阶教程 -- TreeView控件的bug (9)>里,Alberl发现了两个bug,并解决了其中一个,现在教程已经接近尾声啦,所以Alberl就解决了另外一个bug. ...
- 基于wke封装的duilib的webkit浏览器控件,可以c++与js互交,源码及demo下载地址
转载请说明原出处,谢谢~~ 前些日子用wke内核封装了duilib的webkit浏览器控件,好多群里朋友私聊我希望可以我公布源码,今天把这个控件的源码和使用demo公布.其实这个控件封装起来没什么难度 ...
- duilib+cef自定义浏览器控件编译错误
新版博客已经搭建好了,有问题请访问 htt://www.crazydebug.com 公司二期好主播项目,决定用duilib开发界面,且从ie内核换成谷歌内核 再用duilib自定义一个Browser ...
- 基于PhotoView的头像/圆形裁剪控件
常见的图片裁剪有两种,一种是图片固定,裁剪框移动放缩来确定裁剪区域,早期见的比较多,缺点在于不能直接预览裁剪后的效果:还有一种现在比较普遍了,就是裁剪框固定,直接拖动缩放图片,便于预览裁剪结果. 我做 ...
- duilib进阶教程 -- 各种控件的响应 (10)
到上一个教程为止,界面显示的代码就都介绍完啦,现在开始介绍控件的响应,其实在<2013 duilib入门简明教程 -- 事件处理和消息响应 (17)>里已经列出了duilib自己定义的所有 ...
- duilib 的IE浏览器控件去边框和去滚动条的代码
近些天在duilib群里经常有朋友问起,怎么让duilib的IE控件可以去边框,去滚动条的问题,或者是如何去控件IE控件的行为.为了避免重复的回答,我就写一篇博文,把处理方法说明一下. duilib中 ...
- Duilib 鼠标在某控件例如按钮上悬停后,对目标控件操作
其实对WM_MOUSEHOVER消息的处理,因为WindowImplBase基类中对此消息未处理,所以在自己的窗口类中实现: .h文件中加入 LRESULT OnMouseHover( UINT uM ...
- duilib relativepos属性导致控件错误的bug修复
转载请说明出处,谢谢~~ 我在仿酷狗音乐播放器的开发日志系列里,曾经提到了这个bug,文章地址为:http://blog.csdn.net/zhuhongshu/article/details/381 ...
- 将VLC库封装为duilib的万能视频播放控件
转载请说明出处,谢谢~~ 昨天封装好了基于webkit的浏览器控件,修复了duilib的浏览器功能的不足,而我的仿酷狗播放器项目中不光需要浏览器,同时也需要视频播放功能,也就是完成MV的功能.所以我打 ...
随机推荐
- Logic BIST
Logic BIST is crucial for many applications, in particular for life-critical and mission-critical ap ...
- css 文字换行控制
强制不换行 white-space:nowrap 强制不换行,并显示省略号 word-wrap: normal; text-overflow: ellipsis; white-space: nowra ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON SetGray
zw版[转发·台湾nvp系列Delphi例程]HALCON SetGray SetGray_Delphi.PNG unit Unit1;interfaceuses Windows, Messages, ...
- 【crunch bang】论坛tint2配置讨论贴
地址: http://crunchbang.org/forums/viewtopic.php?id=3232
- MapReduce之Writable相关类
当要在进程间传递对象或持久化对象的时候,就需要序列化对象成字节流,反之当要将接收到或从磁盘读取的字节流转换为对象,就要进行反序列化.Writable是Hadoop的序列化格式,Hadoop定义了这样一 ...
- android 项目学习随笔二十一(IM、语音识别、机器人、统计、扫描二维码、条形码)
语音识别:科大讯飞语音云 http://www.xfyun.cn/ 语音机器人模拟 public class TalkBean { public String text; public boolean ...
- alias function varibales in Linux/GNU and Mac alias命令细说
细说,在古文言中是”奸细佞臣的话“,现如今成了”详细说明“的缩略. alias是MS-DOC中cmds中doskey的counterpart,是”别名“或者”化名“的意思 alias强大之处在于可以化 ...
- eclispse快捷操作
1几个最重要的快捷键 代码助手:Ctrl+Space(简体中文操作系统是Alt+/)快速修正:Ctrl+1单词补全:Alt+/打开外部Java文档:Shift+F2 显示搜索对话框:Ctrl+H快速O ...
- 【jqGrid for ASP.NET MVC Documentation】.学习笔记.2.jqGrid Model-View-Controller 分离
1 基本 分离代码 和 描述 ,在ASP.NET MVC 应用程序中是非常重要的.因此,jqGrid 的 mvc 模式使用一个共同的网格安装设置,包括 Model ,Controller 和 View ...
- vim查找/替换字符串
1.:s 命令来替换字符串. :s/vivian/sky/ 替换当前行第一个 vivian 为 sky :s/vivian/sky/g 替换当前行所有 vivian 为 sky :n,$s/vivia ...