duilib将xml和图片合并到exe资源中或者dll中

版权声明:本文为博主原创文章,欢迎交流学习 https://blog.csdn.net/u012081284/article/details/78863341

直接上demo的编写步骤了。

1.创建一个win32的空的工程用作demo工程。从其他工程复制stdafx.h、stdafx.cpp、testmain.cpp、mainwnd.h、mainwnd.cpp文件过来使用。我是从TestAlphaWindow工程中复制过来的。

2.添加现有文件,改写代码等,使之正常编译通过。具体的改写就略过了。主要是删除无用的代码。预编译头等工程设置自己也设置好。

3.这里我们是要将xml和图片等从exe的资源中加载,duilib已经考虑并提供了相关的接口。

1)在WinMain函数中设置duilib的资源路径。

CPaintManagerUI::SetInstance(hInstance);
CPaintManagerUI::SetResourceDll(hInstance);

SetResourceDll就是设置资源的dll的,意思是资源也可以放到其他dll中。这里我们是将资源放到了exe中,所以直接就设置当前的实例句柄即可,当然了,也可以省略,因为默认就是使用当前exe的实例句柄。最好还是显式的写出来。

2)添加xml到资源文件中。首先看到资源文件那里是空的,也没有resource.h等,我们先右键资源文件-添加-资源-Version双击。现在有resource.h了。也有了rc文件。此时在源代码目录下新建一个文件夹叫res。将事先准备好的xml和图片放进去。右键VS里面的资源文件,然后选择添加-资源-导入-选择图片导入。然后再次右键VS里面的资源文件,然后选择添加-现有项-选择xml添加进去。

3)图片和xml都添加进去之后,双击打开resource.h会发现,图片有对应的资源ID,然而xml却没有,不要紧,我们自己添加。右键VS资源文件下面的PackagingResToExeDemo.rc,然后查看代码,复制

/////////////////////////////////////////////////////////////////////////////
//
// PNG
//

IDB_PNG1                PNG                     "res\\close.png"
IDB_PNG2                PNG                     "res\\min.png"
#endif    // 中文(中华人民共和国) resources
/////////////////////////////////////////////////////////////////////////////
这里代码到它下面,然后改写。最终为(注意删掉了PNG下面的)#endif    // 中文(中华人民共和国) resources
/////////////////////////////////////////////////////////////////////////////
//
// PNG
//

IDB_PNG1                PNG                     "res\\close.png"
IDB_PNG2                PNG                     "res\\min.png"

/////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////
//
// XML
//

IDF_XML1                 XML                     "res\\MainWnd.xml"
#endif    // 中文(中华人民共和国) resources
/////////////////////////////////////////////////////////////////////////////
然后在双击打开resource.h,给这个xml的标识IDF_XML1分配一个资源ID,比如
#define IDF_XML1						500
在stdafx.h中添加  #include "resource.h",这样添加工作就完成了。

4)在OnCreate函数中,builder.Create接口采用CControlUI* Create(STRINGorID xml, LPCTSTR type = NULL, IDialogBuilderCallback* pCallback = NULL, CPaintManagerUI* pManager = NULL, CControlUI* pParent = NULL);这个接口即可。代码如下

LRESULT CMainWnd::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	LONG styleValue = ::GetWindowLong(*this, GWL_STYLE);
	styleValue &= ~WS_CAPTION;	//取消标题框
	styleValue &= ~WS_SIZEBOX;	//取消自动调整边框的风格,可以放置贴边自动最大化
	::SetWindowLong(*this, GWL_STYLE, styleValue | WS_CLIPSIBLINGS | WS_CLIPCHILDREN);

	m_PM.Init(m_hWnd);
	CDialogBuilder builder; 
	CControlUI* pRoot = builder.Create(IDF_XML1, _T("xml"),  this, &m_PM);
	ASSERT(pRoot && "Failed to parse XML");
	m_PM.AttachDialog(pRoot);
	m_PM.AddNotifier(this);

	return 0;
}
5)在xml中使用图片资源。首先查看PackagingResToExeDemo.rc的代码,发现close图片用的是IDB_PNG1,min用的IDB_PNG2;在双击打开resource.h查看IDB_PNG1对应的ID值是102,IDB_PNG2对应值是103。双击Mainwnd.xml在vs中打开(其他编辑器打开也行).写法如下
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<Window size="800,600" caption="0,0,0,45">
  <Font shared="true" id="0" name="微软雅黑" size="14" default="true"/>
  <VerticalLayout bordersize="1" bordercolor="#FF4B4B4B" inset="1,0,1,1" bkcolor="#FFFFFFFF">     
	<HorizontalLayout height="45" bkcolor="#FF507ED3">
      <Control />
      <Button name="minBtn" tooltip="最小化" normalimage="res='103' restype='png' source='0,0,18,18'" hotimage="res='103' restype='png' source='18,0,36,18'" width="18" padding="40,13,0,14"/>     
      <Button name="closeBtn" tooltip="关闭" normalimage="res='102' restype='png' source='0,0,18,18'" hotimage="res='102' restype='png' source='18,0,36,18'" width="18" padding="10,13,10,14"/>
    </HorizontalLayout>
  </VerticalLayout>
</Window>
不再写file='xxxx.png'了而是写res='103' restype='png'代替。

至此,已经全部结束,编译运行看结果吧。这样就是exe和dll就行了。资源文件就不用带了。有的朋友连duilib_u.dll也不想带,也简单,使用静态库即可。

静态库的使用也简单,在工程-C++-预处理器里面加上UILIB_STATIC,然后改写下stdafx.h中链接的duilib.lib为静态库的lib即可。不做细说了。具体可以去我的github代码仓库中看。我会把这个demo代码提交上去。地址:https://github.com/juhuaguai/duilib

猜你喜欢

转载自blog.csdn.net/u012081284/article/details/78863341