Duilib Button属性和HorizontalLayoutUI的扩展

本文介绍了Duilib中HorizontalLayoutUI控件的鼠标热区事件处理扩展及Button控件的属性增强,包括自定义边框绘制和状态响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于HorizontalLayoutUI属性的扩展

 duilib中的控件最基本的属性扩展,实质上是添加windows消息的处理,Duilib的源代码中没有对水平布局控件HorizontalLayoutUI做鼠标hot事件的处理。
 现在如果要求鼠标进入到HorizontalLayoutUI这个容器控件内,HorizontalLayoutUI容器自绘边框,就得加上鼠标enter和leave控件的消息处理了。
 这个属性用得比较少,而且只是做了自绘边框的处理,如果直接在源码的基础上修改,假使哪一天又要求hot事件处理其他事情,就又得修改代码了。
 所以这里直接派生了一个HorizontalLayoutUI的子类并扩充这个属性
  • 1
  • 2
  • 3
  • 4
  • 5

TileLayOut布局中的HorizontalLayoutUI容器属性扩展后的控件

class CContainerOwnerDrawingBoardUi : public CHorizontalLayoutUI
{
    private:
        bool            m_bMouseInContainer;
    public:
        CContainerOwnerDrawingBoardUi();

        //事件管理
        void    DoEvent(TEventUI& event);
        //绘制边框
        void    PaintBorder(HDC hDC);
};

void CContainerOwnerDrawingBoardUi::DoEvent(TEventUI& event)
{
    CHorizontalLayoutUI::DoEvent(event);
    if (m_iSepWidth == 0) {
        //鼠标进入控件
        if (event.Type == UIEVENT_MOUSEENTER)
        {
            m_bMouseInContainer = true;
            Invalidate();
        }
        //鼠标离开控件
        if (event.Type == UIEVENT_MOUSELEAVE)
        {
            m_bMouseInContainer = false;
            Invalidate();
        }
    }
}

CContainerOwnerDrawingBoardUi::CContainerOwnerDrawingBoardUi()
:m_bMouseInContainer = false
{   
}


void CContainerOwnerDrawingBoardUi::DoEvent(TEventUI& event)
{
    CHorizontalLayoutUI::DoEvent(event);
    if (m_iSepWidth == 0) {
        if (event.Type == UIEVENT_MOUSEENTER)
        {
            m_bMouseInContainer = true;
            Invalidate();
        }
        if (event.Type == UIEVENT_MOUSELEAVE)
        {
            m_bMouseInContainer = false;
            Invalidate();
        }
    }
}

void CContainerOwnerDrawingBoardUi::PaintBorder(HDC hDC)
{ 
    if (m_bMouseInContainer)
    {
        CRenderEngine::DrawRoundRect(hDC, m_rcItem, 1,7,7,0xFFFF9900);


        /*
        使用GDI+绘制 这处是使用GDI+绘制容器控件圆角,因为GDI+抗锯齿,但是GDI+的坐标计算存在着一些误差,所以效果不理想此处忽略
        Gdiplus::Graphics graphics(hDC);
        Gdiplus::Pen pen(0xFFFF9900, 2);
        graphics.SetSmoothingMode(Gdiplus::SmoothingModeHighQuality);//抗锯齿  
        graphics.DrawLine(&pen, m_rcItem.left,            m_rcItem.top + OFFSET_Y,  m_rcItem.left,              m_rcItem.bottom - OFFSET_Y);
        graphics.DrawLine(&pen, m_rcItem.left + OFFSET_X, m_rcItem.top,             m_rcItem.right - OFFSET_X , m_rcItem.top);
        graphics.DrawLine(&pen, m_rcItem.right,           m_rcItem.top + OFFSET_Y,  m_rcItem.right,             m_rcItem.bottom - OFFSET_Y);
        graphics.DrawLine(&pen, m_rcItem.left + OFFSET_X, m_rcItem.bottom,          m_rcItem.right - OFFSET_X,  m_rcItem.bottom);

        graphics.DrawArc(&pen, m_rcItem.left, m_rcItem.top, 2*OFFSET_X, 2*OFFSET_Y, 180, 90);
        graphics.DrawArc(&pen, m_rcItem.right - 2*OFFSET_X, m_rcItem.top, 2 * OFFSET_X, 2 * OFFSET_Y, 270, 90);
        graphics.DrawArc(&pen, m_rcItem.left, m_rcItem.bottom - 2*OFFSET_Y, 2 * OFFSET_X, 2 * OFFSET_Y, 90, 90);
        graphics.DrawArc(&pen, m_rcItem.left - 2*OFFSET_X, m_rcItem.bottom - 2*OFFSET_Y, 2 * OFFSET_X, 2 * OFFSET_Y, 0, 90);

        */
    }

    //绘制边框
    if (!m_bMouseInContainer)
    {
        CRenderEngine::DrawRoundRect(hDC, m_rcItem, 1,7,7, 0xFFCECECE);
    }
}

整个流程实质上很简单
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
Created with Raphaël 2.1.0duilib响应windows鼠标进出控件事件,由控件的Doevent接管处理事件,Invalidate()产生重绘事件Duilib执行PaintBorder决定怎样绘制边框Duilib界面发生重绘

Button属性的扩展,主要加上了响应鼠标悬浮,点击,绘制board以及bkcolor属性,直接在Duilib的源代码中进行了修改

//在button.h中添加几个属性 和 方法
DWORD m_dwHotBorderColor;
DWORD m_dwPushedBorderColor;

DWORD m_dwNormalBkColor;
DWORD m_dwPushedBkColor;

void SetHotBorderColor(DWORD dwColor);
DWORD GetHotBroderColor()const;
void SetPushedBorderColor(DWORD dwColor);
DWORD GetPushedBorderColor()const;

//在SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)方法中为这添加的几个属性赋值
if (_tcscmp(pstrName, _T("normalBkColor")) == 0)
{
    if (*pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
    LPTSTR pstr = NULL;
    DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
    SetNormalBkColor(clrColor);
}
else if (_tcscmp(pstrName, _T("pushedBkColor")) == 0)
{
    if (*pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
    LPTSTR pstr = NULL;
    DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
    SetPushedBkColor(clrColor);
}

if (_tcscmp(pstrName, _T("hotBorderColor")) == 0)
{
    if (*pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
    LPTSTR pstr = NULL;
    DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
    SetHotBorderColor(clrColor);
}
else if (_tcscmp(pstrName, _T("pushedBorderColor")) == 0)
{
    if (*pstrValue == _T('#')) pstrValue = ::CharNext(pstrValue);
    LPTSTR pstr = NULL;
    DWORD clrColor = _tcstoul(pstrValue, &pstr, 16);
    SetPushedBorderColor(clrColor);
}

//在绘制函数里面对事件进行处理
//在添加属性时主要不要破坏了duilib自身原有的属性,比如控件的endbled属性
//绘制边框
void CButtonUI::PaintBorder(HDC hDC)
{
    CControlUI::PaintBorder(hDC);
    DWORD clrColor;

    if (!IsEnabled())
    {
        clrColor = m_dwDisabledBorderColor;
        CRenderEngine::DrawRoundRect(hDC, m_rcItem, m_nBorderSize, m_cxyBorderRound.cx, m_cxyBorderRound.cy, GetAdjustColor(clrColor));
        return;
    }

if (m_nBorderSize > 0 && (m_cxyBorderRound.cx > 0 || m_cxyBorderRound.cy > 0))//画圆角边框
{

    if (((m_uButtonState & UISTATE_HOT) != 0) && (GetHotBroderColor() != 0))
    {
        clrColor = GetHotBroderColor();
        CRenderEngine::DrawRoundRect(hDC, m_rcItem, m_nBorderSize, m_cxyBorderRound.cx, m_cxyBorderRound.cy, GetAdjustColor(clrColor));
    }
    if (((m_uButtonState & UISTATE_PUSHED) != 0) && (GetPushedBorderColor() != 0))
    {
        clrColor = GetPushedBorderColor();
        CRenderEngine::DrawRoundRect(hDC, m_rcItem, m_nBorderSize, m_cxyBorderRound.cx, m_cxyBorderRound.cy, GetAdjustColor(clrColor));
    }
}
else
{
    if (m_rcBorderSize.left > 0 || m_rcBorderSize.top > 0 || m_rcBorderSize.right > 0 || m_rcBorderSize.bottom > 0)
    {

        if (((m_uButtonState & UISTATE_HOT) != 0) && (GetHotBroderColor() != 0))
        {
            clrColor = GetHotBroderColor();
        }
        if (((m_uButtonState & UISTATE_PUSHED) != 0) && (GetPushedBorderColor() != 0))
        {
            clrColor = GetPushedBorderColor();
        }

        RECT rcBorder;

        if (m_rcBorderSize.left > 0) {
            rcBorder = m_rcItem;
            rcBorder.right = m_rcItem.left;
            CRenderEngine::DrawLine(hDC, rcBorder, m_rcBorderSize.left, GetAdjustColor(clrColor), m_nBorderStyle);
        }
        if (m_rcBorderSize.top > 0) {
            rcBorder = m_rcItem;
            rcBorder.bottom = m_rcItem.top;
            CRenderEngine::DrawLine(hDC, rcBorder, m_rcBorderSize.top, GetAdjustColor(clrColor), m_nBorderStyle);
        }
        if (m_rcBorderSize.right > 0) {
            rcBorder = m_rcItem;
            rcBorder.left = m_rcItem.right;
            CRenderEngine::DrawLine(hDC, rcBorder, m_rcBorderSize.right, GetAdjustColor(clrColor), m_nBorderStyle);
        }
        if (m_rcBorderSize.bottom > 0) {
            rcBorder = m_rcItem;
            rcBorder.top = m_rcItem.bottom;
            CRenderEngine::DrawLine(hDC, rcBorder, m_rcBorderSize.bottom, GetAdjustColor(clrColor), m_nBorderStyle);
        }
    }
        else if (m_nBorderSize > 0)
        {
            if (((m_uButtonState & UISTATE_HOT) != 0) && (GetHotBroderColor() != 0))
            {
                clrColor = GetHotBroderColor();
                CRenderEngine::DrawRect(hDC, m_rcItem, m_nBorderSize, GetAdjustColor(clrColor));
            }
            if (((m_uButtonState & UISTATE_PUSHED) != 0) && (GetPushedBorderColor() != 0))
            {
                clrColor = GetPushedBorderColor();
                CRenderEngine::DrawRect(hDC, m_rcItem, m_nBorderSize, GetAdjustColor(clrColor));
            }
        }
    }
}

//绘制背景
void CButtonUI::PaintStatusImage(HDC hDC)
{
    if( IsFocused() ) m_uButtonState |= UISTATE_FOCUSED;
    else m_uButtonState &= ~ UISTATE_FOCUSED;
    if( !IsEnabled() ) m_uButtonState |= UISTATE_DISABLED;
    else m_uButtonState &= ~ UISTATE_DISABLED;

    if( (m_uButtonState & UISTATE_DISABLED) != 0 ) {
        if( !m_sDisabledImage.IsEmpty() )
        {
            if( !DrawImage(hDC, (LPCTSTR)m_sDisabledImage) ) m_sDisabledImage.Empty();
            else goto Label_ForeImage;
        }
    }
    else if( (m_uButtonState & UISTATE_PUSHED) != 0 ) {
        if( !m_sPushedImage.IsEmpty() ) {
            if( !DrawImage(hDC, (LPCTSTR)m_sPushedImage) ){
                m_sPushedImage.Empty(); 
            }
            if( !m_sPushedForeImage.IsEmpty() )
            {
                if( !DrawImage(hDC, (LPCTSTR)m_sPushedForeImage) )
                    m_sPushedForeImage.Empty();
                return;
            }
            else goto Label_ForeImage;
        }

        /////////////////////////////////////////////////////////
        //2015.9.6
        else if (m_dwPushedBkColor != 0) {
            CRenderEngine::DrawColor(hDC, m_rcPaint, GetAdjustColor(m_dwPushedBkColor));
            return;
        }
        ///////////////////////////////////////////////////////////////////////////////////
    }
    else if( (m_uButtonState & UISTATE_HOT) != 0 ) {
        if( !m_sHotImage.IsEmpty() ) {
            if( !DrawImage(hDC, (LPCTSTR)m_sHotImage) ){
                m_sHotImage.Empty();
            }
            if( !m_sHotForeImage.IsEmpty() ) {
                if( !DrawImage(hDC, (LPCTSTR)m_sHotForeImage) )
                    m_sHotForeImage.Empty();
                return;
            }
            else goto Label_ForeImage;
        }
        else if(m_dwHotBkColor != 0) {
            CRenderEngine::DrawColor(hDC, m_rcPaint, GetAdjustColor(m_dwHotBkColor));
            return;
        }
    }


    else if( (m_uButtonState & UISTATE_FOCUSED) != 0 ) {
        if( !m_sFocusedImage.IsEmpty() ) {
            if( !DrawImage(hDC, (LPCTSTR)m_sFocusedImage) ) m_sFocusedImage.Empty();
            else goto Label_ForeImage;
        }
    }

    if( !m_sNormalImage.IsEmpty() ) {
        if( !DrawImage(hDC, (LPCTSTR)m_sNormalImage) ) m_sNormalImage.Empty();

        else goto Label_ForeImage;
    }

    //////////////////////////////////////////////////////////////////////////
    //////2015.9.6
        else if(m_dwNormalBkColor != 0) {
            CRenderEngine::DrawColor(hDC, m_rcPaint, GetAdjustColor(m_dwNormalBkColor));
        }
    //////////////////////////////////////////////////////////////////////////


    if(!m_sForeImage.IsEmpty() )
        goto Label_ForeImage;

    return;

Label_ForeImage:
    if(!m_sForeImage.IsEmpty() ) {
        if( !DrawImage(hDC, (LPCTSTR)m_sForeImage) ) m_sForeImage.Empty();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值