在上一篇文章"MFC中使用Duilib--1"中, 没有用到资源文件,即xml,本篇讲怎样加载文件。
1. 在exe输出目录下,创建一个skin目录,里面放入需要用到的图片文件,以及创建一个或几个xml文件,在这里,我创建一个skin.xml文件。内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<Window mininfo="200,360" size=" 480,320 ">
<Default name="Button" value="normalimage="file='skin\button_nor.bmp' corner='4,2,4,2' fade='200' hsl='true'" hotimage="file='skin\button_over.bmp' corner='4,2,4,2' fade='200' hsl='true'" pushedimage="file='skin\button_down.bmp' corner='4,2,4,2' fade='200' hsl='true' " " />
<Font name="幼圆" size="16" default="true" />
<VerticalLayout bkcolor="#FFFF00FF">
<Button name="changeskinbtn" height="20" text="测试按钮" maxwidth="120"/>
<RichEdit name="testrichedit" bordercolor="#FF0000" bordersize="0" borderround="18,18" inset="4,2,4,2" bkcolor="#FFF2F5FA" bkcolor2="#FFA0A000" bkcolor3="#FFF2F5FA" font="1" multiline="true" vscrollbar="true" autovscroll="true" enabled="true" rich="true" readonly="false" text="测试richedit">
</RichEdit>
<Edit name="testedit" text="测试编辑框" />
</VerticalLayout>
</Window>
需要用到的资源图片有button_nor.bmp、button_over.bmp、button_down.bmp。
再将上节内容中的CTestDlg的HandleMessage函数中的WM_CREATE消息改成如下内容:
LRESULT CTestDlg::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if( uMsg == WM_CREATE ) {
//m_pm.Init(m_hWnd);
//CControlUI *pButton = new CButtonUI;
//pButton->SetName(_T("closebtn"));
//pButton->SetBkColor(0xFFFF0000);
//m_pm.AttachDialog(pButton);
//m_pm.AddNotifier(this);
m_pm.Init(m_hWnd);
CDialogBuilder builder;
CControlUI *pRoot = builder.Create(L"skin\\skin.xml", (UINT)0, NULL, &m_pm);
ASSERT(pRoot && "Failed to parse XML");
m_pm.AttachDialog(pRoot);
m_pm.AddNotifier(this);
return 0;
}
else if( uMsg == WM_DESTROY ) {
::PostQuitMessage(0);
}
LRESULT lRes = 0;
if( m_pm.MessageHandler(uMsg, wParam, lParam, lRes) ) return lRes;
return CWindowWnd::HandleMessage(uMsg, wParam, lParam);
}
编译运行即可。