duilib入門教程

duilib使用手冊

1.簡介

duilib是一個開源 的directui 界面庫,使用XML來描述界面風格,界面佈局,將界面與邏輯分離,可以快速開發美觀的界面。html

2.代碼下載與編譯

2.1代碼下載

github網址:https://github.com/duilib/duilib 下載後的代碼結構以下:git

文件結構

2.2編譯

打開根目錄的Duilib.sln,github上的工程是VS2013的,使用VS2017打開時,會提示升級工程,點升級便可。若是沒有升級成功,在工程配置中修改以下選項,選擇win10的sdk和VS2017的工具集。github

sdk與工具集修改

按照以下要求修改文件編碼,解決因爲編碼格式的問題會致使vs2017下編碼失敗的問題。 修改文件編碼函數

根據實際需求,選擇對應的工程配置,靜態庫或者動態庫,Unicode或者多字節等等。工具

工程配置

工程配置

選擇好工程配置以後,編譯工程DuiLib便可。 注意:生成duilib靜態庫時,須要添加預編譯宏UILIB_STATICoop

3.使用

3.1庫連接與頭文件包含

使用duilib界面庫,能夠分爲動態連接和靜態連接兩種方式。佈局

1.靜態連接 工程配置中添加對duilib.lib靜態庫的連接,而且添加預約義宏UILIB_STATICui

2.動態連接 將生成的duilib.dll拷貝到輸出文件的同級目錄下,同時添加對導入庫duilib.lib的靜態連接。編碼

3.頭文件 將以下標記的文件和文件夾拷貝,使用時包含UIlib.h文件便可。spa

須要拷貝的文件

3.2代碼中的使用

duilib提供了一個封裝類WindowImplBase,從該類繼承,並重寫部分函數便可,詳情參考demo代碼:

#include "..\DuiLib\UIlib.h"

#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

using namespace DuiLib;


class TestFrame : public WindowImplBase
{
public:
    TestFrame(){}
    ~TestFrame(){}

    virtual CDuiString GetSkinFolder()
    {
        return _T("");
    }
    virtual CDuiString GetSkinFile()
    {
        return _T("demo.xml");
    }
    virtual LPCTSTR GetWindowClassName(void) const
    {
        return _T("TestWindowClass");
    }
    virtual LRESULT OnClose(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
    {
        PostQuitMessage(0);
        return 0;
    }

    virtual void Notify(TNotifyUI& msg)
    {
        if (msg.sType == _T("click"))
        {
            if (msg.pSender->GetName() == _T("buttonName1"))
            {
                ::MessageBox(NULL, _T("buttonName1 has been clicked"), _T(""), NULL);
            }
        }
    }

    
private:

};

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPSTR /*lpCmdLine*/, int nCmdShow)
{
    CPaintManagerUI::SetInstance(hInstance);

    HRESULT Hr = ::CoInitialize(NULL);
    if (FAILED(Hr)) return 0;

    TestFrame* pFrame = new TestFrame();
    if (pFrame == NULL) return 0;
    pFrame->Create(NULL, _T("TestWindow"), UI_WNDSTYLE_FRAME, 0, 0, 0, 0, 0);
    pFrame->CenterWindow();
    ::ShowWindow(*pFrame, SW_SHOW);

    CPaintManagerUI::MessageLoop();

    ::CoUninitialize();
    return 0;
}

對應的xml佈局文件:

<?xml version="1.0" encoding="UTF-8"?>
<Window size="600,360">
	<VerticalLayout bkcolor="0xFF87CEEB">
		<Control />
		<HorizontalLayout height="40">
			<Control />
			<Button name="button1" text="button1" width="80" height="40" bkcolor="0xFF228B22" />
			<Control />
		</HorizontalLayout>
		<Control />
	</VerticalLayout>
</Window>

運行結果:

運行結果

關於duiliib的詳細用法,能夠參考源碼工程中的demo,基本列舉了全部控件的用法,包括各類佈局狀況。也能夠參考博客: duilib佈局詳解 duilib屬性列表

原文出處:https://www.cnblogs.com/lzm-cn/p/11422558.html

相關文章
相關標籤/搜索