• DUILIB圆形头象


    #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__

    #include "StdAfx.h"
    #include "UIHeadIcon.h"

    CHeadUI::CHeadUI()
    {
    m_sDefaultBkImage = _T("Head\100_1.png");
    m_bAutoPenColor = false;
    m_dwPenColor = Color(255, 255, 255, 255).GetValue();
    m_nPenWidth = 2;
    }

    LPCTSTR CHeadUI::GetClass() const
    {
    return _T("HeadIconUI");
    }

    LPVOID CHeadUI::GetInterface(LPCTSTR pstrName)
    {
    if( _tcscmp(pstrName, _T("HeadIcon")) == 0 ) return static_cast<CHeadUI*>(this);
    return CControlUI::GetInterface(pstrName);
    }

    void CHeadUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
    {
    if (_tcscmp(pstrName, _T("defaultbkimage")) == 0) SetDefaultBkImage(pstrValue);
    else if (_tcscmp(pstrName, _T("bkimage")) == 0) SetBkImage(pstrValue);
    else if (_tcscmp(pstrName, _T("pencolor")) == 0) {
    while (*pstrValue > _T('') && *pstrValue <= _T(' ')) pstrValue = ::CharNext(pstrValue);
    if (*pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
    LPTSTR pstr = NULL;
    DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
    SetPenColor(clrColor);
    }
    else if (_tcscmp(pstrName, _T("autopencolor")) == 0) SetAutoPenColor(_tcscmp(pstrValue, _T("true")) == 0);
    else if (_tcscmp(pstrName, _T("penwidth")) == 0) 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 + 1] = { 0 };
    ::GetModuleFileName(CPaintManagerUI::GetInstance(), tszModule, MAX_PATH);
    CDuiString sInstancePath = tszModule;
    int pos = sInstancePath.ReverseFind(_T('\'));
    if (pos >= 0) sInstancePath = sInstancePath.Left(pos + 1);
    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 + 1, rc.top + 1);

    BYTE r = GetRValue(color);
    BYTE g = GetGValue(color);
    BYTE b = GetBValue(color);

    return Color(255, r, g, b).GetValue();
    }

    return m_dwPenColor;
    }

    bool CHeadUI::IsHeadImageExist(LPCTSTR pStrImage)
    {
    return GetFileAttributes(pStrImage) == -1 ? false : true;
    }

    <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")) == 0)
    {
    return new CHeadUI;
    }
    return NULL;
    }

  • 相关阅读:
    《我们不一样》β冲刺_1
    《我们不一样》Alpha冲刺_1-5
    《我们不一样》团队项目软件设计方案
    如何绘制符合规范的流程图?
    《我们不一样》团队项目软件系统设计改进
    <Dare To Dream>团队项目用户验收评审
    Beta冲刺 第四天
    Beta冲刺 第三天
    Beta冲刺 第二天
    Beta冲刺 第一天
  • 原文地址:https://www.cnblogs.com/jackieron/p/6055161.html
Copyright © 2020-2023  润新知