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开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

Mysql表的简单操作(基本技能)

《Mysql表的简单操作(基本技能)》在数据库中,表的操作主要包括表的创建、查看、修改、删除等,了解如何操作这些表是数据库管理和开发的基本技能,本文给大家介绍Mysql表的简单操作,感兴趣的朋友一起看... 目录3.1 创建表 3.2 查看表结构3.3 修改表3.4 实践案例:修改表在数据库中,表的操作主要

springboot简单集成Security配置的教程

《springboot简单集成Security配置的教程》:本文主要介绍springboot简单集成Security配置的教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录集成Security安全框架引入依赖编写配置类WebSecurityConfig(自定义资源权限规则

如何自定义Nginx JSON日志格式配置

《如何自定义NginxJSON日志格式配置》Nginx作为最流行的Web服务器之一,其灵活的日志配置能力允许我们根据需求定制日志格式,本文将详细介绍如何配置Nginx以JSON格式记录访问日志,这种... 目录前言为什么选择jsON格式日志?配置步骤详解1. 安装Nginx服务2. 自定义JSON日志格式各

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

如何使用Python实现一个简单的window任务管理器

《如何使用Python实现一个简单的window任务管理器》这篇文章主要为大家详细介绍了如何使用Python实现一个简单的window任务管理器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起... 任务管理器效果图完整代码import tkinter as tkfrom tkinter i

C++中函数模板与类模板的简单使用及区别介绍

《C++中函数模板与类模板的简单使用及区别介绍》这篇文章介绍了C++中的模板机制,包括函数模板和类模板的概念、语法和实际应用,函数模板通过类型参数实现泛型操作,而类模板允许创建可处理多种数据类型的类,... 目录一、函数模板定义语法真实示例二、类模板三、关键区别四、注意事项 ‌在C++中,模板是实现泛型编程

Python中DataFrame转列表的最全指南

《Python中DataFrame转列表的最全指南》在Python数据分析中,Pandas的DataFrame是最常用的数据结构之一,本文将为你详解5种主流DataFrame转换为列表的方法,大家可以... 目录引言一、基础转换方法解析1. tolist()直接转换法2. values.tolist()矩阵

基于Spring实现自定义错误信息返回详解

《基于Spring实现自定义错误信息返回详解》这篇文章主要为大家详细介绍了如何基于Spring实现自定义错误信息返回效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录背景目标实现产出背景Spring 提供了 @RestConChina编程trollerAdvice 用来实现 HTT