DuiLib实现自定义控件

在DuiLib中实现自定义控件,和QT重载类似,也是继承原有的控件只不过是要将这个控件也能在xml中进行使用。当作xml中的一个节点。下面就看一下我写的一个小例子,这个例子是重载了一个按钮(CButtonUI)。

重载CButtonUI的头文件(SubControl.h):

#ifndef SUBCONTROL_H
#define SUBCONTROL_H


#include "..\DuiLib\UIlib.h"
using namespace DuiLib;


class CButtonEXUI : public CButtonUI
{
public:
CButtonEXUI();
~CButtonEXUI();
public:
virtual LPCTSTR GetClass() const;
virtual LPVOID GetInterface(LPCTSTR pstrName);
virtual void SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue);
void SetButtonText(LPCTSTR text);
};
#endif //SUBCONTROL_H

实现文件(SubControl.cpp):

#include "SubControl.h"


CButtonEXUI::CButtonEXUI()
{


}


CButtonEXUI::~CButtonEXUI()
{


}


LPCTSTR CButtonEXUI::GetClass() const
{
// 这个是类名,去掉开头的是C字符
return _T("ButtonEXUI");
}


LPVOID CButtonEXUI::GetInterface(LPCTSTR pstrName)
{
// 这个是xml控件的名称
if (_tcscmp(pstrName, _T("ButtonEX")) == 0)
return static_cast(this);


return CControlUI::GetInterface(pstrName);
}


void CButtonEXUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue)
{
if (_tcscmp(pstrName, _T("textEX")) == 0)
{
SetButtonText(pstrValue);
}


CButtonUI::SetAttribute(pstrName, pstrValue);
}


void CButtonEXUI::SetButtonText(LPCTSTR text)
{
wchar_t str[256];
wsprintf(str, _T("扩展%s"), text);
SetText(str);
}

下面写了一个简单的使用上面自定义控件的类实现:

#include "SubControl.h"


class CLoginWin : public WindowImplBase
{
public:




protected:
private:
virtual CDuiString GetSkinFolder();
virtual CDuiString GetSkinFile();
virtual LPCTSTR GetWindowClassName(void) const;
virtual CControlUI* CreateControl(LPCTSTR pstrClass);
virtual LRESULT OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/,
LPARAM /*lParam*/, BOOL& bHandled);




virtual void OnFinalMessage(HWND hWnd);
};


DuiLib::CDuiString CLoginWin::GetSkinFolder()
{
return _T("skin\\test_02");
}


DuiLib::CDuiString CLoginWin::GetSkinFile()
{
return _T("test_02.xml");
}


LPCTSTR CLoginWin::GetWindowClassName(void) const
{


return _T("LoginWinClass");
}


CControlUI* CLoginWin::CreateControl(LPCTSTR pstrClass)
{
if (_tcscmp(pstrClass, _T("ButtonEX")) == 0) return new CButtonEXUI;
return NULL;
}


void CLoginWin::OnFinalMessage(HWND hWnd)
{
__super::OnFinalMessage(hWnd);
delete this;
}


LRESULT CLoginWin::OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
{
::PostQuitMessage(0L);
return 0;
}

下面是在win32下调用方法:

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int nCmdShow)
{
CPaintManagerUI::SetInstance(hInstance);
CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath());
HRESULT Hr = ::CoInitialize(NULL);
if (FAILED(Hr)) return 0;
CLoginWin* pFrame = new CLoginWin();
if (pFrame == NULL) return 0;
pFrame->Create(NULL, _T("测试自定义的子控件"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE);
pFrame->CenterWindow();
pFrame->ShowWindow(true);
CPaintManagerUI::MessageLoop();
::CoUninitialize();
return 0;
}

下面将xml文件的写法加入进来:



 
 
 
 
 
 
 
     
 

最后看一下效果吧

DuiLib实现自定义控件_第1张图片

可以看到在xml中我只是在按钮上添加了“123”字符串,在自定义的控件中添加了“扩展”两个字。证明重载成功。

你可能感兴趣的:(DuiLib,界面)