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

相关文章

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配置邮件通知

Python自定义异常的全面指南(入门到实践)

《Python自定义异常的全面指南(入门到实践)》想象你正在开发一个银行系统,用户转账时余额不足,如果直接抛出ValueError,调用方很难区分是金额格式错误还是余额不足,这正是Python自定义异... 目录引言:为什么需要自定义异常一、异常基础:先搞懂python的异常体系1.1 异常是什么?1.2

Linux中的自定义协议+序列反序列化用法

《Linux中的自定义协议+序列反序列化用法》文章探讨网络程序在应用层的实现,涉及TCP协议的数据传输机制、结构化数据的序列化与反序列化方法,以及通过JSON和自定义协议构建网络计算器的思路,强调分层... 目录一,再次理解协议二,序列化和反序列化三,实现网络计算器3.1 日志文件3.2Socket.hpp

C语言自定义类型之联合和枚举解读

《C语言自定义类型之联合和枚举解读》联合体共享内存,大小由最大成员决定,遵循对齐规则;枚举类型列举可能值,提升可读性和类型安全性,两者在C语言中用于优化内存和程序效率... 目录一、联合体1.1 联合体类型的声明1.2 联合体的特点1.2.1 特点11.2.2 特点21.2.3 特点31.3 联合体的大小1

把Python列表中的元素移动到开头的三种方法

《把Python列表中的元素移动到开头的三种方法》在Python编程中,我们经常需要对列表(list)进行操作,有时,我们希望将列表中的某个元素移动到最前面,使其成为第一项,本文给大家介绍了把Pyth... 目录一、查找删除插入法1. 找到元素的索引2. 移除元素3. 插入到列表开头二、使用列表切片(Lis