Duilib自定义控件

1.继承基础控件

.h

namespace DuiLib
{
	class UILIB_API CListTableUI : public CListUI
	{
		DECLARE_DUICONTROL(CListTableUI)//使用方法2时才需要该行
	public:
		const static LPCTSTR ClassName;
		CListTableUI();
		LPCTSTR GetClass() const;

		void DoEvent(TEventUI& event);
	};
}

.cpp

namespace DuiLib
{
	IMPLEMENT_DUICONTROL(CListTableUI)//使用方法2时才需要该行

	const LPCTSTR CListTableUI::ClassName = _T("CListTableUI");
	CListTableUI::CListTableUI()
	{
	}

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

	void CListTableUI::DoEvent(TEventUI& event)
	{
	}
}

2.方法1:在使用的对话框中创建该控件

2.1在XML中使用

<?xml version="1.0" encoding="utf-8"?>
<Window>
  <VerticalLayout">
    <CListTableUI />
  </VerticalLayout>
</Window>

2.2生成该控件
.h

class MyUI : public DuiLib::WindowImplBase
{
public:
	MyUI() {}
	~MyUI() {}
	virtual LPCTSTR GetWindowClassName() const override { return L"MyUI"; }
	virtual DuiLib::CDuiString GetSkinFile() override { return L"MyUI.xml"; }
	virtual DuiLib::CControlUI* CreateControl(LPCTSTR pstrClass) override;

.cpp

DuiLib::CControlUI* MyUI::CreateControl(LPCTSTR pstrClass)
{
	//创建自定义控件,否则会报错提示“未知控件”
	if (_tcscmp(pstrClass, CListItem::ClassName) == 0)
		return new CListItem;
	return WindowImplBase::CreateControl(pstrClass);
}

3.方法2:在duilib源码ControlFactory.cpp中添加代码,适合自定义的标准控件

注意1:这里不能使用CListTableUI,要使用ListTable,因为在 CDialogBuilder::_Parse 解析CML时,会自动添加C*UI
注意2:需要在.h文件和.cpp文件中分别添加DECLARE_DUICONTROL(CListTableUI)与IMPLEMENT_DUICONTROL(CListTableUI)

CDuiString strClass;
strClass.Format(_T("C%sUI"), pstrClass);
pControl = dynamic_cast<CControlUI*>(CControlFactory::GetInstance()->CreateControl(strClass));

XML

<?xml version="1.0" encoding="utf-8"?>
<Window>
  <VerticalLayout">
    <ListTable />
  </VerticalLayout>
</Window>

添加的代码

CControlFactory::CControlFactory()
	{
		'''
		
		INNER_REGISTER_DUICONTROL(CListItem);
	}

参考文章:https://blog.csdn.net/zhuhongshu/article/details/45362751

你可能感兴趣的:(Duilib,c++)