文章目录
1.COptionUI控件
- 1.COptionUI控件
- 2.作者答疑
COptionUI控件是duilib中的选项控件,它继承至CButtonUI控件,所以它有CButtonUI的几种状态的切换, 首先查看它支持的属性列表:
//属性展示
然后分析阅读源码,如下所示:
class UILIB_API COptionUI : public CButtonUI
{
DECLARE_DUICONTROL(COptionUI)
public:
COptionUI();
~COptionUI();
LPCTSTR GetClass() const;
LPVOID GetInterface(LPCTSTR pstrName);
void SetManager(CPaintManagerUI* pManager, CControlUI* pParent, bool bInit = true);
bool Activate();
void SetEnabled(bool bEnable = true);
LPCTSTR GetSelectedImage();
void SetSelectedImage(LPCTSTR pStrImage);
LPCTSTR GetSelectedHotImage();
void SetSelectedHotImage(LPCTSTR pStrImage);
LPCTSTR GetSelectedPushedImage();
void SetSelectedPushedImage(LPCTSTR pStrImage);
void SetSelectedTextColor(DWORD dwTextColor);
DWORD GetSelectedTextColor();
void SetSelectedBkColor(DWORD dwBkColor);
DWORD GetSelectBkColor();
LPCTSTR GetSelectedForedImage();
void SetSelectedForedImage(LPCTSTR pStrImage);
void SetSelectedStateCount(int nCount);
int GetSelectedStateCount() const;
virtual LPCTSTR GetSelectedStateImage();
virtual void SetSelectedStateImage(LPCTSTR pStrImage);
void SetSelectedFont(int index);
int GetSelectedFont() const;
LPCTSTR GetGroup() const;
void SetGroup(LPCTSTR pStrGroupName = NULL);
bool IsSelected() const;
virtual void Selected(bool bSelected, bool bMsg = true);
void SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue);
void PaintBkColor(HDC hDC);
void PaintStatusImage(HDC hDC);
void PaintForeImage(HDC hDC);
void PaintText(HDC hDC);
protected:
bool m_bSelected;//选择状态
CDuiString m_sGroupName;//组名
int m_iSelectedFont;
DWORD m_dwSelectedBkColor;
DWORD m_dwSelectedTextColor;
CDuiString m_sSelectedImage;
CDuiString m_sSelectedHotImage;
CDuiString m_sSelectedPushedImage;
CDuiString m_sSelectedForeImage;
int m_nSelectedStateCount;
CDuiString m_sSelectedStateImage;
};
它新增了一个m_sGroupName的组名属性,用这个来区别单选和多选的区别,名字非空就是组内单选,否则就是可以多选,它的事件处理函数与CButtonUI是一致的,点击之后调用Activate函数,源码如下所示:
bool COptionUI::Activate()
{
if( !CButtonUI::Activate() ) return false;//父类点击消息发送
if( !m_sGroupName.IsEmpty() ) Selected(true);
else Selected(!m_bSelected);
return true;
}
void COptionUI::Selected(bool bSelected, bool bMsg/* = true*/)
{
if(m_bSelected == bSelected) return;
m_bSelected = bSelected;
if( m_bSelected ) m_uButtonState |= UISTATE_SELECTED;
else m_uButtonState &= ~UISTATE_SELECTED;
if( m_pManager != NULL ) {
if( !m_sGroupName.IsEmpty() ) {//组名存在,则改变所有同组名其它控件选择状态为false
if( m_bSelected ) {
CStdPtrArray* aOptionGroup = m_pManager->GetOptionGroup(m_sGroupName);
for( int i = 0; i GetSize(); i++ ) {
COptionUI* pControl = static_cast(aOptionGroup->GetAt(i));
if( pControl != this ) {
pControl->Selected(false, bMsg);
}
}
if(bMsg) {
m_pManager->SendNotify(this, DUI_MSGTYPE_SELECTCHANGED);
}
}
}
else {
if(bMsg) {
m_pManager->SendNotify(this, DUI_MSGTYPE_SELECTCHANGED);
}
}
}
Invalidate();
}
COptionUI控件的主要作用是提供单选框、多选框和选项卡中的单选功能,由于它不像win32提供的单选框和多选框那样,提供默认的绘制样式,所以必须提供贴图来展示单选框和多选框的图像。
2.作者答疑如有疑问,请留言。