Duilib界面库使用简介

来源:互联网 发布:天刀捏脸数据男唐门 编辑:程序博客网 时间:2024/05/10 11:32

Duilib简介

windows下一款非常好的界面库,实现了UI和逻辑代码的分离,详细介绍百度即可。

使用步骤

如何下载编译duilib我就不说了,github上有编译好的,这里假设你已经有了duilib库的编译好的文件

1.引入头文件,库文件

/引入duilib头文件和库文件#include <OAIdl.h>//duilib依赖这个vc头文件,如果没有引入的话要引入以下#include <UIlib.h>using namespace DuiLib;#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

2.自定义duilib窗口类,继承CWindowWnd和INotifyUI,实现相关虚函数

详细代码下面会贴出

3.在程序入口函数中创建自定义类对象显示窗口即可

int APIENTRY _tWinMain(HINSTANCE hInstance,                     HINSTANCE hPrevInstance,                     LPTSTR    lpCmdLine,                     int       nCmdShow){//设置程序实例句柄CPaintManagerUI::SetInstance(hInstance);//创建并显示窗口CDuiFrameWnd duiFrame;duiFrame.Create(NULL, _T("DUIWnd"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE);duiFrame.ShowModal();return 0;}

完整源码

HelloDuilib.cpp

// HelloDuilib.cpp : 定义应用程序的入口点。//#include "stdafx.h"#include "HelloDuilib.h"//引入duilib头文件和库文件#include <OAIdl.h>//duilib依赖这个vc头文件,如果没有引入的话要引入以下#include <UIlib.h>using namespace DuiLib;#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//自定义duilib窗口类class CDuiFrameWnd : public CWindowWnd, public INotifyUI{public://---------------虚函数-------------://获取窗口类名virtual LPCTSTR GetWindowClassName() const { return _T("DUIMainFrame"); }//消息通知virtual void Notify(TNotifyUI& msg) {if(msg.sType == _T("click")){if(msg.pSender->GetName() == _T("btnHello")) {::MessageBox(NULL, _T("点击了按钮"), _T("提示"), NULL);}}}//消息循环处理virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam){LRESULT lRes = 0;if( uMsg == WM_CREATE ) {m_PaintManager.Init(m_hWnd);CControlUI *pWnd = NULL;#if 0//代码方式创建UI//创建一个buttonpWnd = new CButtonUI;pWnd->SetName(_T("btnHello"));//名字是唯一的,用来唯一区分该控件pWnd->SetText(_T("Hello duilib"));   // 设置文字pWnd->SetBkColor(0xFFC7EDCC);       // 设置背景色(护眼绿)#else//xml方式创建控件//声明窗口构造器CDialogBuilder builder;//根据xml创建UIpWnd = builder.Create(_T("duilib.xml"), (UINT)0, NULL, &m_PaintManager);   // duilib.xml需要放到exe目录下ASSERT(pWnd && "Failed to parse XML");#endif//关联创建的uim_PaintManager.AttachDialog(pWnd);//将当前类添加进duilib的消息循环,这样我们的Notify才能收到通知m_PaintManager.AddNotifier(this);return lRes;}// 以下3个消息WM_NCACTIVATE、WM_NCCALCSIZE、WM_NCPAINT用于屏蔽系统标题栏else if( uMsg == WM_NCACTIVATE ) {if( !::IsIconic(m_hWnd) ) {return (wParam == 0) ? TRUE : FALSE;}}else if( uMsg == WM_NCCALCSIZE ) {return 0;}else if( uMsg == WM_NCPAINT ) {return 0;}if( m_PaintManager.MessageHandler(uMsg, wParam, lParam, lRes) ) {return lRes;}return __super::HandleMessage(uMsg, wParam, lParam);}protected:CPaintManagerUI m_PaintManager;};int APIENTRY _tWinMain(HINSTANCE hInstance,                     HINSTANCE hPrevInstance,                     LPTSTR    lpCmdLine,                     int       nCmdShow){//设置程序实例句柄CPaintManagerUI::SetInstance(hInstance);// 设置资源的默认路径(此处设置为和exe在同一目录),使用xml创建布局时在此目录下查找xml文件CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath());  //创建并显示窗口CDuiFrameWnd duiFrame;duiFrame.Create(NULL, _T("DUIWnd"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE);duiFrame.CenterWindow();duiFrame.ShowModal();return 0;}

duilib.xml

<?xml version="1.0" encoding="UTF-8"?><Window size="800,600"> <!-- 窗口的初始尺寸 -->    <HorizontalLayout bkcolor="0xFFC7EDCC"> <!-- 整个窗口的背景 -->        <Button name="btnHello" text="Hello duilib"/> <!-- 按钮的属性,如名称、文本 -->    </HorizontalLayout></Window>

关键步骤说明

完整源码中的注释已经够详细了,这里再总结一下几个步骤

响应事件步骤

1. WM_CREATE的时候
//将当前类添加进duilib的消息循环,这样我们的Notify才能收到通知
m_PaintManager.AddNotifier(this);
2. 在Notify函数中, 根据消息类型和控件名称(控件要设置了名称)来响应点击事件

XML配置界面步骤

1.设置布局文件路径
// 设置资源的默认路径(此处设置为和exe在同一目录),使用xml创建布局时在此目录下查找xml文件
CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath());  
2. xml方式创建控件
//声明窗口构造器
CDialogBuilder builder;
//根据xml创建UI
pWnd = builder.Create(_T("duilib.xml"), (UINT)0, NULL, &m_PaintManager);   // duilib.xml需要放到exe目录下
ASSERT(pWnd && "Failed to parse XML");

1 0