UiListEx列表控件示例(22_UiListEx)

UiListEx列表控件

此示例展示以下控件的使用方法:

列表扩展控件UiListEx的使用流程

示例代码展示:

22_UiListEx_1.png
22_UiListEx_2.png
22_UiListEx_3.png

初始化 UiListEx 控件

m_list.SetID(IDR_UILISTEX);
m_list.SetPos(0,0,GetWidth(),GetHeight()-MZM_HEIGHT_TEXT_TOOLBAR);
m_list.EnableDragModeH(true);
m_list.EnableInsideScroll(true);
m_list.EnableUltraGridlines(true);
m_list.SetItemHeight(100);//注意要在SetIteemAttribute前设置ItemHeigth
m_list.SetItemAttribute(UILISTEX_ITEMTYPE_SMS);  
m_list.EnableNotifyMessage(true);
m_list.UpdateItemAttribute_Del();

添加列表项

CMzString name = L"姓名%d";
CMzString content = L"短信内容 SmsContent%d:";
CMzString stime = L"9/8 12:20";

CMzString name1(10);
CMzString content1(30);
for (int i = 0; i < 10000; i++)
{
    swprintf(name1.C_Str(),name.C_Str(),i);
    swprintf(content1.C_Str(),content.C_Str(),i);

    ListItemEx* item = new ListItemEx;
    item->m_pData = (void*)i;
    item->m_textTitle = name1.C_Str();

    item->m_textPostscript1 = stime.C_Str();

    if (i == 2)
    {
        item->m_textDescription = L"content: I'm dying to see u! what? I couldn't get it";
    }
    else if(i == 6)
    {
        item->m_textDescription = L"我这里天快要黑了,那里呢?我这里天气凉凉的那里呢? 我这里一切都变了,而那你呢?";
    }
    else
    {
        item->m_textDescription = content1.C_Str();
    }

    //添加头像
    CMzString path = L"..\\Disk\\Photo\\list\\%d.jpg";
    if(i<=5)
    {
        if (i <= 3)
        {
            item->m_enableDragDelete = false; //前四项不可删除
        }

        ImagingHelper* pImg = new ImagingHelper;
        CMzString sPath(50);
        swprintf(sPath.C_Str(),path.C_Str(),i);
        BOOL bRet = pImg->LoadImage(sPath.C_Str(),true,true);
        if (bRet)
        {
            item->m_pImgFirst = pImg;
        }
    }
    else
    {
        item->m_pImgFirst = m_imgContainer.LoadImage(MzGetInstanceHandle(), IDR_PNG_SMS_LIST_READ, true);
    }

    item->m_pImgSecond = m_imgContainer.LoadImage(MzGetInstanceHandle(), IDR_PNG_ARROW_R, true);
    m_list.AddItem(item);
}
//! ]]添加项

重写 CMzWnd::MzDefWndProc(UINT message, WPARAM wParam, LPARAM lParam), 删除 UiListEx 的列表项

virtual LRESULT MzDefWndProc(UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
        //处理删除某项时的消息
    case MZ_WM_ITEM_ONREMOVE:
        {
            int index = wParam;
            ListItemEx* pItem = m_list.GetItem(index);
            if (pItem)
            {
                int customData = (int)pItem->m_pData;
                if (customData < 6)
                {
                    delete pItem->m_pImgFirst;
                }
            }
        }
        break;
    default:
        break;
    }
    return CMzWndEx::MzDefWndProc(message,wParam,lParam);
}

完整示例:

/************************************************************************/
/*
* Copyright (C) Meizu Technology Corporation Zhuhai China
* All rights reserved.
* 中国珠海, 魅族科技有限公司, 版权所有.
*
* This file is a part of the Meizu Foundation Classes library.
* Author:    Lynn
* Create on: 2009-4-25
*/
/************************************************************************/

//请按照以步骤运行此实例代码:
//首先, 打开VS2005/2008创建一个Win 32智能设备项目
//在项目向导中选择M8SDK, 并勾选空项目
//然后,在项目中新建一个cpp文件,将此处代码拷贝到cpp文件中
//最后,按照M8SDK的帮助文档,配置项目属性
//现在,可以运行此程序了
#include <mzfc_inc.h>

//此代码演示了:
//创建和初始化应用程序
//创建和初始化窗体
//使用UiListEx控件

#include <mzfc/UiListEx.h>
#include "resource.h"
#define     IDR_TOOLBAR     101
#define     IDR_UILISTEX    102
#include <mzfc/UiCustomList.h>
class ListSample:public CMzWndEx
{
private:
    UiListEx m_list;
    UiToolbar_Text m_toolBar;

    ImageContainer m_imgContainer;

protected:
    virtual BOOL OnInitDialog()
    {
        if (!CMzWndEx::OnInitDialog())
        {
            return FALSE;
        }

        m_toolBar.SetID(IDR_TOOLBAR);
        m_toolBar.SetPos(0,GetHeight()-MZM_HEIGHT_TEXT_TOOLBAR,GetWidth(),MZM_HEIGHT_TEXT_TOOLBAR);
        m_toolBar.SetButton(0,true,true,L"选择");
        m_toolBar.SetButton(2,true,true,L"***");
        AddUiWin(&m_toolBar);

        m_list.SetID(IDR_UILISTEX);
        m_list.SetPos(0,0,GetWidth(),GetHeight()-MZM_HEIGHT_TEXT_TOOLBAR);
        m_list.EnableDragModeH(true);
        m_list.EnableInsideScroll(true);
        m_list.EnableUltraGridlines(true);
        m_list.SetItemHeight(100);//注意要在SetIteemAttribute前设置ItemHeigth
        m_list.SetItemAttribute(UILISTEX_ITEMTYPE_SMS);  
        m_list.EnableNotifyMessage(true);
        m_list.UpdateItemAttribute_Del();
        AddUiWin(&m_list);


        CMzString name = L"姓名%d";
        CMzString content = L"短信内容 SmsContent%d:";
        CMzString stime = L"9/8 12:20";

        CMzString name1(10);
        CMzString content1(30);
        for (int i = 0; i < 10000; i++)
        {
            swprintf(name1.C_Str(),name.C_Str(),i);
            swprintf(content1.C_Str(),content.C_Str(),i);

            ListItemEx* item = new ListItemEx;
            item->m_pData = (void*)i;
            item->m_textTitle = name1.C_Str();

            item->m_textPostscript1 = stime.C_Str();

            if (i == 2)
            {
                item->m_textDescription = L"content: I'm dying to see u! what? I couldn't get it";
            }
            else if(i == 6)
            {
                item->m_textDescription = L"我这里天快要黑了,那里呢?我这里天气凉凉的那里呢? 我这里一切都变了,而那你呢?";
            }
            else
            {
                item->m_textDescription = content1.C_Str();
            }

            //添加头像
            CMzString path = L"..\\Disk\\Photo\\list\\%d.jpg";
            if(i<=5)
            {
                if (i <= 3)
                {
                    item->m_enableDragDelete = false; //前四项不可删除
                }

                ImagingHelper* pImg = new ImagingHelper;
                CMzString sPath(50);
                swprintf(sPath.C_Str(),path.C_Str(),i);
                BOOL bRet = pImg->LoadImage(sPath.C_Str(),true,true);
                if (bRet)
                {
                    item->m_pImgFirst = pImg;
                }
            }
            else
            {
                item->m_pImgFirst = m_imgContainer.LoadImage(MzGetInstanceHandle(), IDR_PNG_SMS_LIST_READ, true);
            }

            item->m_pImgSecond = m_imgContainer.LoadImage(MzGetInstanceHandle(), IDR_PNG_ARROW_R, true);

            m_list.AddItem(item);
        }

        return TRUE;
    }

    virtual void OnMzCommand(WPARAM wParam, LPARAM lParam)
    {
        UINT_PTR id = LOWORD(wParam);
        switch(id)
        {
        case IDR_TOOLBAR:
            {
                int index = lParam;
                if (index == 0)
                {
                    if (wcscmp(m_toolBar.GetButtonText(0).C_Str(),L"选择") == 0 )
                    {
                        m_list.SetMultiSelectMode(UILISTEX_MULTISELECT_RIGHT);

                        m_toolBar.SetButtonText(0,L"返回");
                        m_toolBar.SetButtonText(2,L"删除");
                        Invalidate();
                        UpdateWindow();
                    }
                    else if (wcscmp(m_toolBar.GetButtonText(0).C_Str(),L"返回") == 0)
                    {
                        m_list.SetMultiSelectMode();
                        m_list.SetSelectedIndex(-1);
                        m_toolBar.SetButtonText(0,L"选择");
                        m_toolBar.SetButtonText(2,L"***");
                        Invalidate();
                        UpdateWindow();
                    }   
                    
                }
                else if (index == 2)
                {
                    if (wcscmp(m_toolBar.GetButtonText(2).C_Str(),L"删除") == 0 )
                    {
                        for (int i = 0; i < m_list.GetItemCount();)
                        {
                            ListItemEx* pItem = m_list.GetItem(i);
                            if (pItem && pItem->m_isSelected)
                            {
                                m_list.RemoveItem(i);
                            }
                            else
                            {
                                i++;
                            }
                        }
                        m_list.Invalidate();
                        m_list.Update();
                    } 
                }
            }
            break;

        }
    }

    virtual LRESULT MzDefWndProc(UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch(message)
        {
            //处理删除某项时的消息
        case MZ_WM_ITEM_ONREMOVE:
            {
                int index = wParam;
                ListItemEx* pItem = m_list.GetItem(index);
                if (pItem)
                {
                    int customData = (int)pItem->m_pData;
                    if (customData < 6)
                    {
                        delete pItem->m_pImgFirst;
                    }
                }
            }
            break;
        default:
            break;
        }
        return CMzWndEx::MzDefWndProc(message,wParam,lParam);
    }

};

class CSampleApp: public CMzApp
{
    ListSample m_MainWnd;

    virtual BOOL Init()
    {
        CoInitializeEx(0, COINIT_MULTITHREADED);

        // Create the main window
        RECT rcWork = MzGetWorkArea();
        m_MainWnd.Create(rcWork.left,rcWork.top,RECT_WIDTH(rcWork),RECT_HEIGHT(rcWork), 0, 0, 0);
        m_MainWnd.SetBgColor(RGB(254,249,220));
        m_MainWnd.Show();

        // return TRUE means init success.
        return TRUE;
    }
};

CSampleApp smapleApp;

Generated at Tue Feb 9 15:09:54 2010 for Meizu M8 SDK Documentation by  doxygen 1.6.1