zoukankan      html  css  js  c++  java
  • 2021 Duilib最新入门教程(五)Duilib调用静态库示例

    原理

      静态库文件后缀名是lib,编译的时候合并到软件里面去。
      调用静态库需要两件套:1、头文件2、静态库。
      特殊的地方:调用Duilib的静态库时,需要先定义静态宏(#define UILIB_STATIC)
      

    步骤

      
    1、新建项目,选择“Windows 桌面应用程序”

      
    2、项目名字定为DuilibMinDemoStatic

      
    3、选择“桌面应用程序”和“空项目”

      
    4、添加Duilib头文件所在的目录

      
    5、复制静态库DuiLib_Static_ud.lib到代码目录下

      
    6、添加代码文件main.cpp

      

    #define UILIB_STATIC				// 声明静态宏:UILIB_STATIC(必须在UIlib.h之前声明)			
    #include "UIlib.h"		                // 引入头文件:UIlib.h						
    using namespace DuiLib;	                        // 使用命名空间:DuiLib
    #pragma comment(lib, "DuiLib_Static_ud.lib")	// 加载静态库:DuiLib_Static_ud.lib      
    
    //CWindowWnd:处理Window消息 INotifyUI:处理Duilib消息
    class CDuilibWnd : public CWindowWnd, public INotifyUI  
    {
    public:
    	virtual LPCTSTR GetWindowClassName() const { return _T("DuiWnd"); }
            // 响应消息 重写
    	virtual void Notify(TNotifyUI& msg) INotifyUI::Notify
    	{
            //if (msg.sType == DUI_MSGTYPE_CLICK)
    		if (msg.sType == _T("click"))	
    		{
    			if (msg.pSender->GetName() == _T("btn"))
    			{
    				::MessageBox(NULL, _T("按钮内容"), _T("按钮标题"), NULL);
    			}
    		}
    	}
    
            // 重写CWindowWnd::HandleMessage处理消息
    	virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam) 
    	{
    		LRESULT lRes = 0;
                    // 初始化窗口消息
    		if (uMsg == WM_CREATE) 				    
                    {
                            // 初始化绘制管理器,m_hWnd句柄在一个消息WM_NCCREATE中赋值
    			m_PaintManager.Init(m_hWnd);		
    			
                            // 使用XML布局文件生成界面
                            CDialogBuilder builder;				
    			CControlUI* pRoot = builder.Create(_T("duilib.xml"), 0, NULL, &m_PaintManager);
    			ASSERT(pRoot && "Failed to parse XML");
    
                            // 添加CControlUI对象,用于管理界面
    			m_PaintManager.AttachDialog(pRoot);	
    
                            // 添加INotifyUI对象,用于处理消息
    			m_PaintManager.AddNotifier(this);	
    
    			return lRes;
    		}
                    // CPaintManagerUI处理消息
    		if (m_PaintManager.MessageHandler(uMsg, wParam, lParam, lRes)) return lRes;	
    
                    // CWindowWnd兜底处理消息
    		return __super::HandleMessage(uMsg, wParam, lParam);						
    	}
    protected:
    	CPaintManagerUI m_PaintManager;				// 绘制管理器:负责绘制界面和管理消息
    };
    
    int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nShowCmd )
    {
            // 绘制管理器CPaintManagerUI绑定窗口句柄
            CPaintManagerUI::SetInstance(hInstance);
    
            // 绘制管理器CPaintManagerUI设置资源目录,用于加载XML    
            CPaintManagerUI::SetResourcePath(CPaintManagerUI::GetInstancePath()); 
    
            // 创建窗口
    	CDuilibWnd duilibWnd; 
    	duilibWnd.Create(NULL, _T("标题"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE); 
    
            // 显示窗口并且监听消息
    	duilibWnd.ShowModal();  
    
    	return 0;
    }
    

      
    7、编译之后出现RuntimeLibrary不匹配问题
    错误 LNK2038 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MDd_DynamicDebug”(main.obj 中) DuilibMinDemoStatic G:DuilibDuilibMinDemoStaticDuilibMinDemoStaticDuiLib_Static_ud.lib(UIBase.obj) 1

      
    8、修改运行库为 MTd

      
    9、这里就编译成功了

      
    10、在Debug目录下加上界面文件duilib.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <Window size="200,100">
        <HorizontalLayout bkcolor="#FFFFFFFF">
    		<Button name="btn" text="按钮" height="40" width="80" bordersize="1,1,1,1" bordercolor="#FFBCBCBC"/>
        </HorizontalLayout>
    </Window>
    


      
    11、软件运行效果

      
    Duilib技术交流群:799142530
    源码地址:https://github.com/KongKong20/DuilibTutor

  • 相关阅读:
    初识Python
    MySql的前戏
    abstract class 和 interface 有什么区别?(抽象类和接口的区别)
    java方法签名
    final
    OverLoad 和 Override 的区别
    WebService (什么是WebService ,有哪些优点? WebService由什么组成?分别对应的含义?)
    人民币
    快速排序
    动态反射
  • 原文地址:https://www.cnblogs.com/wwgk/p/14320322.html
Copyright © 2011-2022 走看看