当前位置:   article > 正文

duilib最简单的自定义列表_duilib例子

duilib例子

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

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

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <Window size="500,500" caption="0,0,0,30">
  3. <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;" />
  4. <VerticalLayout bkcolor="#FFFFFFE0">
  5. <VerticalLayout height="50">
  6. <HorizontalLayout>
  7. <HorizontalLayout>
  8. </HorizontalLayout>
  9. <Label align="center" text="列表测试Demo"/>
  10. <HorizontalLayout></HorizontalLayout>
  11. <Button name="btnstart" text="开始" bkcolor="#FFF5DEB3" padding="8,8,8,8" borderround="5,5"/>
  12. <HorizontalLayout>
  13. </HorizontalLayout>
  14. </HorizontalLayout>
  15. </VerticalLayout>
  16. <VerticalLayout >
  17. <!--list开始-->
  18. <MyList2 name="list1" header="hidden" itemshowhtml="true" vscrollbar="true">
  19. </MyList2>
  20. </VerticalLayout>
  21. </VerticalLayout>
  22. </Window>

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

 

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Window>
  3. <ListContainerElement height="32">
  4. <VerticalLayout height="32">
  5. <HorizontalLayout>
  6. <VerticalLayout name="logo_container" width="50">
  7. <Button name="logo" width="32" height="32" />
  8. </VerticalLayout>
  9. <VerticalLayout >
  10. <HorizontalLayout>
  11. <Label name="nickname" text="default" bordersize="0" width="120" textcolor="#FF000000" disabledtextcolor="#FF808080" />
  12. <Label name="description" bordersize="0" textcolor="#FF808080" />
  13. </HorizontalLayout>
  14. </VerticalLayout>
  15. </HorizontalLayout>
  16. </VerticalLayout>
  17. </ListContainerElement>
  18. </Window>

MyList2类的代码如下:

UIMyList2.h

  1. #ifndef UIMYLIST2_H
  2. #define UIMYLIST2_H
  3. #include "../../../duilib/DuiLib/UIlib.h"
  4. namespace DuiLib
  5. {
  6. struct MyItem2
  7. {
  8. //头像
  9. CDuiString strPic;
  10. //名称
  11. CDuiString strNiceName;
  12. //描述
  13. CDuiString strDes;
  14. };
  15. class CMyListUI2 :public CListUI
  16. {
  17. public:
  18. enum { SCROLL_TIMERID = 10 };
  19. CMyListUI2(CPaintManagerUI& paint_manager);
  20. ~CMyListUI2();
  21. bool Add(CControlUI* pControl);
  22. bool AddAt(CControlUI* pControl, int iIndex);
  23. bool Remove(CControlUI* pControl, bool bDoNotDestroy = false);
  24. bool RemoveAt(int iIndex, bool bDoNotDestroy);
  25. void RemoveAll();
  26. //定制方法
  27. void AddItem(MyItem2& itemdata);
  28. private:
  29. CPaintManagerUI& paint_manager_;
  30. CDialogBuilder m_dlgBuilder;
  31. };
  32. }
  33. #endif // UIMYLIST2_H

UIMyList2.cpp

  1. #include "UIMyList2.h"
  2. namespace DuiLib{
  3. CMyListUI2::CMyListUI2(CPaintManagerUI& paint_manager)
  4. :paint_manager_(paint_manager)
  5. {
  6. SetItemShowHtml(true);
  7. }
  8. CMyListUI2::~CMyListUI2()
  9. {
  10. }
  11. bool CMyListUI2::Add(CControlUI* pControl)
  12. {
  13. if (!pControl)
  14. return false;
  15. if (_tcsicmp(pControl->GetClass(), DUI_CTR_LISTCONTAINERELEMENT) != 0)
  16. return false;
  17. return CListUI::Add(pControl);
  18. }
  19. bool CMyListUI2::AddAt(CControlUI* pControl, int iIndex)
  20. {
  21. if (!pControl)
  22. return false;
  23. if (_tcsicmp(pControl->GetClass(), DUI_CTR_LISTCONTAINERELEMENT) != 0)
  24. return false;
  25. return CListUI::AddAt(pControl, iIndex);
  26. }
  27. bool CMyListUI2::Remove(CControlUI* pControl, bool bDoNotDestroy)
  28. {
  29. if (!pControl)
  30. return false;
  31. if (_tcsicmp(pControl->GetClass(), DUI_CTR_LISTCONTAINERELEMENT) != 0)
  32. return false;
  33. return CListUI::Remove(pControl, bDoNotDestroy);
  34. }
  35. bool CMyListUI2::RemoveAt(int iIndex, bool bDoNotDestroy)
  36. {
  37. CControlUI* pControl = GetItemAt(iIndex);
  38. if (!pControl)
  39. return false;
  40. if (_tcsicmp(pControl->GetClass(), DUI_CTR_LISTCONTAINERELEMENT) != 0)
  41. return false;
  42. return CListUI::RemoveAt(iIndex, bDoNotDestroy);
  43. }
  44. void CMyListUI2::RemoveAll()
  45. {
  46. CListUI::RemoveAll();
  47. }
  48. void CMyListUI2::AddItem(MyItem2& itemdata)
  49. {
  50. CListContainerElementUI* pItem = NULL;
  51. if (!m_dlgBuilder.GetMarkup()->IsValid())
  52. {
  53. pItem = static_cast<CListContainerElementUI*>(m_dlgBuilder.Create(_T("friend_list_item.xml"), (UINT)0, NULL, &paint_manager_));
  54. }
  55. else {
  56. pItem = static_cast<CListContainerElementUI*>(m_dlgBuilder.Create((UINT)0, &paint_manager_));
  57. }
  58. if (!pItem) return;
  59. this->Add(pItem);
  60. //设置头像
  61. CButtonUI *pBtn = static_cast<CButtonUI*>(pItem->FindSubControl(_T("logo")));
  62. if (pBtn)
  63. {
  64. pBtn->SetNormalImage(itemdata.strPic);
  65. }
  66. //设置昵称
  67. CLabelUI * pNickName = static_cast<CLabelUI*>(pItem->FindSubControl(_T("nickname")));
  68. if (pNickName)
  69. {
  70. pNickName->SetText(itemdata.strNiceName);
  71. }
  72. //设置描述
  73. CLabelUI * pDes = static_cast<CLabelUI*>(pItem->FindSubControl(_T("description")));
  74. if (pDes)
  75. {
  76. pDes->SetText(itemdata.strDes);
  77. }
  78. }
  79. }

 

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

  1. #pragma once
  2. #include "../../../duilib/DuiLib/UIlib.h"
  3. //#include <Windows.h>
  4. #pragma comment(lib,"./bin/duilib_d.lib")
  5. #include "UIMyList2.h"
  6. using namespace DuiLib;
  7. class CDuiFrameWnd : public WindowImplBase
  8. {
  9. public:
  10. virtual LPCTSTR GetWindowClassName() const { return _T("listDemo1"); }
  11. virtual CDuiString GetSkinFile() { return _T("listdemo.xml"); }
  12. virtual CDuiString GetSkinFolder() { return _T(""); }
  13. //virtual LRESULT HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  14. //{
  15. // return WindowImplBase::HandleMessage(uMsg, wParam, lParam);
  16. //}
  17. virtual void Notify(TNotifyUI& msg)
  18. {
  19. if (msg.sType == DUI_MSGTYPE_CLICK && msg.pSender->GetName() == _T("btnstart"))
  20. {
  21. //点按钮往列表插入数据
  22. CMyListUI2* pMyList2 = static_cast<CMyListUI2*>(m_PaintManager.FindControl(_T("list1")));
  23. if (pMyList2)
  24. {
  25. MyItem2 itemdata1;
  26. itemdata1.strPic = _T("default.png");
  27. itemdata1.strNiceName = _T("小明");
  28. itemdata1.strDes = _T("我就是我,是颜色不一样的焰火1");
  29. pMyList2->AddItem(itemdata1);
  30. MyItem2 itemdata2;
  31. itemdata2.strPic = _T("default.png");
  32. itemdata2.strNiceName = _T("小王");
  33. itemdata2.strDes = _T("我就是我,是颜色不一样的焰火2");
  34. pMyList2->AddItem(itemdata2);
  35. MyItem2 itemdata3;
  36. itemdata3.strPic = _T("default.png");
  37. itemdata3.strNiceName = _T("小张");
  38. itemdata3.strDes = _T("我就是我,是颜色不一样的焰火3");
  39. pMyList2->AddItem(itemdata3);
  40. }
  41. return;
  42. }
  43. }
  44. virtual CControlUI* CreateControl(LPCTSTR pstrClass)
  45. {
  46. if (_tcsicmp(pstrClass, _T("MyList2")) == 0)
  47. {
  48. return new CMyListUI2(m_PaintManager);
  49. }
  50. else
  51. return NULL;
  52. }
  53. };
  54. int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
  55. {
  56. CPaintManagerUI::SetInstance(hInstance);
  57. CDuiFrameWnd duiFrame;
  58. duiFrame.Create(NULL, _T("DUIWnd"), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE);
  59. duiFrame.CenterWindow();
  60. duiFrame.ShowModal();
  61. return 0;
  62. }

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

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

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

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

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

不懂请留言

程序运行结果如下:

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

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/942898
推荐阅读
相关标签