1. 程式人生 > >duilib將xml和圖片合併到exe資源中或者dll中

duilib將xml和圖片合併到exe資源中或者dll中

直接上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