CListCtrl的使用完全指南

CListCtrl的使用完全指南 转自:http://www.programbbs.com/doc/2887.htm
创建图形列表并和CListCtrl关联:
 m_image_list.Create(IDB_CALLER2, 16, 10, RGB(192,192, 192));
 m_image_list.SetBkColor( GetSysColor( COLOR_WINDOW ) );
 m_caller_list.SetImageList( &m_image_list, LVSIL_SMALL);
为报表添加4列:
  char *szColumn[]={"昵称","IP地址","登陆时间","状态"};
  int widths[]={100,98,70,55};
  LV_COLUMN lvc;
  lvc.mask=LVCF_FMT|LVCF_WIDTH|LVCF_TEXT|LVCF_SUBITEM;
  lvc.fmt=LVCFMT_LEFT;
  for(int i=0;i<4;i++) {//插入各列
   lvc.pszText=szColumn[i];
   lvc.cx=widths[i];
   lvc.iSubItem=i;
   m_caller_list.InsertColumn(i,&lvc);
  }
为报表添加两项,以附加方式添加:
 char* data[4];
 data[0]="所有人";
 data[1]="0.0.0.0";
 data[3]="在线";
 data[2]=new char;
 CTime now=CTime::GetCurrentTime();
       CString temp = now.Format("%H:%M:%S");
 data[2]=temp.GetBuffer(1);
 LV_ITEM lvi;
 lvi.mask=LVIF_TEXT|LVIF_IMAGE|LVIF_PARAM;
 lvi.iSubItem=0;
 lvi.pszText=(char *)data[0];
 lvi.iImage = 0;
 lvi.iItem=0;
 m_caller_list.InsertItem(&lvi);
 for (int j=0;j<4;j++) m_caller_list.SetItemText(count,j,data[j]);
 count++;
 lvi.iImage = 1;
 lvi.iItem=count;
 m_caller_list.InsertItem(&lvi);
 data[0]="cherami";
 data[1]="127.0.0.1";
 for (int n=0;n<4;n++) m_caller_list.SetItemText(count,n,data[n]);
 count++;
设置报表的样式
选中一整行:
m_list_ctrl.SetExtendedStyle(m_list_ctrl.GetExtendedStyle()|LVS_EX_FULLROWSELECT); 
绘制表格:
m_list_ctrl.SetExtendedStyle(m_list_ctrl.GetExtendedStyle()|LVS_EX_GRIDLINES);
带复选框:
m_list_ctrl.SetExtendedStyle(m_list_ctrl.GetExtendedStyle()|LVS_EX_CHECKBOXES);
自动切换:
m_list_ctrl.SetExtendedStyle(m_list_ctrl.GetExtendedStyle()|LVS_EX_TRACKSELECT);
选定一行:
设置CListCtrl的Show selection always选项
SetItemState (iIndex, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED)
 
选中一个或多个项目时,会发送LVN_ITEMCHANGED消息,可以使用
GetSelectedCount()方法得到被选定的项的数目。
点击列头的消息响应:
ON_NOTIFY(HDN_ITEMCLICKW, 0, ResponseFunc)
消息,需要自己添加
或者:
ON_NOTIFY(LVN_COLUMNCLICK, ID_yourCtrl,  ResponseFunc)//向导添加
前者后响应,后者先响应
响应函数:
ResponseFunc(NMHDR *pNMHDR, LRESULT *pResult)
双击CListCtrl中的ITEM的消息是及消息函数:
ON_NOTIFY(NM_DBLCLK, ID_yourCtrl, ResponseFunc)
单击ITEM的消息响应:
ON_NOTIFY(NM_CLICK, ID_yourCtrl, ResponseFunc)
ResponseFunc(NMHDR *pNMHDR, LRESULT *pResult)

HDN_ITEMCLICK    就是Header control Notify message for mouse left click on the Header control!
而HDN_ITEMCLICK是当List View中存在一个Header Contrl时,Header Ctrl通知父窗口List View的!
CListCtrl中的Item被选中触发LBN_SELCHANGE(通过WM_COMMAND)消息!
删除CListCtrl中选定的项:
POSITION pos;
int nIndex;
for(; pos= GetFirstSelectedItemPosition();)
{
nIndex = GetNextSelectedItem(pos);
DeleteItem(nIndex);
}
在ListCtrl中进行排序
列表控件(CListCtrl)的顶部有一排按钮,用户可以通过选择不同的列来对记录进行排序。但是 CListCtrl并没有自动排序的功能,我们需要自己添加一个用于排序的回调函数来比较两个数据的大小,此外还需要响应排序按钮被点击的消息。下面讲述一下具体的做法。
CListCtrl提供了用于排序的函数,函数原型为:BOOL CListCtrl::SortItems( PFNLVCOMPARE pfnCompare, DWORD dwData )。其中第一个参数为全局排序函数的地址,第二个参数为用户数据,你可以根据你的需要传递一个数据或是指针。该函数返回-1代表第一项排应在第二项前面,返回1代表第一项排应在第二项后面,返回0代表两项相等。
用于排序的函数原形为:int CALLBACK ListCompare(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort),其中第三个参数为调用者传递的数据(即调用SortItems时的第二个参数dwData)。第一和第二个参数为用于比较的两项的ItemData,你可以通过DWORD CListCtrl::GetItemData( int nItem )/BOOL CListCtrl::SetItemData( int nItem, DWORD dwData )来对每一项的ItemData进行存取。在添加项时选用特定的CListCtrl::InsertItem也可以设置该值。由于你在排序时只能通过该值来确定项的位置所以你应该比较明确的确定该值的含义。
最后一点,我们需要知道什么时候需要排序,实现这点可以在父窗口中对LVN_COLUMNCLICK消息进行处理来实现。
下面我们看一个例子,这个例子是一个派生类,并支持顺序/倒序两种方式排序。为了简单我对全局数据进行排序,而在实际应用中会有多组需要排序的数据,所以需要通过传递参数的方式来告诉派序函数需要对什么数据进行排序。

//全局数据
struct DEMO_DATA
{
 char szName[20];
 int iAge;
}strAllData[5]={{"王某",30},{"张某",40},{"武某",32},{"陈某",20},{"李某",36}};
//CListCtrl派生类定义
class CSortList : public CListCtrl
{
// Construction
public:
 CSortList();
 BOOL m_fAsc;//是否顺序排序
 int m_nSortedCol;//当前排序的列
protected:
 //{{AFX_MSG(CSortList)
 //}}AFX_MSG
...
};
//父窗口中包含该CListCtrl派生类对象
class CSort_in_list_ctrlDlg : public CDialog
{
// Construction
public:
 CSort_in_list_ctrlDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
 //{{AFX_DATA(CSort_in_list_ctrlDlg)
 enum { IDD = IDD_SORT_IN_LIST_CTRL_DIALOG };
 CSortList m_listTest;
 //}}AFX_DATA
}
//在父窗口中定义LVN_COLUMNCLICK消息映射
BEGIN_MESSAGE_MAP(CSort_in_list_ctrlDlg, CDialog)
 //{{AFX_MSG_MAP(CSort_in_list_ctrlDlg)
 ON_NOTIFY(LVN_COLUMNCLICK, IDC_LIST1, OnColumnclickList1)
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()
//初始化数据
BOOL CSort_in_list_ctrlDlg::OnInitDialog()
{
 CDialog::OnInitDialog();
 //初始化ListCtrl中数据列表
 m_listTest.InsertColumn(0,"姓名");
 m_listTest.InsertColumn(1,"年龄");
 m_listTest.SetColumnWidth(0,80);
 m_listTest.SetColumnWidth(1,80);
 for(int i=0;i<5;i++)
 {
  m_listTest.InsertItem(i,strAllData[i].szName);
  char szAge[10];
  sprintf(szAge,"%d",strAllData[i].iAge);
  m_listTest.SetItemText(i,1,szAge);
  //设置每项的ItemData为数组中数据的索引
  //在排序函数中通过该ItemData来确定数据
  m_listTest.SetItemData(i,i);
 }
 return TRUE;  // return TRUE  unless you set the focus to a control
}
//处理消息
void CSort_in_list_ctrlDlg::OnColumnclickList1(NMHDR* pNMHDR, LRESULT* pResult)
{
 NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
 //设置排序方式
 if( pNMListView->iSubItem == m_listTest.m_nSortedCol )
  m_listTest.m_fAsc = !m_listTest.m_fAsc;
 else
 {
  m_listTest.m_fAsc = TRUE;
  m_listTest.m_nSortedCol = pNMListView->iSubItem;
 }
 //调用排序函数
 m_listTest.SortItems( ListCompare, (DWORD)&m_listTest );       
 *pResult = 0;
}
//排序函数实现
int CALLBACK ListCompare(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
 //通过传递的参数来得到CSortList对象指针,从而得到排序方式
 CSortList* pV=(CSortList*)lParamSort;
 
 //通过ItemData来确定数据
 DEMO_DATA* pInfo1=strAllData+lParam1;
 DEMO_DATA* pInfo2=strAllData+lParam2;
 CString szComp1,szComp2;
 int iCompRes;
 switch(pV->m_nSortedCol)
 {
 case(0):
  //以第一列为根据排序
  szComp1=pInfo1->szName;
  szComp2=pInfo2->szName;
  iCompRes=szComp1.Compare(szComp2);
  break;
 case(1):
  //以第二列为根据排序
  if(pInfo1->iAge == pInfo2->iAge)
   iCompRes = 0;
  else
   iCompRes=(pInfo1->iAge < pInfo2->iAge)?-1:1;
  break;
 default:
  ASSERT(0);
  break;
 }
 //根据当前的排序方式进行调整
 if(pV->m_fAsc)
  return iCompRes;
 else
  return iCompRes*-1;
}
排序最快:
CListCtrl::SortItems
Example
// Sort the item in reverse alphabetical order.
static int CALLBACK
MyCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
  // lParamSort contains a pointer to the list view control.
  // The lParam of an item is just its index.
  CListCtrl* pListCtrl = (CListCtrl*) lParamSort;
  CString    strItem1 = pListCtrl->GetItemText(lParam1, 0);
  CString    strItem2 = pListCtrl->GetItemText(lParam2, 0);
  return strcmp(strItem2, strItem1);
}
void snip_CListCtrl_SortItems()
{
  // The pointer to my list view control.
  extern CListCtrl* pmyListCtrl;
  // Sort the list view items using my callback procedure.
  pmyListCtrl->SortItems(MyCompareProc, (LPARAM) pmyListCtrl);
}

If you don’t want to allow the users to sort the list by clicking on the header, you can use the style LVS_NOSORTHEADER. However, if you do want to allow sorting, you do not specify the LVS_NOSORTHEADER. The control, though, does not sort the items. You have to handle the HDN_ITEMCLICK notification from the header control and process it appropriately. In the code below, we have used the sorting function SortTextItems() developed in a previous section. You may choose to sort the items in a different manner.
Step 1: Add two member variables
Add two member variables to the CListCtrl. The first variable to track which column has been sorted on, if any. The second variable to track if the sort is ascending or descending.
        int nSortedCol;
        BOOL bSortAscending;
 
Step 2: Initialize them in the constructor.
Initialize nSortedCol to -1 to indicate that no column has been sorted on. If the list is initially sorted, then this variable should reflect that.
 
        nSortedCol = -1;
        bSortAscending = TRUE;
 
Step 3: Add entry in message map to handle HDN_ITEMCLICK
Actually you need to add two entries. For HDN_ITEMCLICKA and HDN_ITEMCLICKW. Do not use the class wizard to add the entry. For one, you need to add two entries whereas the class wizard will allow you only one. Secondly, the class wizard uses the wrong macro in the entry. It uses ON_NOTIFY_REFLECT() instead of ON_NOTIFY(). Since the HDN_ITEMCLICK is a notification from the header control to the list view control, it is a direct notification and not a reflected one.
ON_NOTIFY(HDN_ITEMCLICKA, 0, OnHeaderClicked)
ON_NOTIFY(HDN_ITEMCLICKW, 0, OnHeaderClicked)
 Note that we specify the same function for both the notification. Actually the program will receive one or the other and not both. What notification it receives will depend on the OS. The list view control on Windows 95 will send the ANSI version and the control on NT will send the UNICODE version.
Also, note that the second argument is zero. This value filters for the id of the control and we know that header control id is zero.
Step 4: Write the OnHeaderClicked() function
Here’s where you decide what to do when the user clicks on a column header. The expected behaviour is to sort the list based on the values of the items in that column. In this function we have used the SortTextItems() function developed in a previous section. If any of the columns displays numeric or date values, then you would have to provide custom sorting for them.
 
void CMyListCtrl::OnHeaderClicked(NMHDR* pNMHDR, LRESULT* pResult)
{
        HD_NOTIFY *phdn = (HD_NOTIFY *) pNMHDR;
        if( phdn->iButton == 0 )
        {
                // User clicked on header using left mouse button
                if( phdn->iItem == nSortedCol )
                        bSortAscending = !bSortAscending;
                else
                        bSortAscending = TRUE;
                nSortedCol = phdn->iItem;
                SortTextItems( nSortedCol, bSortAscending );
        }
        *pResult = 0;
}
让CListCtrl的SubItem也具有编辑功能:
要重载一个文本框,然后在LVN_BEGINLABELEDIT时改变文本框位置。
CInEdit m_InEdit;
    if( ( GetStyle() & LVS_TYPEMASK ) == LVS_REPORT && ( m_nEditSubItem != 0 ) )
    {
        HWND    hwndEdit;
        CRect    rtBound;
        CString strText;
        hwndEdit = (HWND)SendMessage( LVM_GETEDITCONTROL );
        GetSubItemRect( pDispInfo->item.iItem, m_nEditSubItem, LVIR_LABEL, rtBound );
        m_InEdit.SubclassWindow( hwndEdit );
        m_InEdit.m_left = rtBound.left;
        strText = GetItemText( pDispInfo->item.iItem, m_nEditSubItem );
        m_InEdit.SetWindowText( strText );
    }
void CInEdit::OnWindowPosChanging(WINDOWPOS FAR* lpwndpos)
{
    CRect rtClient;
    lpwndpos->x = m_left;  // m_left在LVN_BEGINLABELEDIT中设置
    CEdit::OnWindowPosChanging(lpwndpos);
   
    // TODO: Add your message handler code here
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在C++中使用链表和CListCtrl来实现数据的页显示,你可以将链表中的数据逐个添加到CListCtrl控件中,并根据当前页码和分页大小显示对应的数据。 以下是一个示例代码,展示了如何使用链表和CListCtrl来实现数据的分页显示: ```cpp #include <afxwin.h> #include <afxcmn.h> #include <vector> struct DataItem { int id; CString name; }; class CMyDialog : public CDialog { private: CListCtrl m_listCtrl; std::vector<DataItem> m_data; int m_pageSize; int m_currentPage; public: CMyDialog() : CDialog(IDD_MYDIALOG) { m_pageSize = 5; m_currentPage = 1; } protected: virtual BOOL OnInitDialog() { CDialog::OnInitDialog(); // 创建CListCtrl控件 m_listCtrl.Create(WS_CHILD | WS_VISIBLE | LVS_REPORT, CRect(10, 10, 300, 200), this, IDC_LIST_CTRL); m_listCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT); // 添加列标题 m_listCtrl.InsertColumn(0, _T("ID"), LVCFMT_LEFT, 50); m_listCtrl.InsertColumn(1, _T("Name"), LVCFMT_LEFT, 100); // 添加示例数据到链表 for (int i = 1; i <= 20; i++) { CString name; name.Format(_T("Item %d"), i); m_data.push_back({i, name}); } // 显示第一页数据 ShowPage(m_currentPage); return TRUE; } void ShowPage(int pageNumber) { // 清空列表 m_listCtrl.DeleteAllItems(); // 计算当前页的起始索引和结束索引 int startIndex = (pageNumber - 1) * m_pageSize; int endIndex = startIndex + m_pageSize - 1; if (endIndex >= m_data.size()) { endIndex = m_data.size() - 1; } // 添加当前页的数据到列表 for (int i = startIndex; i <= endIndex; i++) { const DataItem& item = m_data[i]; int index = m_listCtrl.InsertItem(i - startIndex, _T("")); m_listCtrl.SetItemText(index, 0, CString(item.id)); m_listCtrl.SetItemText(index, 1, item.name); } } afx_msg void OnBnClickedPrevButton() { if (m_currentPage > 1) { m_currentPage--; ShowPage(m_currentPage); } } afx_msg void OnBnClickedNextButton() { int totalPages = (m_data.size() + m_pageSize - 1) / m_pageSize; if (m_currentPage < totalPages) { m_currentPage++; ShowPage(m_currentPage); } } DECLARE_MESSAGE_MAP() }; BEGIN_MESSAGE_MAP(CMyDialog, CDialog) ON_BN_CLICKED(IDC_PREV_BUTTON, &CMyDialog::OnBnClickedPrevButton) ON_BN_CLICKED(IDC_NEXT_BUTTON, &CMyDialog::OnBnClickedNextButton) END_MESSAGE_MAP() int main() { AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0); CMyDialog dlg; dlg.DoModal(); return 0; } ``` 在这个示例代码中,创建了一个自定义的对话框类`CMyDialog`,其中包含一个CListCtrl控件用于显示数据。 在`OnInitDialog`函数中,首先创建了CListCtrl控件,并设置了扩展样式和列标题。然后,添加了一些示例数据到链表。 `ShowPage`函数用于根据当前页码显示对应的数据。首先,清空列表中的所有项。然后,根据当前页码计算起始索引和结束索引,并将对应的数据逐个添加到列表中。 在`OnBnClickedPrevButton`和`OnBnClickedNextButton`消息处理函数中,分别处理上一页和下一页按钮的点击事件。通过更新当前页码并调用`ShowPage`函数来显示上一页或下一页的数据。 最后,在`main`函数中创建了`CMyDialog`对象,并显示对话框。 运行这段代码,你会看到一个带有上一页和下一页按钮的对话框,点击按钮可以切换数据的分页显示。 这个示例代码基于MFC框架,使用了MFC的消息映射和对话框类。如果你不熟悉MFC,你可以根据自己的需求将相关代码移植到其他框架或平台上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值