Duilib的使用(优化MFC界面)之 使用XML文档开发界面

1、加载XML

编写XML文件

<?xml version="1.0" encoding="UTF-8"?>
<Window size="800,600"> <!-- 窗口的初始尺寸 -->
    <HorizontalLayout bkcolor="#FF00FF00"> <!-- 整个窗口的背景 -->
	<Button name="btnHello" text="Hello World"  /> <!-- 按钮的属性,如名称、文本 -->
    </HorizontalLayout>
</Window>

引用XML文件

在wWinMain函数中设置文件默认存储路径,本文设置为EXE文件所在路径

CPaintManagerUI::SetInstance(hInstance);

CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath());   // 设置资源的默认路径(此处设置为和exe在同一目录)

调用XML资源,创建CDialogBuilder对象,调用其create函数

		CDialogBuilder builder;
		CControlUI* pRoot = builder.Create(_T("duilib.xml"), (UINT)0, NULL, &m_PaintManager);   // duilib.xml需要放到exe目录下
		ASSERT(pRoot && "Failed to parse XML");

全部源码

.h

.h
#pragma once
#define WIN32_LEAN_AND_MEAN	
#define _CRT_SECURE_NO_DEPRECATE

#include <windows.h>
#include < oleidl.h >
#include <UIlib.h>
using namespace DuiLib;

#ifdef _DEBUG
#   ifdef _UNICODE
#       pragma comment(lib, "DuiLib_ud.lib")
#   else
#       pragma comment(lib, "DuiLib_d.lib")
#   endif
#else
#   ifdef _UNICODE
#       pragma comment(lib, "DuiLib_u.lib")
#   else
#       pragma comment(lib, "DuiLib.lib")
#   endif
#endif

class CDuiFrameWnd : public CWindowWnd, public INotifyUI
{
public:
	virtual void    Notify(TNotifyUI& msg) ;
	virtual LPCTSTR GetWindowClassName() const { return _T("DUIMainFrame"); }
	

	virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);

protected:
	CPaintManagerUI m_PaintManager;
};

.cpp

#include"FileManagerDlg.h"


LRESULT CDuiFrameWnd::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	LRESULT lRes = 0;

	if( uMsg == WM_CREATE ) 
	{
		m_PaintManager.Init(m_hWnd);

		CDialogBuilder builder;
		CControlUI* pRoot = builder.Create(_T("duilib.xml"), (UINT)0, NULL, &m_PaintManager);  		ASSERT(pRoot && "Failed to parse XML");

		m_PaintManager.AttachDialog(pRoot);
		m_PaintManager.AddNotifier(this); 
		return lRes;
	}
	else if( uMsg == WM_NCACTIVATE ) 
	{
		if( !::IsIconic(m_hWnd) ) 
		{
			return (wParam == 0) ? TRUE : FALSE;
		}	
	}
	else if( uMsg == WM_NCCALCSIZE ) 
	{
		return 0;
	}
	else if( uMsg == WM_NCPAINT ) 
	{
		return 0;
	}

	if( m_PaintManager.MessageHandler(uMsg, wParam, lParam, lRes) ) 
	{
		return lRes;
	}

	return __super::HandleMessage(uMsg, wParam, lParam);
}

void   CDuiFrameWnd:: Notify(TNotifyUI& msg) 
{
	if(msg.sType == _T("click"))
	{
		if(msg.pSender->GetName() == _T("btnHello")) 
		{
			::MessageBox(NULL, _T("我是按钮"), _T("点击了按钮¥"), NULL);
		}
	}
}

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR cmdLine, int nShow){

	//::MessageBox(NULL,"测试","内容",0);
	CPaintManagerUI::SetInstance(hInstance);
	CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath());  
	CDuiFrameWnd duiFrame;
	duiFrame.Create(NULL, _T("测试"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE);
	duiFrame.ShowModal();
	duiFrame.CenterWindow();
	return 0;

}


猜你喜欢

转载自blog.csdn.net/xyb_l_code/article/details/81334911