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和PyQt5框架开发一个名为白泽题库系... 目录概述功能特点界面展示系统架构设计类结构图Excel题库填写格式模板题库题目填写格式表核心数据结构

如何自定义一个log适配器starter

《如何自定义一个log适配器starter》:本文主要介绍如何自定义一个log适配器starter的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求Starter 项目目录结构pom.XML 配置LogInitializer实现MDCInterceptor

C/C++ chrono简单使用场景示例详解

《C/C++chrono简单使用场景示例详解》:本文主要介绍C/C++chrono简单使用场景示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录chrono使用场景举例1 输出格式化字符串chrono使用场景China编程举例1 输出格式化字符串示

Druid连接池实现自定义数据库密码加解密功能

《Druid连接池实现自定义数据库密码加解密功能》在现代应用开发中,数据安全是至关重要的,本文将介绍如何在​​Druid​​连接池中实现自定义的数据库密码加解密功能,有需要的小伙伴可以参考一下... 目录1. 环境准备2. 密码加密算法的选择3. 自定义 ​​DruidDataSource​​ 的密码解密3

spring-gateway filters添加自定义过滤器实现流程分析(可插拔)

《spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔)》:本文主要介绍spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔),本文通过实例图... 目录需求背景需求拆解设计流程及作用域逻辑处理代码逻辑需求背景公司要求,通过公司网络代理访问的请求需要做请

Python中合并列表(list)的六种方法小结

《Python中合并列表(list)的六种方法小结》本文主要介绍了Python中合并列表(list)的六种方法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋... 目录一、直接用 + 合并列表二、用 extend() js方法三、用 zip() 函数交叉合并四、用

Spring Boot中的YML配置列表及应用小结

《SpringBoot中的YML配置列表及应用小结》在SpringBoot中使用YAML进行列表的配置不仅简洁明了,还能提高代码的可读性和可维护性,:本文主要介绍SpringBoot中的YML配... 目录YAML列表的基础语法在Spring Boot中的应用从YAML读取列表列表中的复杂对象其他注意事项总

windows和Linux安装Jmeter与简单使用方式

《windows和Linux安装Jmeter与简单使用方式》:本文主要介绍windows和Linux安装Jmeter与简单使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录Windows和linux安装Jmeter与简单使用一、下载安装包二、JDK安装1.windows设

C++类和对象之初始化列表的使用方式

《C++类和对象之初始化列表的使用方式》:本文主要介绍C++类和对象之初始化列表的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C++初始化列表详解:性能优化与正确实践什么是初始化列表?初始化列表的三大核心作用1. 性能优化:避免不必要的赋值操作2. 强

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依