CListCtrl 一些使用技巧(更新中....)

1、

问:CListCtrl 用DeleteItem删除中间项后,后面的项没有往上推,怎么解决?


答:可以在之后调用Arrange, CListCtrl::Arrange( UINT nCode ) 。


示例:

// Align all of the list view control items along the top

// of the window (the list view control must be in icon or

// small icon mode).


m_myListCtrl.Arrange(LVA_ALIGNTOP);

 

2、

问:

利用: 
CListCtrl   m_List; 
POSITION   pos; 
pos=m_List.GetFirstSelectedItemPosition(); 
int   nItem=m_List.GetNextSelectedItem(pos); 
m_List.DeleteItem(nItem   ); 
删除指定行后,如何让剩余行仍按序排序? 

如                 删除2行后             我需要 
1                         1                           1 
2                         3                           2 
3                         4                           3 
4                         5                           4 
5 

 

答:

BOOL   SortItems( 
      PFNLVCOMPARE   pfnCompare, 
      DWORD_PTR   dwData   
); 

//   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. 
      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); 
} 

  • 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、付费专栏及课程。

余额充值