📄 reportctrl.h
字号:
///////////////////////////////////////////////////////////////////////////////
// ReportCtrl.h
//
// Written by Bin Liu (abinn32@163.com)
//
// Special notes: The sorting methods was inspired by Mark Jackson in his "Sort List
// Control" article on http://www.codeproject.com
//
// This file is the definition of class "CReportCtrl", which is derived from MFC class
// "CListCtrl" and specialized in the "Report" style list control manipulation.
//
// A bunch of methods are implemented or overloaded in this class in order to provide
// fast, efficient and convenient access and operations. For example, it is no more
// necessary to convert all other data types into character strings before
// using "SetItemText", you can pass in whatever into "SetItemText" because
// this class overloaded "SetItemText" for all common data types.
//
// Moreover, using heap pointers as list item data has become more safer since
// the user defined CALLBACK function "BOOL (*) (DWORD)" will be called every time
// _before_ a list item is actually being deleted, therefore you can perform the
// cleanup period to the deletion of each item.
//
// This code may be used in compiled form in any way you desire. This file may be
// redistributed unmodified by any means PROVIDING it is not sold for profit without
// the authors written consent, and providing that this notice and the authors name
// is included. If the source code in this file is used in any commercial application
// then acknowledgement must be made to the author of this file .
//
// This file is provided "as is" with no expressed or implied warranty.
//
// Special notes: The sorting methods was inspired by Mark Jackson in his "Sort List
// Control" article on http://www.codeproject.com
///////////////////////////////////////////////////////////////////////////////
#ifndef __REPORTCTRL_H__
#define __REPORTCTRL_H__
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// 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 only 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);
#include "ReportHeaderCtrl.H"
#include "ListCtrlBase.H"
//#include "..\ReportHeaderCtrl.H"
///////////////////////////////////////////////////////////////////////////
// The CReportCtrl class definition
///////////////////////////////////////////////////////////////////////////
class CReportCtrl : public CListCtrlBase
{
// Construction
public:
CReportCtrl();
virtual ~CReportCtrl();
///////////////////////////////////////////////////////////////////////
// Column attributes & operations
///////////////////////////////////////////////////////////////////////
int GetColumnCount() const; // Get the column count.
BOOL SetHeadings(UINT uiStringID); // Set columns and their formats.
BOOL SetHeadings(const CString& strHeadings); // Set columns and their formats.
int InsertColumn(int nCol, LPCTSTR lpszColumnHeading, int nFormat = LVCFMT_LEFT, int nWidth = -1, int nSubItem = -1);
int InsertColumn(int nCol, const LVCOLUMN* pColumn);
BOOL DeleteColumn(int nCol);
BOOL DeleteAllColumns();
///////////////////////////////////////////////////////////////////////
// Item attributes & operations
///////////////////////////////////////////////////////////////////////
// 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
BOOL IsItemSelected(int nItem) const; // Is item selected?
CString GetItemTextEx(int nItem, LPCSTR pItemName);
BOOL SetItemTextEx(int nItem, LPCSTR pItemName, LPCSTR pText);
int GetSelectedItemCount() const; // How many items are selected?
int GetUnselectedItemCount() const; // How many items are not selected?
int GetFirstSelectedItem(int nStartAfter = -1) const; // Index of the first selected item.
int GetFirstUnselectedItem(int nStartAfter = -1) const; // Index of the first unselected item.
BOOL SelectItem(int nItem, BOOL bSelectAdjacentIfNotAvailable = FALSE); // Select an item
BOOL UnSelectItem(int nItem); // Unselect an item.
BOOL SelectAllItems(); // Select all items.
BOOL UnSelectAllItems(); // Unselect all items.
BOOL InvertSelect(); // Unselect all selected items, and select those were not.
// Checkbox related
BOOL IsItemChecked(int nItem) const; // Is item checked?
int GetCheckedItemCount() const; // How many items are checked?
int GetUncheckedItemCount() const; // How many items are not checked?
int GetFirstCheckedItem(int nStartAfter = -1) const; // Index of the first checked item.
int GetFirstUncheckedItem(int nStartAfter = -1) const; // Index of the first unchecked item.
BOOL CheckItem(int nItem); // Check an item.
BOOL UnCheckItem(int nItem); // Uncheck an item.
void CheckAllItems(); // Check all items.
void UnCheckAllItems(); // Uncheck all items.
void InvertCheck(); // Uncheck all checked items, and check those were not.
// 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(int nItem, LPCTSTR lpszItem);
int InsertItem(const LVITEM* pItem);
int InsertItemEx(int nIndex, LPCTSTR pszText, ...);
// 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 , ITEMDATAPROC lpFunc = NULL, LPARAM lParam = NULL); // Delete an item.
BOOL DeleteItem(int iItem); // Delete an item.
int DeleteAllItems(); // Delete all items.
// int DeleteAllItems(ITEMDATAPROC lpFunc = NULL, LPARAM lParam = NULL); // Delete all items.
int DeleteAllSelectedItems(ITEMDATAPROC lpFunc = NULL, LPARAM lParam = NULL); // Delete all selected items.
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
BOOL SwapItems(int nItem1, int nItem2); // Swap two items in the list, including texts and item data.
BOOL MoveUp(int nItem, int nCount = 1); // Move an item up by "nCount" positions.
BOOL MoveDown(int nItem, int nCount = 1); // Move an item down by "nCount" positions.
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, LPCSTR pItemName, LPCTSTR lpszText); // String.
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.
BOOL SetItemText(int nItem, int nSubItem, ULONG val); // Unsigned long.
BOOL SetItemText(int nItem, int nSubItem, LONG val); // Long.
BOOL SetItemText(int nItem, int nSubItem, UINT val); // Unsigned int.
BOOL SetItemText(int nItem, int nSubItem, INT val); // Int.
BOOL SetItemText(int nItem, int nSubItem, USHORT val); // Unsigned short.
BOOL SetItemText(int nItem, int nSubItem, SHORT val); // Short.
BOOL SetItemText(int nItem, int nSubItem, BYTE val); // Byte.
BOOL SetItemText(int nItem, int nSubItem, TCHAR val); // TCHAR.
// Sorting
void Sort(int iColumn, int iAscending); // Sort a specified column.
void Sort(); // Re-sort previously sorted column, after item insertion or modification.
// Item data operation
BOOL SetItemData(int nItem, DWORD dwData); // Assign the 32-bit application defined data.
DWORD GetItemData(int nItem) const; // Retrieve the 32-bit application defined data.
int GetSubItem(LPCSTR pItemName);
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CReportCtrl)
protected:
virtual void PreSubclassWindow();
//}}AFX_VIRTUAL
protected:
// Protected member methods
BOOL _IsValidIndex(int nIndex) const;
BOOL _ResetTextArray(int nItem);
BOOL _AssignNewItemData(int iItem, LPCTSTR lpItem);
void _FreeItemMemory(const int iItem);
BOOL _AssignNewItemData(int iItem, const CString* pTexts, int nSize);
// Static methods used for sorting and comparison
static int CALLBACK _CompareFunction(LPARAM lParam1, LPARAM lParam2, LPARAM lParamData);
static int _StringCompare(const CString& s1, const CString& s2);
static BOOL _IsNumber(LPCTSTR pszText);
static int _NumberCompare(LPCTSTR pszNumber1, LPCTSTR pszNumber2);
static BOOL _IsDate(LPCTSTR pszText);
static int _DateCompare(const CString& strDate1, const CString& strDate2);
////////////////////////////////////////////////////////////////////////////////
// The header control only used by CReportCtrl
////////////////////////////////////////////////////////////////////////////////
/* class CReportHeaderCtrl : public CHeaderCtrl
{
public:
CReportHeaderCtrl();
virtual ~CReportHeaderCtrl() {};
void SetSortedColumn(int nCol);
void SetSortAscending(int bAscending);
int IsSortAscending() const;
int GetSortedColumn() const;
void UpdateSortArrow();
protected:
void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
int m_iSortColumn;
int m_bSortAscending;
protected:
// Wizzard generated stuff
//{{AFX_MSG(CReportHeaderCtrl)
//}}AFX_MSG
};
*/
// Member data
CReportHeaderCtrl m_wndHeader;
protected:
// Wizzard generated stuff
//{{AFX_MSG(CReportCtrl)
afx_msg void OnColumnClick(NMHDR* pNMHDR, LRESULT* pResult);
afx_msg void OnDestroy();
afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
#endif // __REPORTCTRL_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -