列表控件
此示例展示以下控件的使用方法:
列表控件UiList 的使用流程
UiList m_List;
//启用竖直滚动条 m_List.EnableScrollBarV(true); //设置控件是否启用鼠标通知(发送MZ_WM_MOUSE_NOTIFY到它所在的窗口) m_List.EnableNotifyMessage(true);
//根据坐标计算列表的索引 int nIndex = m_List.CalcIndexOfPos(x, y); //设置索引项为当前选中项 m_List.SetSelectedIndex(nIndex); //以下两步操作使控件更新 m_List.Invalidate(); m_List.Update();
switch(message) { //此处捕获鼠标通知事件(如果开启,在EnableNotifyMessage()中设置) case MZ_WM_MOUSE_NOTIFY: { //鼠标事件的当前控件ID int nID = LOWORD(wParam); //鼠标事件的类型,详见MzUser.h int nNotify = HIWORD(wParam); //当前鼠标的x,y坐标 int x = LOWORD(lParam); int y = HIWORD(lParam); //此处 //在符合某条件的情况下,对事件的处理 break; } }
往列表控件添加项:
CMzString str(128); wchar_t *buf = L"List item %d"; ListItem li; for (int i=0;i<50;i++) { wsprintf(str.C_Str(), buf, i); li.Text = str; m_List.AddItem(li); }
重载 MZFC 的消息处理函数:
LRESULT MzDefWndProc(UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { case MZ_WM_MOUSE_NOTIFY: { int nID = LOWORD(wParam); int nNotify = HIWORD(wParam); int x = LOWORD(lParam); int y = HIWORD(lParam); if (nID==MZ_IDC_LIST && nNotify==MZ_MN_LBUTTONDOWN) { if (!m_List.IsMouseDownAtScrolling() && !m_List.IsMouseMoved()) { int nIndex = m_List.CalcIndexOfPos(x, y); m_List.SetSelectedIndex(nIndex); m_List.Invalidate(); m_List.Update(); } return 0; } if (nID==MZ_IDC_LIST && nNotify==MZ_MN_MOUSEMOVE) { if(m_List.GetSelectedIndex()!=-1) { m_List.SetSelectedIndex(-1); m_List.Invalidate(); m_List.Update(); } return 0; } //if (nID==MZ_IDC_LIST && nNotify==MZ_MN_LBUTTONDOWN) //{ // if (!m_List.IsMouseDownAtScrolling() && !m_List.IsMouseMoved()) // { // m_List.SetSelectedIndex(-1); // m_List.Invalidate(); // m_List.Update(); // } // return 0; //} } return 0; } 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: Michael * Create on: 2008-12-12 */ /************************************************************************/ //请按照以步骤运行此实例代码: //首先, 打开VS2005/2008创建一个Win 32智能设备项目 //在项目向导中选择M8SDK, 并勾选空项目 //然后,在项目中新建一个cpp文件,将此处代码拷贝到cpp文件中 //最后,按照M8SDK的帮助文档,配置项目属性 //现在,可以运行此程序了 //包含MZFC库的头文件 #include <mzfc_inc.h> //此代码演示了: // 创建和初始化应用程序 // 创建和初始化窗体 // 控件的显示、隐藏, // 文字按钮工具条、弹出菜单、消息框的使用 // 列表控件的使用(从 UiList 派生),并处理它的鼠标通知消息MZ_WM_MOUSE_NOTIFY #define MZ_IDC_LIST 101 #define MZ_IDC_TOOLBAR1 102 #define IDC_PPM_OK 103 #define IDC_PPM_CANCEL 104 // 从 CMzWndEx 派生的主窗口类 class CSample1MainWnd: public CMzWndEx { MZ_DECLARE_DYNAMIC(CSample1MainWnd); public: UiToolbar_Text m_Toolbar; UiList m_List; protected: // 窗口的初始化 virtual BOOL OnInitDialog() { // 必须先调用基类的初始化 if (!CMzWndEx::OnInitDialog()) { return FALSE; } // 初始化窗口中的控件 m_List.SetPos(0,0,GetWidth(),GetHeight()-MZM_HEIGHT_TEXT_TOOLBAR); m_List.SetID(MZ_IDC_LIST); m_List.EnableScrollBarV(true); m_List.EnableNotifyMessage(true); AddUiWin(&m_List); // 往列表控件添加项 CMzString str(128); wchar_t *buf = L"List item %d"; ListItem li; for (int i=0;i<50;i++) { wsprintf(str.C_Str(), buf, i); li.Text = str; m_List.AddItem(li); } m_Toolbar.SetPos(0,GetHeight()-MZM_HEIGHT_TEXT_TOOLBAR,GetWidth(),MZM_HEIGHT_TEXT_TOOLBAR); m_Toolbar.SetButton(0, true, true, L"Exit"); m_Toolbar.SetButton(1, true, true, L"Delete"); m_Toolbar.SetButton(2, true, true, L"Setting"); m_Toolbar.SetID(MZ_IDC_TOOLBAR1); AddUiWin(&m_Toolbar); return TRUE; } // 重载 MZFC 的消息处理函数 LRESULT MzDefWndProc(UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { case MZ_WM_MOUSE_NOTIFY: { int nID = LOWORD(wParam); int nNotify = HIWORD(wParam); int x = LOWORD(lParam); int y = HIWORD(lParam); if (nID==MZ_IDC_LIST && nNotify==MZ_MN_LBUTTONDOWN) { if (!m_List.IsMouseDownAtScrolling() && !m_List.IsMouseMoved()) { int nIndex = m_List.CalcIndexOfPos(x, y); m_List.SetSelectedIndex(nIndex); m_List.Invalidate(); m_List.Update(); } return 0; } if (nID==MZ_IDC_LIST && nNotify==MZ_MN_MOUSEMOVE) { if(m_List.GetSelectedIndex()!=-1) { m_List.SetSelectedIndex(-1); m_List.Invalidate(); m_List.Update(); } return 0; } //if (nID==MZ_IDC_LIST && nNotify==MZ_MN_LBUTTONDOWN) //{ // if (!m_List.IsMouseDownAtScrolling() && !m_List.IsMouseMoved()) // { // m_List.SetSelectedIndex(-1); // m_List.Invalidate(); // m_List.Update(); // } // return 0; //} } return 0; } return CMzWndEx::MzDefWndProc(message,wParam,lParam); } // 重载 MZFC 的命令消息处理函数 virtual void OnMzCommand(WPARAM wParam, LPARAM lParam) { UINT_PTR id = LOWORD(wParam); switch(id) { case MZ_IDC_TOOLBAR1: { int nIndex = lParam; if (nIndex==0) { CMzString str(128); wsprintf(str.C_Str(), L"You pressed the %s button!", m_Toolbar.GetButtonText(0).C_Str()); MzMessageBoxEx(m_hWnd, str.C_Str(), L"Test", MB_OK); PostQuitMessage(0); return; } if (nIndex==1) { // 弹出菜单 CPopupMenu ppm; struct PopupMenuItemProp pmip; pmip.itemCr = MZC_BUTTON_PELLUCID; pmip.itemRetID = IDC_PPM_CANCEL; pmip.str = L"Cancel"; ppm.AddItem(pmip); pmip.itemCr = MZC_BUTTON_ORANGE; pmip.itemRetID = IDC_PPM_OK; pmip.str = L"OK"; ppm.AddItem(pmip); RECT rc = MzGetWorkArea(); rc.top = rc.bottom - ppm.GetHeight(); ppm.Create(rc.left,rc.top,RECT_WIDTH(rc),RECT_HEIGHT(rc),m_hWnd,0,WS_POPUP); int nID = ppm.DoModal(); if (nID==IDC_PPM_OK) { // do what you want... int nSel = m_List.GetSelectedIndex(); m_List.RemoveItem(nSel); m_List.SetSelectedIndex(-1); m_List.Invalidate(); m_List.Update(); } if (nID==IDC_PPM_CANCEL) { // do what you want... } return; } if (nIndex==2) { CMzString str(128); wsprintf(str.C_Str(), L"You pressed the %s button!", (LPWSTR)m_Toolbar.GetButtonText(2)); MzMessageBoxEx(m_hWnd, str.C_Str(), L"Test", MB_OK); return; } } break; } } }; MZ_IMPLEMENT_DYNAMIC(CSample1MainWnd) // 从 CMzApp 派生的应用程序类 class CSample1App: public CMzApp { public: // 应用程序的主窗口 CSample1MainWnd m_MainWnd; // 应用程序的初始化 virtual BOOL Init() { // 初始化 COM 组件 CoInitializeEx(0, COINIT_MULTITHREADED); // 创建主窗口 RECT rcWork = MzGetWorkArea(); m_MainWnd.Create(rcWork.left,rcWork.top,RECT_WIDTH(rcWork),RECT_HEIGHT(rcWork), 0, 0, 0); m_MainWnd.SetBgColor(MzGetThemeColor(TCI_WINDOW_BG)); m_MainWnd.Show(); // 成功则返回TRUE return TRUE; } }; // 全局的应用程序对象 CSample1App theApp;