CReportCtrl - 报表样式 CListCtrl 的极其便捷的版本






4.79/5 (35投票s)
2003年4月26日
2分钟阅读

265062

4639
一个专门用于报表风格列表控件操作的 MFC CListCtrl 派生类。
引言
“报表风格”列表控件在各种情况下都很常用,虽然我们可以直接使用 MFC CListCtrl
类并使用 LVS_REPORT
样式指定它,但如果你问我,这还不够。
首先,它用起来有点不方便,特别是当您需要向上或向下移动特定项目,或交换两个项目,或执行诸如“反转选择”之类的操作时,您将不得不编写相当多的代码。此外,CListCtrl
成员方法 SetItemText
仅接受字符串值,这可能很麻烦,因为您必须首先将每个数据转换为字符串。
其次,CListCtrl
缺少一些它本应该具备的必要功能。例如,通过单击列标题进行项目排序,这在大多数报表风格列表控件中始终是必须的,但我们需要编写自己的代码(相当复杂的代码)来实现它。另一个例子是“项目数据”,我们经常使用堆指针作为项目数据,以便将列表项目与一些实际结构相关联。在这种情况下,记住在删除任何项目之前进行清理至关重要,因为我们可能在项目删除后没有机会进行清理。如果有一个回调函数在项目即将从列表控件中删除时自动调用,无论哪个项目以及何时删除,那岂不是很好?
好的,让我们看看我们能拥有什么。CReportCtrl
是一个 MFC CListCtrl
派生类,专门用于报表风格列表控件操作。在此类中实现或重载了一堆方法,以便提供快速、高效和方便的访问和操作。我说这个类完全是为了方便。
特点
不再需要在使用 SetItemText
之前将所有其他数据类型转换为字符串,因为此方法已针对所有常见数据类型进行了重载,并考虑了 float/double 数字的精度。
将堆指针用作列表项目数据已变得更安全,因为用户定义的 CALLBACK
函数 BOOL (*) (DWORD, LPARAM)
将在每次实际删除列表项目之前调用。因此,您可以在每个项目删除之前执行清理周期,或者如果您觉得事情出错,可以在 DeleteAllItems
过程中随时中止删除。
实现了项目排序,比较函数将根据特定子项目文本的外观(数字、字符串或日期时间格式)来确定如何比较您的项目。您的最终用户只需单击列表标题列,所有项目将以所需的方式排序。
为了纯粹的方便,广泛地实现了项目选择/取消选择、检查/取消检查和重新定位。
公共方法
回调函数
// The callback function which is // optionally defined by you, the programmer. // A very common useage of this function // is to free item data if they are heap // pointers, before each list item is deleted. // The item data will be passed in as // the first parameter. // You may return FALSE if you want to // abort the deletion of current item, if you // want to proceed, you must return TRUE. typedef BOOL (CALLBACK *ITEMDATAPROC)(DWORD, LPARAM);
列属性和操作
int GetColumnCount() const; // Get the column count. // Set columns and their formats. BOOL SetHeadings(UINT uiStringID); // Set columns and their formats. BOOL SetHeadings(const CString& strHeadings); // How to use "SetHeadings": // "strHeadings" can be the following format: // "column_name, format, width; column_name, format, width; ...", where: // "column_name" is the title of the column. // "format" is format of the column(0=LVCFMT_LEFT, // 1=LVCFMT_CENTER, 2=LVCFMT_RIGHT), if you don't specify a format, // it will be LVCFMT_LEFT by default. // "width" is width of that column, in pixels. // For example, // SetHeadings(_T("ID, 1, 75; Name, 100; Salary, 2, 60")); // will make the CreportCtrl have 3 columns, // the first column is "ID" with LVCFMT_CENTER format // and 75 pixels wide, the second column is "Name" with // LVCFMT_LEFT format and 100 pixels wide, the third column is // "Salary" with LVCFMT_RIGHT format and 60 pixels wide. int InsertColumn(int nCol, const LVCOLUMN* pColumn); // Insert a new column int InsertColumn(int nCol, LPCTSTR lpszColumnHeading, int nFormat = LVCFMT_LEFT, int nWidth = -1, int nSubItem = -1); BOOL DeleteColumn(int nCol); // Delete a column BOOL DeleteAllColumns(); // Delete all columns.
项目属性和操作
// List style operation void SetGridLines(BOOL bSet = TRUE); // Show grid lines. void SetFullRowSelect(BOOL bSet = TRUE); // Use full row selection style. void SetCheckboxes(BOOL bSet = TRUE); // Show checkboxes. // Selection related // Is item selected? BOOL IsItemSelected(int nItem) const; // How many items are selected? int GetSelectedItemCount() const; // How many items are not selected? int GetUnselectedItemCount() const; // Index of the first selected item. int GetFirstSelectedItem(int nStartAfter = -1) const; // Index of the first unselected item. int GetFirstUnselectedItem(int nStartAfter = -1) const; // Select an item. BOOL SelectItem(int nItem, BOOL bSelectAdjacentIfNotAvailable = FALSE); // Unselect an item. BOOL UnSelectItem(int nItem); // Select all items. BOOL SelectAllItems(); // Unselect all items. BOOL UnSelectAllItems(); // Unselect all selected items, // and select those were not. BOOL InvertSelect(); // Checkbox related // Is item checked? BOOL IsItemChecked(int nItem) const; // How many items are checked? int GetCheckedItemCount() const; // How many items are not checked? int GetUncheckedItemCount() const; // Index of the first checked item. int GetFirstCheckedItem(int nStartAfter = -1) const; // Index of the first unchecked item. int GetFirstUncheckedItem(int nStartAfter = -1) const; // Check an item. BOOL CheckItem(int nItem); // Uncheck an item. BOOL UnCheckItem(int nItem); // Check all items. void CheckAllItems(); // Uncheck all items. void UnCheckAllItems(); // Uncheck all checked items, and check those were not. void InvertCheck(); // Item Insertion int InsertItem(UINT nMask, int nItem, LPCTSTR lpszItem, UINT nState, UINT nStateMask, int nImage, LPARAM lParam); int InsertItem(int nItem, LPCTSTR lpszItem, int nImage); int InsertItem(const LVITEM* pItem); int InsertItem(int nIndex, LPCTSTR pszText, ...); // How to use "InsertItem(int nIndex, LPCTSTR pszText, ...)": // Take the same example we used to demonstrate "SetHeadings", if we use: // InsertItem(INT_MAX, _T("001"), _T("John Smith"), _T("52000.00")); // We are appending(inserting at the last) a new item // into this CReportCtrl, item text of the first column(ID) // is "001", item text of the second column(Name) // is "John Smith", item text of the // third column(Salary) is "52000.00". // Item Deletion // Note: if the item data are heap pointers, // make sure to free them before calling // any item deletion methods, or define and pass in callback function // "ITEMDATAPROC" and do the memory cleanup within. BOOL DeleteItem(int iItem, BOOL bSelectNextItem = FALSE, ITEMDATAPROC lpFunc = NULL, LPARAM lParam = NULL); // Delete an item. // Delete all items. Returns number of // items those are actually deleted. int DeleteAllItems(ITEMDATAPROC lpFunc = NULL, LPARAM lParam = NULL); // Delete all selected items. int DeleteAllSelectedItems(ITEMDATAPROC lpFunc = NULL, LPARAM lParam = NULL); int DeleteAllUnselectedItems(ITEMDATAPROC lpFunc = NULL, LPARAM lParam = NULL); // Delete all unselected items. int DeleteAllCheckedItems(ITEMDATAPROC lpFunc = NULL, LPARAM lParam = NULL); // Delete all checked items. int DeleteAllUncheckedItems(ITEMDATAPROC lpFunc = NULL, LPARAM lParam = NULL); // Delete all unchecked items. // Item position related // Swap two items in the list, // including texts and item data. BOOL SwapItems(int nItem1, int nItem2); // Move an item up by "nCount" positions. BOOL MoveUp(int nItem, int nCount = 1); // Move an item down by "nCount" positions. BOOL MoveDown(int nItem, int nCount = 1); BOOL MoveToTop(int nItem); // Move an item up to the top. BOOL MoveToBottom(int nItem); // Move an item down to the bottom. // Convenient versions of "CListCtrl::SetItemText" BOOL SetItemText(int nItem, int nSubItem, LPCTSTR lpszText); // String. BOOL SetItemText(int nItem, int nSubItem, DOUBLE val, int nPrecision = -1); // Double. BOOL SetItemText(int nItem, int nSubItem, FLOAT val, int nPrecision = -1); // Float.