duilib列表list使用方法

xml文件如下:



	
	
		
			
				
				
				

C++代码如下:

#pragma once
#include "../../../duilib/DuiLib/UIlib.h"
using namespace DuiLib;
//#pragma comment

#ifdef _DEBUG
#   ifdef _UNICODE
#       pragma comment(lib, "DuiLib_ud.lib")
#   else
#       pragma comment(lib, "../../../duilib/lib/DuiLib_d.lib")
#   endif
#else
#   ifdef _UNICODE
#       pragma comment(lib, "DuiLib_u.lib")
#   else
#       pragma comment(lib, "DuiLib.lib")
#   endif
#endif

using namespace DuiLib;

class CDuiFrameWnd : public WindowImplBase
{
public:
    virtual LPCTSTR    GetWindowClassName() const { return _T("DUIMainFrame"); }
    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"))
        {
            //往列表插入数据
            DuiLib::CListUI* pList = static_cast(m_PaintManager.FindControl(_T("list1")));

            if (!pList) return;
            for (int i = 0; i < 100; ++i)
            {
                CListTextElementUI *pLine = new CListTextElementUI;
                pLine->SetTag(i);  //相当于设置行号
                pList->Add(pLine);

                char buff[10] = { 0 };
                sprintf_s(buff, 10, "%d", i);
                pLine->SetText(0, buff);
                pLine->SetText(1, "2222");
                pLine->SetText(2, "3333");


            }
        }
        else if (msg.sType == DUI_MSGTYPE_ITEMSELECT && msg.pSender->GetName()==_T("list1"))
        {
            //获取选中行
            DuiLib::CListUI* pList = static_cast(m_PaintManager.FindControl(_T("list1")));
            int nIndex = pList->GetCurSel();   //获取选中行的索引

            //获取行数据
            CListTextElementUI *pLine = static_cast(pList->GetItemAt(nIndex));
            if (pLine)
            {
                //分别获取1,2,3列的数据
                MessageBox(NULL, pLine->GetText(0), "1列", MB_OK);
                MessageBox(NULL, pLine->GetText(1), "2列", MB_OK);
                MessageBox(NULL, pLine->GetText(2), "3列", MB_OK);
            }
        }
    }
};

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;
}

效果图如下:

duilib列表list使用方法_第1张图片

点击开始,动态插入100条数据。

我的这个版本的duilib,滚动条显示不出来,所以设置了一些样式,图片下载地址:

https://download.csdn.net/download/Think88666/11987270

将图片拷贝到xml的目录即可。

你可能感兴趣的:(duilib)