Duilib介绍

来源:互联网 发布:文字解密软件 编辑:程序博客网 时间:2024/06/05 18:12

一、Duilib介绍

         Duilib是一个开源 的directui 界面库,开放,共享,惠众,共赢,遵循bsd协议,可以免费用于商业项目,目前支持Windows 32 、Window CE、Mobile等平台。由于简约易扩展的设计以及稳定高效的实现被各大互联网公司普遍接受,广泛应用于包括IM、视频客户端、股票行情软件、导航软件、手机辅助软件、安全软件等多个行业的众多pc客户端软件。Duilib还在不断的发展中,在文档、例子、动画、渲染引擎等多个方面将持续改进。

    提供所见即所得的开发工具UIDesigner。使用DirectUI后将使得我们的设计人员彻底解 放,不会受到开发的束缚,可以充分地发挥其设计能力来设计软件界面,并参与到用户界面开发过程中。 Duilib 目前支持Windows 32 、Window CE、Mobile等平台,使用C++开发,遵循BSD协议,可以免费用于商业项目。

https://github.com/duilib/duilib

 

二、Duilib整体结构

三、Duilib框架基本流程

1):实例句柄与渲染类关联

                   CPaintManagerUI::SetInstance(hInstance);

              CPaintManagerUI::SetResourcePath(LPCTSTRpStrPath)

2):初始化COM,为加载COM库提供支持

           ::CoInitialize

3):创建窗口类,主函数中的第二段代码主要完成的是类CDuiFrameWnd对象的创建,我们跟到对应的构造函数中发现它并没有做多余的操作,现在先不管它是如何构造的,它下面就是调用了类的Create函数创建了一个窗口,这个函数的代码如下:

HWND CWindowWnd::Create(HWND hwndParent, LPCTSTR pstrName, DWORD dwStyle, DWORD dwExStyle, int x, int y, int cx, int cy, HMENU hMenu){    if( GetSuperClassName() != NULL && !RegisterSuperclass() ) return NULL;    if( GetSuperClassName() == NULL && !RegisterWindowClass() ) return NULL;    m_hWnd = ::CreateWindowEx(dwExStyle, GetWindowClassName(), pstrName, dwStyle, x, y, cx, cy, hwndParent, hMenu, CPaintManagerUI::GetInstance(), this);    ASSERT(m_hWnd!=NULL);    return m_hWnd;}

4):调用CWindowWnd::Create创建窗口,Create函数实现了注册窗口,指定回调函数,创建窗口,处理消息等。

5):窗口居中   

           CWindowWnd::CenterWindow

6):处理消息循环

                    CPaintManagerUI::MessageLoop

7):CPaintManagerUI::MessageLoop

           ::CoUninitialize()

8):窗口类中需要重写MessageHandle函数用于处理我们感兴趣的消息。并且在最后需要调用基类的MessageHandle函数,主要是为了调用DefWindowProc处理我们不感兴趣的消息。

 

四、实例

 //Helloworld.h

#pragma once#include <ObjBase.h>#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

//Helloworld.cpp

#include "Helloworld.h"class CDuiFrameWnd : public CWindowWnd, public INotifyUI{public:virtual LPCTSTR GetWindowClassName() const { return _T("DUIMainFrame"); }UINT GetClassStyle() const { return UI_CLASSSTYLE_FRAME | CS_DBLCLKS; };void OnFinalMessage(HWND /*hWnd*/) { delete this; };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_pCloseBtn = new CButtonUI();m_pCloseBtn->SetName(_T("btnHello"));//设置控件的名称,这个名称用于标识每一个控件,必须唯一m_pCloseBtn->SetText(_T("Hello World"));//设置文字m_pCloseBtn->SetBorderSize(10);m_pCloseBtn->SetBkColor(0xFF00FF00); //设置背景色m_PaintManager.Init(m_hWnd);m_PaintManager.AttachDialog(m_pCloseBtn);m_PaintManager.AddNotifier(this);//添加控件等消息响应,这样消息就会传达到duilib的消息循环return lRes;}else if(uMsg == WM_PAINT){RECT rect = {100,100,200,150};m_pCloseBtn->SetPos(rect);}else if( uMsg == WM_DESTROY ) {::PostQuitMessage(0L);}else if( uMsg == WM_ERASEBKGND ) {return 1;}lRes = 0;if(m_PaintManager.MessageHandler(uMsg, wParam, lParam, lRes) ) return lRes;return CWindowWnd::HandleMessage(uMsg, wParam, lParam);}protected:CPaintManagerUI m_PaintManager;CControlUI *m_pCloseBtn;};//#define  SHOW_MODAL1//阻塞显示int APIENTRY _tWinMain(HINSTANCE hInstance,                     HINSTANCE hPrevInstance,                     LPTSTR    lpCmdLine,                     int       nCmdShow){CPaintManagerUI::SetInstance(hInstance);CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath());HRESULT Hr = ::CoInitialize(NULL);if( FAILED(Hr) ) return 0;#ifdef SHOW_MODALCDuiFrameWnd* pFrame = new CDuiFrameWnd();if( pFrame == NULL ) return 0;pFrame->Create(NULL, _T("简单测试demo-1"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE);pFrame->ShowModal();#elseCDuiFrameWnd* pFrame = new CDuiFrameWnd();if( pFrame == NULL ) return 0;pFrame->Create(NULL, _T("简单测试demo-2"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE);pFrame->CenterWindow();pFrame->ShowWindow(true);CPaintManagerUI::MessageLoop();#endif::CoUninitialize();return 0;}

原创粉丝点击