您当前的位置: 首页 >  ui

插件开发

暂无认证

  • 1浏览

    0关注

    492博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

第二章 第三小节Duilib中的CButtonUI控件

插件开发 发布时间:2022-03-30 08:56:35 ,浏览量:1

文章目录
    • 1.CButtonUI控件
    • 2.作者答疑

1.CButtonUI控件

  CButtonUI控件是duilib中的按钮控件,它主要处理鼠标点击事件。在界面编程中,非常实用,它继承至CLabelUI类,   首先查看它支持的属性列表:

//使用范例

//属性列表

		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	

  然后查看它的源码如下所示:

class UILIB_API CButtonUI : public CLabelUI
    {
        DECLARE_DUICONTROL(CButtonUI)

    public:
        CButtonUI();

        LPCTSTR GetClass() const;
        LPVOID GetInterface(LPCTSTR pstrName);
        UINT GetControlFlags() const;

        bool Activate();
        void SetEnabled(bool bEnable = true);
        void DoEvent(TEventUI& event);
        
		//按钮贴图
        virtual LPCTSTR GetNormalImage();//正常贴图
        virtual void SetNormalImage(LPCTSTR pStrImage);
        virtual LPCTSTR GetHotImage();//热点贴图
        virtual void SetHotImage(LPCTSTR pStrImage);
        virtual LPCTSTR GetPushedImage();//按下贴图
        virtual void SetPushedImage(LPCTSTR pStrImage);
        virtual LPCTSTR GetFocusedImage();//拥有焦点贴图
        virtual void SetFocusedImage(LPCTSTR pStrImage);
        virtual LPCTSTR GetDisabledImage();
        virtual void SetDisabledImage(LPCTSTR pStrImage);
        virtual LPCTSTR GetHotForeImage();
        virtual void SetHotForeImage(LPCTSTR pStrImage);
        void SetStateCount(int nCount);
        int GetStateCount() const;
        virtual LPCTSTR GetStateImage();
        virtual void SetStateImage(LPCTSTR pStrImage);
		
		//Tab数字
        void BindTabIndex(int _BindTabIndex);
        void BindTabLayoutName(LPCTSTR _TabLayoutName);
        void BindTriggerTabSel(int _SetSelectIndex = -1);
        void RemoveBindTabIndex();
        int     GetBindTabLayoutIndex();
        LPCTSTR GetBindTabLayoutName();

		//几种不同状态下的文本字体
        void SetHotFont(int index);
        int GetHotFont() const;
        void SetPushedFont(int index);
        int GetPushedFont() const;
        void SetFocusedFont(int index);
        int GetFocusedFont() const;
        
		//几种不同状态下的文本颜色
        void SetHotBkColor(DWORD dwColor);
        DWORD GetHotBkColor() const;
        void SetPushedBkColor(DWORD dwColor);
        DWORD GetPushedBkColor() const;
        void SetDisabledBkColor(DWORD dwColor);
        DWORD GetDisabledBkColor() const;
        void SetHotTextColor(DWORD dwColor);
        DWORD GetHotTextColor() const;
        void SetPushedTextColor(DWORD dwColor);
        DWORD GetPushedTextColor() const;
        void SetFocusedTextColor(DWORD dwColor);
        DWORD GetFocusedTextColor() const;
        void SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue);

        SIZE EstimateSize(SIZE szAvailable);
		
		//文本
        void PaintText(HDC hDC);

        void PaintBkColor(HDC hDC);
        void PaintStatusImage(HDC hDC);
        void PaintForeImage(HDC hDC);

    protected:
        UINT m_uButtonState;

        int        m_iHotFont;
        int        m_iPushedFont;
        int        m_iFocusedFont;

        DWORD m_dwHotBkColor;
        DWORD m_dwPushedBkColor;
        DWORD m_dwDisabledBkColor;
        DWORD m_dwHotTextColor;
        DWORD m_dwPushedTextColor;
        DWORD m_dwFocusedTextColor;

        CDuiString m_sNormalImage;
        CDuiString m_sHotImage;
        CDuiString m_sHotForeImage;
        CDuiString m_sPushedImage;
        CDuiString m_sPushedForeImage;
        CDuiString m_sFocusedImage;
        CDuiString m_sDisabledImage;
        int m_nStateCount;
        CDuiString m_sStateImage;

        int            m_iBindTabIndex;
        CDuiString    m_sBindTabLayoutName;
    };

  在事件函数中,有不同状态的鼠标状态的切换,代码如下:

void CButtonUI::DoEvent(TEventUI& event)
    {
        if( !IsMouseEnabled() && event.Type > UIEVENT__MOUSEBEGIN && event.Type DoEvent(event);
            else CLabelUI::DoEvent(event);
            return;
        }

        if( event.Type == UIEVENT_SETFOCUS ) 
        {
            Invalidate();
        }
        if( event.Type == UIEVENT_KILLFOCUS ) 
        {
            Invalidate();
        }
        if( event.Type == UIEVENT_KEYDOWN )
        {
            if (IsKeyboardEnabled()) {
                if( event.chKey == VK_SPACE || event.chKey == VK_RETURN ) {
                    Activate();
                    return;
                }
            }
        }        
        if( event.Type == UIEVENT_BUTTONDOWN || event.Type == UIEVENT_DBLCLICK)
        {
            if( ::PtInRect(&m_rcItem, event.ptMouse) && IsEnabled() ) {
                m_uButtonState |= UISTATE_PUSHED | UISTATE_CAPTURED;
                Invalidate();
            }
            return;
        }    
        if( event.Type == UIEVENT_MOUSEMOVE )
        {
            if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
                if( ::PtInRect(&m_rcItem, event.ptMouse) ) m_uButtonState |= UISTATE_PUSHED;
                else m_uButtonState &= ~UISTATE_PUSHED;
                Invalidate();
            }
            return;
        }
        if( event.Type == UIEVENT_BUTTONUP )
        {
            if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
                m_uButtonState &= ~(UISTATE_PUSHED | UISTATE_CAPTURED);
                Invalidate();
                if( ::PtInRect(&m_rcItem, event.ptMouse) ) Activate();//按下弹起发送消息发送
            }
            return;
        }
        if( event.Type == UIEVENT_CONTEXTMENU )
        {
            if( IsContextMenuUsed() ) {
                m_pManager->SendNotify(this, DUI_MSGTYPE_MENU, event.wParam, event.lParam);
            }
            return;
        }
        if( event.Type == UIEVENT_MOUSEENTER )
        {
            if( IsEnabled() ) {
                m_uButtonState |= UISTATE_HOT;
                Invalidate();
            }
        }
        if( event.Type == UIEVENT_MOUSELEAVE )
        {
            if( IsEnabled() ) {
                m_uButtonState &= ~UISTATE_HOT;
                Invalidate();
            }
        }
        CLabelUI::DoEvent(event);
    }
    
	bool CButtonUI::Activate()
    {
        if( !CControlUI::Activate() ) return false;
        if( m_pManager != NULL )
        {
            m_pManager->SendNotify(this, DUI_MSGTYPE_CLICK);//发送点击事件
            BindTriggerTabSel();
        }
        return true;
    }

  在鼠标点击弹起时,发送点击消息,可以在注册了notify通知的对象中响应,一般在项目中,继承实现了窗口对象,可以在窗口的notify函数中,进行处理,在第三章的范例中,会进行一个例子的讲述。如果制作比较绚丽的按钮,就需要使用贴图,采用几种不同状态下的贴图就能满足基本用途。

2.作者答疑

  如有疑问,请留言。

关注
打赏
1665481431
查看更多评论
立即登录/注册

微信扫码登录

0.0826s