DuiLib都支持Bitmap图片,但就不能icon图标,实际上用到Icon的地方还蛮多的,后来自己先办法看能不能写个专门的控件的来显示icon图片(以前看过一篇文章《修改stb_image.c以让Duilib直接支持Ico格式的图标显示》http://www.cnblogs.com/memset/p/dui_with_ico.html,但试过了好像显示有问题,联系作者他也说这中方法有bug,所以大家可以用我这种方法试下,代码比较简单,有待完善的地方欢迎大家联系我 :)) 先定义头文件(直接加到DuiLib的库中)
.cpp文件
然后修改下UIlib.h加入库中
在UIDefine.h中加入定义
最后在UIDlgBuilder.cpp中加入一行代码
这样,就可以像库中的其他控件一样使用CIconUI控件了,在xml中使用Icon标识来就可以使用CIconUI显示图片,然后再设置icon属性(属性后面要写相对于exe的路径,要使用相对于其他资源的路径可以在LoadImage的m_sIcoImage前加m_pManager->GetResourcePath())。
- #pragma once
- namespace DuiLib
- {
- //
- // CIconUI
- class UILIB_API CIconUI : public DuiLib::CControlUI
- {
- friend class CIconWnd;
- public:
- CIconUI(void);
- ~CIconUI(void);
- LPCTSTR GetClass() const;
- LPVOID GetInterface(LPCTSTR pstrName);
- void SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue);
- void SetPos(RECT rc);
- void SetInset(RECT rc);
- void SetVisible(bool bVisible);
- void SetInternVisible(bool bVisible);
- void DoPaint(HDC hDC, const RECT& rcPaint);
- void DoEvent(TEventUI& event);
- private:
- DuiLib::CDuiString m_sIcoImage;
- RECT m_rcInset;
- };
- }
- #include "StdAfx.h"
- #include "IconUI.h"
- namespace DuiLib
- {
- //
- // CIconUI
- CIconUI::CIconUI(void)
- {
- memset(&m_rcInset, 0, sizeof(m_rcInset));
- SetAttribute(_T("bkcolor"), _T("#00000000"));
- }
- CIconUI::~CIconUI(void)
- {
- }
- LPCTSTR CIconUI::GetClass() const
- {
- return _T("IconUI");
- }
- LPVOID CIconUI::GetInterface(LPCTSTR pstrName)
- {
- if (_tcscmp(pstrName, _T("Icon")) == 0)
- return static_cast<ciconui>(this);
- return __super::GetInterface(pstrName);
- }
- void CIconUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
- {
- if(_tcscmp(pstrName, _T("icon")) == 0)
- m_sIcoImage = pstrValue;
- if (_tcscmp(pstrName, _T("inset")) == 0)
- {
- RECT rcInset = { 0 };
- LPTSTR pstr = NULL;
- rcInset.left = _tcstol(pstrValue, &pstr, 10); ASSERT(pstr);
- rcInset.top = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
- rcInset.right = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
- rcInset.bottom = _tcstol(pstr + 1, &pstr, 10); ASSERT(pstr);
- SetInset(rcInset);
- }
- Invalidate();
- __super::SetAttribute(pstrName, pstrValue);
- }
- void CIconUI::SetPos(RECT rc)
- {
- CControlUI::SetPos(rc);
- }
- void CIconUI::SetInset(RECT rc)
- {
- m_rcInset = rc;
- }
- void CIconUI::DoEvent(TEventUI& event)
- {
- CControlUI::DoEvent(event);
- }
- void CIconUI::SetVisible(bool bVisible)
- {
- CControlUI::SetVisible(bVisible);
- }
- void CIconUI::SetInternVisible(bool bVisible)
- {
- CControlUI::SetInternVisible(bVisible);
- }
- void CIconUI::DoPaint(HDC hDC, const RECT& rcPaint)
- {
- RECT rcTemp = { 0 };
- if( !::IntersectRect(&m_rcPaint, &rcPaint, &m_rcItem) ) return;
- HICON hIcon = (HICON)::LoadImage(m_pManager->GetInstance(), m_sIcoImage, IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR | LR_LOADFROMFILE | LR_DEFAULTSIZE);
- if (hIcon == NULL)
- hIcon = (HICON)::LoadImage(m_pManager->GetInstance(), m_pManager->GetResourcePath() + _T("\\Favicon\\blank_favicon.ico"), IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR | LR_LOADFROMFILE | LR_DEFAULTSIZE);
- if (NULL != hIcon)
- {
- RECT rect;
- rect.left = m_rcItem.left + m_rcInset.left;
- rect.right = m_rcItem.right - m_rcInset.right;
- rect.top = m_rcItem.top + m_rcInset.top;
- rect.bottom = m_rcItem.bottom - m_rcInset.bottom;
- ::DrawIconEx(hDC, rect.left, rect.top, hIcon, rect.right - rect.left,
- rect.bottom - rect.top, 0, NULL, DI_NORMAL);
- ::DeleteObject(hIcon);
- }
- }
- }
- </ciconui>
- #include "Control/UIActiveX.h"
- #include "Control/UIWebBrowser.h"
- #include "Control/UIFlash.h"
- #include "Control/IconUI.h"
- #define DUI_CTR_EDIT (_T("Edit"))
- #define DUI_CTR_LIST (_T("List"))
- #define DUI_CTR_TEXT (_T("Text"))
- #define DUI_CTR_ICON (_T("Icon"))
- else if( _tcscmp(pstrClass, DUI_CTR_LIST) == 0 ) pControl = new CListUI;
- else if( _tcscmp(pstrClass, DUI_CTR_TEXT) == 0 ) pControl = new CTextUI;
- else if( _tcscmp(pstrClass, DUI_CTR_ICON) == 0 ) pControl = new CIconUI;