duilib最简单的自定义列表

2024-03-30 19:48
文章标签 简单 自定义 列表 duilib

本文主要是介绍duilib最简单的自定义列表,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

网上写的关于自定义列表的例子都过于复杂,对初学者不太友好。这里举了一个最简单的例子。

主界面的listdemo.xml内容如下:

<?xml version="1.0" encoding="UTF-8" ?>
<Window size="500,500" caption="0,0,0,30"><Default name="VScrollBar" value="button1normalimage=&quot;file='scrollbar.png' source='0,0,16,16'&quot; button1hotimage=&quot;file='scrollbar.png' source='16,0,32,16,16'&quot; button1pushedimage=&quot;file='scrollbar.png' source='32,0,48,16'&quot; button2normalimage=&quot;file='scrollbar.png' source='0,32,16,48'&quot; button2hotimage=&quot;file='scrollbar.png' source='16,32,32,48'&quot; button2pushedimage=&quot;file='scrollbar.png' source='32,32,48,48'&quot; thumbnormalimage=&quot;file='scrollbar.png' source='0,48,16,64' corner='0,2,0,2'&quot; thumbhotimage=&quot;file='scrollbar.png' source='16,48,32,64' corner='0,2,0,2'&quot; thumbpushedimage=&quot;file='scrollbar.png' source='32,48,48,64' corner='0,2,0,2'&quot; bknormalimage=&quot;file='scrollbar.png' source='0,16,16,32'&quot;" /><VerticalLayout bkcolor="#FFFFFFE0"><VerticalLayout height="50"><HorizontalLayout><HorizontalLayout></HorizontalLayout><Label align="center" text="列表测试Demo"/><HorizontalLayout></HorizontalLayout><Button name="btnstart" text="开始" bkcolor="#FFF5DEB3" padding="8,8,8,8" borderround="5,5"/><HorizontalLayout></HorizontalLayout>	</HorizontalLayout></VerticalLayout><VerticalLayout > <!--list开始--><MyList2 name="list1" header="hidden" itemshowhtml="true" vscrollbar="true"></MyList2></VerticalLayout></VerticalLayout>
</Window>

其中MyList2是自定义的列表标签。

 

列表项的friend_list_item.xml文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<Window><ListContainerElement height="32"><VerticalLayout height="32"><HorizontalLayout><VerticalLayout name="logo_container" width="50"><Button name="logo" width="32" height="32" /></VerticalLayout><VerticalLayout ><HorizontalLayout><Label name="nickname" text="default" bordersize="0" width="120" textcolor="#FF000000" disabledtextcolor="#FF808080" /><Label name="description" bordersize="0" textcolor="#FF808080" /></HorizontalLayout></VerticalLayout></HorizontalLayout></VerticalLayout></ListContainerElement>
</Window>

MyList2类的代码如下:

UIMyList2.h

#ifndef UIMYLIST2_H
#define UIMYLIST2_H
#include "../../../duilib/DuiLib/UIlib.h"namespace DuiLib
{struct MyItem2{//头像CDuiString strPic;//名称CDuiString strNiceName;//描述CDuiString strDes;};class CMyListUI2 :public CListUI{public:enum { SCROLL_TIMERID = 10 };CMyListUI2(CPaintManagerUI& paint_manager);~CMyListUI2();bool Add(CControlUI* pControl);bool AddAt(CControlUI* pControl, int iIndex);bool Remove(CControlUI* pControl, bool bDoNotDestroy = false);bool RemoveAt(int iIndex, bool bDoNotDestroy);void RemoveAll();//定制方法void AddItem(MyItem2& itemdata);private:CPaintManagerUI& paint_manager_;CDialogBuilder m_dlgBuilder;};
}#endif // UIMYLIST2_H

UIMyList2.cpp


#include "UIMyList2.h"namespace DuiLib{CMyListUI2::CMyListUI2(CPaintManagerUI& paint_manager):paint_manager_(paint_manager){SetItemShowHtml(true);}CMyListUI2::~CMyListUI2(){}bool CMyListUI2::Add(CControlUI* pControl){if (!pControl)return false;if (_tcsicmp(pControl->GetClass(), DUI_CTR_LISTCONTAINERELEMENT) != 0)return false;return CListUI::Add(pControl);}bool CMyListUI2::AddAt(CControlUI* pControl, int iIndex){if (!pControl)return false;if (_tcsicmp(pControl->GetClass(), DUI_CTR_LISTCONTAINERELEMENT) != 0)return false;return CListUI::AddAt(pControl, iIndex);}bool CMyListUI2::Remove(CControlUI* pControl, bool bDoNotDestroy){if (!pControl)return false;if (_tcsicmp(pControl->GetClass(), DUI_CTR_LISTCONTAINERELEMENT) != 0)return false;return CListUI::Remove(pControl, bDoNotDestroy);}bool CMyListUI2::RemoveAt(int iIndex, bool bDoNotDestroy){CControlUI* pControl = GetItemAt(iIndex);if (!pControl)return false;if (_tcsicmp(pControl->GetClass(), DUI_CTR_LISTCONTAINERELEMENT) != 0)return false;return CListUI::RemoveAt(iIndex, bDoNotDestroy);}void CMyListUI2::RemoveAll(){CListUI::RemoveAll();}void CMyListUI2::AddItem(MyItem2& itemdata){CListContainerElementUI* pItem = NULL;if (!m_dlgBuilder.GetMarkup()->IsValid()){pItem = static_cast<CListContainerElementUI*>(m_dlgBuilder.Create(_T("friend_list_item.xml"), (UINT)0, NULL, &paint_manager_));}else {pItem = static_cast<CListContainerElementUI*>(m_dlgBuilder.Create((UINT)0, &paint_manager_));}if (!pItem) return;this->Add(pItem);//设置头像CButtonUI *pBtn = static_cast<CButtonUI*>(pItem->FindSubControl(_T("logo")));if (pBtn){pBtn->SetNormalImage(itemdata.strPic);}//设置昵称CLabelUI * pNickName = static_cast<CLabelUI*>(pItem->FindSubControl(_T("nickname")));if (pNickName){pNickName->SetText(itemdata.strNiceName);}//设置描述CLabelUI * pDes = static_cast<CLabelUI*>(pItem->FindSubControl(_T("description")));if (pDes){pDes->SetText(itemdata.strDes);}}
}

 

main函数主框架的的代码如下:

#pragma once#include "../../../duilib/DuiLib/UIlib.h"
//#include <Windows.h>#pragma comment(lib,"./bin/duilib_d.lib")#include "UIMyList2.h"using namespace DuiLib;class CDuiFrameWnd : public WindowImplBase
{
public:virtual LPCTSTR    GetWindowClassName() const { return _T("listDemo1"); }virtual CDuiString GetSkinFile() { return _T("listdemo.xml"); }virtual CDuiString GetSkinFolder() { return _T(""); }//virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)//{//	return WindowImplBase::HandleMessage(uMsg, wParam, lParam);//}virtual void Notify(TNotifyUI& msg){if (msg.sType == DUI_MSGTYPE_CLICK && msg.pSender->GetName() == _T("btnstart")){//点按钮往列表插入数据CMyListUI2* pMyList2 = static_cast<CMyListUI2*>(m_PaintManager.FindControl(_T("list1")));if (pMyList2){MyItem2 itemdata1;itemdata1.strPic = _T("default.png");itemdata1.strNiceName = _T("小明");itemdata1.strDes = _T("我就是我,是颜色不一样的焰火1");pMyList2->AddItem(itemdata1);MyItem2 itemdata2;itemdata2.strPic = _T("default.png");itemdata2.strNiceName = _T("小王");itemdata2.strDes = _T("我就是我,是颜色不一样的焰火2");pMyList2->AddItem(itemdata2);MyItem2 itemdata3;itemdata3.strPic = _T("default.png");itemdata3.strNiceName = _T("小张");itemdata3.strDes = _T("我就是我,是颜色不一样的焰火3");pMyList2->AddItem(itemdata3);}return;}}virtual CControlUI* CreateControl(LPCTSTR pstrClass){if (_tcsicmp(pstrClass, _T("MyList2")) == 0){return new CMyListUI2(m_PaintManager);}elsereturn NULL;}
};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.CenterWindow();duiFrame.ShowModal();return 0;
}

CMyList2的代码已经非常简单了,重写基类的方法实现也很简单。基本上一看就懂。其实本质就是往list中添加子项为CListContainerElementUI控件,该控件包含所有其他控件组成了列表的一项。

个人认为最重要的几个方法是,

CDuiFrameWnd 的CreateControl方法(duilib识别到未知标签后会调用该方法,让程序员返回自定义标签类的实例)以及

CListContainerElementUI的FindSubControl(通过name属性查找子控件)、

CDialogBuilder 的Create方法使用(通过xml文件创建控件)

不懂请留言

程序运行结果如下:

点击开始按钮向列表添加数据

 

 

 

 

这篇关于duilib最简单的自定义列表的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/862411

相关文章

Python 常用数据类型详解之字符串、列表、字典操作方法

《Python常用数据类型详解之字符串、列表、字典操作方法》在Python中,字符串、列表和字典是最常用的数据类型,它们在数据处理、程序设计和算法实现中扮演着重要角色,接下来通过本文给大家介绍这三种... 目录一、字符串(String)(一)创建字符串(二)字符串操作1. 字符串连接2. 字符串重复3. 字

C#中通过Response.Headers设置自定义参数的代码示例

《C#中通过Response.Headers设置自定义参数的代码示例》:本文主要介绍C#中通过Response.Headers设置自定义响应头的方法,涵盖基础添加、安全校验、生产实践及调试技巧,强... 目录一、基础设置方法1. 直接添加自定义头2. 批量设置模式二、高级配置技巧1. 安全校验机制2. 类型

Python实现简单封装网络请求的示例详解

《Python实现简单封装网络请求的示例详解》这篇文章主要为大家详细介绍了Python实现简单封装网络请求的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录安装依赖核心功能说明1. 类与方法概览2.NetHelper类初始化参数3.ApiResponse类属性与方法使用实

SpringBoot AspectJ切面配合自定义注解实现权限校验的示例详解

《SpringBootAspectJ切面配合自定义注解实现权限校验的示例详解》本文章介绍了如何通过创建自定义的权限校验注解,配合AspectJ切面拦截注解实现权限校验,本文结合实例代码给大家介绍的非... 目录1. 创建权限校验注解2. 创建ASPectJ切面拦截注解校验权限3. 用法示例A. 参考文章本文

Vite 打包目录结构自定义配置小结

《Vite打包目录结构自定义配置小结》在Vite工程开发中,默认打包后的dist目录资源常集中在asset目录下,不利于资源管理,本文基于Rollup配置原理,本文就来介绍一下通过Vite配置自定义... 目录一、实现原理二、具体配置步骤1. 基础配置文件2. 配置说明(1)js 资源分离(2)非 JS 资

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

Python进阶之列表推导式的10个核心技巧

《Python进阶之列表推导式的10个核心技巧》在Python编程中,列表推导式(ListComprehension)是提升代码效率的瑞士军刀,本文将通过真实场景案例,揭示列表推导式的进阶用法,希望对... 目录一、基础语法重构:理解推导式的底层逻辑二、嵌套循环:破解多维数据处理难题三、条件表达式:实现分支

聊聊springboot中如何自定义消息转换器

《聊聊springboot中如何自定义消息转换器》SpringBoot通过HttpMessageConverter处理HTTP数据转换,支持多种媒体类型,接下来通过本文给大家介绍springboot中... 目录核心接口springboot默认提供的转换器如何自定义消息转换器Spring Boot 中的消息

python连接sqlite3简单用法完整例子

《python连接sqlite3简单用法完整例子》SQLite3是一个内置的Python模块,可以通过Python的标准库轻松地使用,无需进行额外安装和配置,:本文主要介绍python连接sqli... 目录1. 连接到数据库2. 创建游标对象3. 创建表4. 插入数据5. 查询数据6. 更新数据7. 删除

Jenkins的安装与简单配置过程

《Jenkins的安装与简单配置过程》本文简述Jenkins在CentOS7.3上安装流程,包括Java环境配置、RPM包安装、修改JENKINS_HOME路径及权限、启动服务、插件安装与系统管理设置... 目录www.chinasem.cnJenkins安装访问并配置JenkinsJenkins配置邮件通知