gogoWebsite

Introduction to C++ lightweight interface library DuiLib (II)—Use DuiLib

Updated to 13 hours ago

Continue with the book.

We have already created the file before, and what we need to do now is to package the newly built xml file and the icon file used to form a compressed package (). Import this resource in the VS compiler. The last ID I formed here is IDR_ZIPRES. Everything is ready, let’s start the description of the code section below.

2. Create Dialog

Create a new win32 project and add the following code to the _tWinMain function:

CPaintManagerUI::SetInstance(hInstance);
	 CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath()); // Set the default path of the resource (set here to be in the same directory as exe)

	 CTestUIDlg *pFrame = new CTestUIDlg(_T(""));
	 pFrame->Create(NULL, _T("This is the simplest test exe, you can see the effect after modifying it"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE);
	 pFrame->CenterWindow();
	 pFrame->ShowModal();
	 CPaintManagerUI::MessageLoop(); //! Message loop

The above code is used to create a dialog box and display it in the center.


3. Introduction to dialog box classes

In the above code, CTestUIDlg is our dialog box class, which inherits from the WindowImplBase class. The WindowImplBase class is an XML wrapper class that encapsulates common operations for convenience for everyone to use. It is described in XML as the interface, so when using it, we must write the interface description into XML.

The following is the definition of the CTestUIDlg class:

using namespace DuiLib;
class CTestUIDlg: public WindowImplBase
{
public:
	virtual ~CTestUIDlg(void);

	virtual LPCTSTR GetWindowClassName() const
	{
		return _T("TestButtonDlg");
	}

	virtual CDuiString GetSkinFile()
	{
		return m_strXMLPath;
	}

	virtual CDuiString GetSkinFolder()
	{
		return _T("");
	}

	virtual LPCTSTR GetResourceID() const
	{
		return MAKEINTRESOURCE(IDR_ZIPRES);
	}

	virtual UILIB_RESOURCETYPE GetResourceType() const
	{
		return UILIB_ZIPRESOURCE; 
	}

	explicit	CTestUIDlg(LPCTSTR pszXMLPath);
	virtual		void InitWindow();
	virtual		CControlUI* CreateControl(LPCTSTR pstrClassName);
	virtual		void Notify(TNotifyUI& msg);
	virtual		LRESULT HandleMessage(UINT uMsg,WPARAM wParam,LPARAM lParam);

private:
	CButtonUI*			m_BtnOk;
	CButtonUI*			m_BtnCancel;
	CButtonUI*			m_BtnClose;
	CPaintManagerUI			m_pm;
	CDuiString			m_strXMLPath;
};

The CTestUIDlg class has implemented some virtual functions of the parent class before, which are used to return some basic information. The most important thing is the following 5 functions, which are analyzed one by one for you below.

1. Explicit CTestUIDlg(LPCTSTR pszXMLPath); constructor, like other constructors, does some initialization operations. The passed parameter is the xml file path, that is, the path. The specific code is as follows:

CTestUIDlg::CTestUIDlg( LPCTSTR pszXMLPath )
:m_strXMLPath(pszXMLPath)
,m_BtnOk(NULL)
,m_BtnCancel(NULL)
,m_BtnClose(NULL)
{
}


2. void InitWindow(); Initialize window information, create space based on the control unique identification we mentioned in the previous xml file. The other functions are equivalent to OnInitDialog() under MFC, used for initialization of some controls, etc. The specific code is as follows:

void CTestUIDlg::InitWindow()
{
	m_pm	= this->m_PaintManager;
	CenterWindow();
	m_BtnOk		= static_cast<CButtonUI*>(m_pm.FindControl(_T("btnTest")));
	m_BtnCancel = static_cast<CButtonUI*>(m_pm.FindControl(_T("btnTest2")));
	m_BtnClose	= static_cast<CButtonUI*>(m_pm.FindControl(_T("btnClose")));
	m_BtnClose->SetEnabled(false);
}
3. CControlUI* CreateControl(LPCTSTR pstrClassName); is used to create custom controls. For now, return NULL, which will be introduced to you later.

4. void Notify(TNotifyUI& msg); operation message response, generally used for click operations, etc. Used to process a set of messages defined by duilib itself. For the message type, please refer to duilib. The specific code is as follows:

void CTestUIDlg::Notify( TNotifyUI& msg)
 {
	 if( == _T("click") )
	 {
		 if( == m_BtnOk)
		 {
			 MessageBox(NULL,"Click OK button","sure", MB_OK);
		 }
		 else if ( == m_BtnCancel)
		 {
			 MessageBox(NULL,"Click the Cancel button","cancel",MB_OK);
		 }
		 if( == m_BtnClose)
		 {
			 PostQuitMessage(0);
		 }
	 }
 }


5. LRESULT HandleMessage(UINT uMsg,WPARAM wParam,LPARAM lParam); message response, similar to MFC's DefWindowProc(). Some of the codes are as follows:

LRESULT CTestUIDlg::HandleMessage( UINT uMsg,WPARAM wParam,LPARAM lParam )
{
	LRESULT lRes = 0;
	BOOL bHandled = TRUE;
	switch(uMsg)
	{
	case WM_SIZE:          lRes = OnSize(uMsg, wParam, lParam, bHandled); break;
	case WM_NCPAINT:       lRes = OnNcPaint(uMsg, wParam, lParam, bHandled); break;
	case WM_GETMINMAXINFO: lRes = OnGetMinMaxInfo(uMsg, wParam, lParam, bHandled); break;
	case WM_SYSCOMMAND:    lRes = OnSysCommand(uMsg, wParam, lParam, bHandled); break;	
	case WM_NCCALCSIZE:    lRes = OnNcCalcSize(uMsg, wParam, lParam, bHandled); break;
	}
	return WindowImplBase::HandleMessage(uMsg,wParam,lParam);
}


After understanding the above part, you can write a demo by yourself to see the use of DuiLib. You can quickly realize the convenience and beauty that DuiLib brings to us by yourself.