下载DuiLib_Ultimate库


库链接:https://github.com/qdtroy/DuiLib_Ultimate

下载后目录结构如下:
2022-08-30T09:21:23.png

编译生成库文件


注意:本文只介绍静态库版本,动态库请看下方作者教程

升级项目

打开项目后提示需要升级,那么这里点击确认即可
2022-08-30T09:22:41.png
升级完成后,项目结构如下图所示
2022-08-30T09:23:21.png

编译方式

该项目有很多种编译方式,详情请看下表。

编译模式 描述
Debug 使用 Unicode 字符集Debug模式动态库版本
DebugA 使用多字节字符集Debug模式动态库版本
Release 使用 Unicode 字符集Release模式动态库版本
ReleaseA 使用多字节字符集Release模式动态库版本
SDebug 使用 Unicode 字符集Debug模式静态库版本
SDebugA 使用多字节字符集Debug模式静态库版本
SRelease 使用 Unicode 字符集Release模式静态库版本
SReleaseA 使用多字节字符集Release模式静态库版本

编译&归类

我们将所有静态库版本均编译出来,并对其进行目录归类。

这里我们只演示一种,其余与之原理相同

  1. 选择编译模式为SDebug,版本为Win32。
    2022-08-30T09:37:27.png
  2. 点击重新生成,不出意外均会成功。出意外只能求助作者了,我编译过程中未发现问题。
    2022-08-30T09:39:27.png
    2022-08-30T09:40:28.png
  3. 将编译好的文件放入你选择的路径/Lib/x32/下进行归类
  4. 编译其余所有版本并对齐进行归类…
  5. 最终目录结构如下
    2022-08-30T09:50:09.png
    2022-08-30T09:50:22.png
    2022-08-30T09:50:34.png

配置项目


更改输出目录为:$(ProjectDir)..\bin\

2022-08-30T09:57:21.png

设置VC++目录中的包含目录和库目录

包含目录为DuiLib_Ultimate-master项目中的DuiLib文件目录。库目录为我们之前编译好的静态库存放目录
2022-08-30T10:00:40.png
2022-08-30T10:02:08.png
库目录我放在了DuiLib_Ultimate-master项目中的Lib文件夹中
2022-08-30T10:02:47.png

添加预编译宏

在预处理器中,添加宏UILIB_STATIC
2022-08-30T10:03:55.png
2022-08-30T10:04:35.png
2022-08-30T10:05:28.png

修改运行时库

2022-08-31T02:30:17.png

2022-08-31T02:31:05.png

创建项目


1. 创建Windows 桌面向导

2022-08-30T09:52:59.png

2. 项目名写成自己的就好

2022-08-30T09:53:55.png

3. 选择桌面应用程序

2022-08-30T09:54:24.png

4. 删除所有代码除了main函数

2022-08-30T09:55:31.png

5. 在framework.h文件中添加项目环境

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// header.h: 标准系统包含文件的包含文件,
// 或特定于项目的包含文件
//

#pragma once

#include "targetver.h"
#define WIN32_LEAN_AND_MEAN // 从 Windows 头文件中排除极少使用的内容
// Windows 头文件
#include <windows.h>
// C 运行时头文件
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>

// 包含库头文件
#include "UIlib.h"

// 引入DuiLib命名空间
using namespace DuiLib;

// 注意,这里我们是64位版本,所以需要x64库目录下的静态库文件
#ifdef _DEBUG
#pragma comment(lib, "//x64//DuiLib_d.lib")
#else
#pragma comment(lib, "//x64//DuiLib.lib")
#endif // _DEBUG

6. 在wWinMain中添加DuiLib相关代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

// 程序实例
CPaintManagerUI::SetInstance(hInstance);
// 配置资源路径
// 资源类型
CPaintManagerUI::SetResourceType(UILIB_FILE);
// 资源路径: 执行程序同目录的Skins文件夹下
// CPaintManagerUI::GetInstancePath() 执行程序目录
CDuiString strResPath;
strResPath.Format(_T("%sSkins"), CPaintManagerUI::GetInstancePath());
CPaintManagerUI::SetResourcePath(strResPath);

// 等待创建窗口代码

// 启动消息循环
CPaintManagerUI::MessageLoop();
return 0;
}

7. 创建MainWnd类,类名可以自定义,基类为WindowImplBase

生成后有一句include的代码,将其删除。改为引入framework.h
2022-08-30T10:18:43.png
2022-08-30T10:19:42.png

8. 创建构造函数,析构函数,实现父类的纯虚函数。

父类的纯虚函数有如下两个

1
2
3
4
    // 返回窗口UI布局文件名
virtual CDuiString GetSkinFile() = 0;
// 返回窗口类名
virtual LPCTSTR GetWindowClassName(void) const = 0 ;

在父类类名处右键,点击快速操作和重构
2022-08-31T02:10:24.png
点击实现基”WindowImplBase”的纯虚方法
2022-08-31T02:10:49.png
自动生成的头文件没有使用public修饰,记得自己加上
2022-08-31T02:12:12.png
实现纯虚方法
2022-08-31T02:20:53.png

9. 在wWinMain中填写创建窗口代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// DuilibDemo.cpp : 定义应用程序的入口点。
//

#include "framework.h"
#include "DuilibDemo.h"
#include "MainWnd.h"

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

CPaintManagerUI::SetInstance(hInstance);
CPaintManagerUI::SetResourceType(UILIB_FILE);
CDuiString strResPath;
strResPath.Format(_T("%sSkins"), CPaintManagerUI::GetInstancePath().GetData());
CPaintManagerUI::SetResourcePath(strResPath);

// 等待创建窗口代码
MainWnd* pMainWnd = new MainWnd();
// 创建窗口,设置属性为可见,弹出式窗口。
pMainWnd->Create(NULL, _T("DuiLibDemo"), WS_POPUP WS_VISIBLE, NULL);
// 窗口移动至屏幕中间
pMainWnd->CenterWindow();

CPaintManagerUI::MessageLoop();
return 0;
}

10. 在Skins目录创建窗口布局文件

右键添加xml文件,在bin目录下
2022-08-31T02:26:26.png
2022-08-31T02:34:56.png

11. 添加UI筛选器

对UI文件进行归类,将MainWnd.xml在解决方案目录中拖拽到该筛选器中。
2022-08-31T02:37:05.png

12. 编写窗口布局代码

1
2
3
4
5
6
<?xml version="1.0" encoding="utf-8"?>
<Window size="800, 600" caption="0,0,0,30">
<VerticalLayout bkcolor="#ff888888" childvalign="vcenter" childalign="center">
<Button name="closebtn" bkcolor="#ffff0000" width="100" height="30" text="关闭窗口"/>
</VerticalLayout>
</Window>

13. 运行效果

至此,DuiLib项目已经创建完毕。打包发布的时候只需要将bin目录下的文件打包即可。对于UI布局和资源文件可以使用ZIP方式从资源中加载。如何实现该代码,请看后续文章~
文中项目链接: https://github.com/204065248/DuilibDemo
2022-08-31T02:37:26.png