ListCtrlEx.h
上传用户:zhouyunkk
上传日期:2022-07-16
资源大小:98k
文件大小:8k
源码类别:

ListView/ListBox

开发平台:

Visual C++

  1. #pragma once
  2. #include "MsgHook.h"
  3. #include<Afxmt.h>
  4. // CListCtrlEx
  5. typedef BOOL (*PFNEDITORCALLBACK)(CWnd** pWnd, int nRow, int nColumn, CString& strSubItemText, DWORD_PTR dwItemData, void* pThis, BOOL bUpdate);
  6. class CListCtrlEx : public CListCtrl
  7. {
  8. DECLARE_DYNAMIC(CListCtrlEx)
  9. public:
  10. CListCtrlEx();
  11. virtual ~CListCtrlEx();
  12. // Retrieves the data (lParam) associated with a particular item.
  13. DWORD_PTR GetItemData(int nItem) const;
  14. // Retrieves the data (lParam) associated with a particular item.
  15. DWORD_PTR GetItemDataInternal(int nItem) const;
  16. // Sets the data (lParam) associated with a particular item.
  17. BOOL SetItemData(int nItem, DWORD_PTR dwData);
  18. // Removes a single item from the control.
  19. BOOL DeleteItem(int nItem);
  20. // Removes all items from the control.
  21. BOOL DeleteAllItems();
  22. // Finds an item in the control matching the specified criteria.  
  23. int FindItem(LVFINDINFO* pFindInfo, int nStart = -1) const;
  24. // Call to sort items using a custom comparison function.
  25. BOOL SortItems(PFNLVCOMPARE pfnCompare, DWORD_PTR dwData);
  26. // Adds an item to the control.
  27. int InsertItem(const LVITEM* pItem);
  28. int InsertItem(int nItem, LPCTSTR lpszItem);
  29. int InsertItem(int nItem, LPCTSTR lpszItem, int nImage);
  30. BOOL EnsureSubItemVisible(int nItem, int nSubItem, CRect *pRect = NULL);
  31. typedef enum Sort
  32. {
  33. None = 0,
  34. Ascending = 1,
  35. Descending = 2,
  36. Auto = 4,
  37. SortBits = 7
  38. }Sort;
  39. typedef enum Comparer
  40. {
  41. NotSet,
  42. Int,
  43. Double,
  44. String,
  45. StringNumber,
  46. StringNoCase,
  47. StringNumberNoCase,
  48. Date
  49. }Comparer;
  50. protected:
  51. static int CALLBACK CompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);
  52. static int Compare(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);
  53. static int CompareInt(LPCSTR pLeftText, LPCSTR pRightText);
  54. static int CompareDouble(LPCSTR pLeftText, LPCSTR pRightText);
  55. static int CompareString(LPCSTR pLeftText, LPCSTR pRightText);
  56. static int CompareNumberString(LPCSTR pLeftText, LPCSTR pRightText);
  57. static int CompareNumberStringNoCase(LPCSTR pLeftText, LPCSTR pRightText);
  58. static int CompareStringNoCase(LPCSTR pLeftText, LPCSTR pRightText);
  59. static int CompareDate(LPCSTR pLeftText, LPCSTR pRightText);
  60. CCriticalSection m_oLock;
  61. protected:
  62. DECLARE_MESSAGE_MAP()
  63. protected:
  64. typedef struct EditorInfo
  65. {
  66. PFNEDITORCALLBACK m_pfnInitEditor;
  67. PFNEDITORCALLBACK m_pfnEndEditor;
  68. CWnd *m_pWnd;
  69. BOOL m_bReadOnly;
  70. EditorInfo();
  71. EditorInfo(PFNEDITORCALLBACK pfnInitEditor, PFNEDITORCALLBACK pfnEndEditor, CWnd *pWnd = NULL);
  72. inline BOOL IsSet(){return (m_pfnInitEditor || m_pWnd);}
  73. }EditorInfo;
  74. typedef struct CellInfo
  75. {
  76. EditorInfo m_eiEditor;
  77. COLORREF m_clrBack;
  78. COLORREF m_clrText;
  79. DWORD_PTR m_dwUserData;
  80. int m_nColumn;
  81. CellInfo(int nColumn);
  82. CellInfo(int nColumn, COLORREF clrBack, COLORREF clrText, DWORD_PTR dwUserData = NULL);
  83. CellInfo(int nColumn, EditorInfo eiEditor, COLORREF clrBack, COLORREF clrText, DWORD_PTR dwUserData = NULL);
  84. CellInfo(int nColumn, EditorInfo eiEditor, DWORD_PTR dwUserData = NULL);
  85. }CellInfo;
  86. typedef struct ColumnInfo
  87. {
  88. EditorInfo m_eiEditor;
  89. int m_nColumn;
  90. COLORREF m_clrBack;
  91. COLORREF m_clrText;
  92. Sort m_eSort;
  93. Comparer m_eCompare;
  94. PFNLVCOMPARE m_fnCompare;
  95. ColumnInfo(int nColumn);
  96. ColumnInfo(int nColumn, PFNEDITORCALLBACK pfnInitEditor, PFNEDITORCALLBACK pfnEndEditor, CWnd *pWnd = NULL);
  97. }ColumnInfo;
  98. typedef struct ItemData
  99. {
  100. EditorInfo m_eiEditor;
  101. CArray<CellInfo*> m_aCellInfo;
  102. COLORREF m_clrBack;
  103. COLORREF m_clrText;
  104. DWORD_PTR m_dwUserData;
  105. ItemData(DWORD_PTR dwUserData = NULL);
  106. ItemData(COLORREF clrBack, COLORREF clrText, DWORD_PTR dwUserData = NULL);
  107. inline BOOL IsSet()
  108. {
  109. return (m_dwUserData || m_clrBack != -1 || m_clrText != -1 || !m_aCellInfo.IsEmpty());
  110. }
  111. virtual ~ItemData();
  112. }ItemData;
  113. CPtrArray m_aItemData;
  114. CPtrArray m_aColumnInfo;
  115. BOOL DeleteItemData(int nItem);
  116. BOOL DeleteAllItemsData( );
  117. BOOL DeleteAllColumnInfo( );
  118. BOOL DeleteColumnInfo(int nIndex );
  119. EditorInfo *m_pEditor;
  120. EditorInfo m_eiDefEditor;
  121. int m_nEditingRow;
  122. int m_nEditingColumn;
  123. int m_nRow;
  124. int m_nColumn;
  125. COLORREF m_clrDefBack;
  126. COLORREF m_clrDefText;
  127. CMsgHook m_msgHook;
  128. BOOL m_bHandleDelete;
  129. int m_nSortColumn;
  130. PFNLVCOMPARE m_fnCompare;
  131. DWORD_PTR m_dwSortData;
  132. public:
  133. ///////////////////////////////////////////////////////////////////////////
  134. //Editor stuff
  135. ///////////////////////////////////////////////////////////////////////////
  136. void SetColumnEditor(int nColumn, PFNEDITORCALLBACK pfnInitEditor, PFNEDITORCALLBACK m_pfnEndEditor = NULL,  CWnd* pWnd = NULL);
  137. void SetColumnEditor(int nColumn, CWnd* pWnd);
  138. void SetCellEditor(int nRow, int nColumn, PFNEDITORCALLBACK pfnInitEditor, PFNEDITORCALLBACK m_pfnEndEditor = NULL,  CWnd* pWnd = NULL);
  139. void SetCellEditor(int nRow, int nColumn, CWnd* pWnd);
  140. void SetRowEditor(int nRow, PFNEDITORCALLBACK pfnInitEditor, PFNEDITORCALLBACK m_pfnEndEditor = NULL,  CWnd* pWnd = NULL);
  141. void SetRowEditor(int nRow, CWnd* pWnd);
  142. void SetDefaultEditor(PFNEDITORCALLBACK pfnInitEditor, PFNEDITORCALLBACK m_pfnEndEditor = NULL,  CWnd* pWnd = NULL);
  143. void SetDefaultEditor(CWnd* pWnd);
  144. void SetColumnReadOnly(int nColumn, bool bReadOnly = true);
  145. void SetCellReadOnly(int nRow, int nColumn, bool bReadOnly = true);
  146. void SetRowReadOnly(int nRow, bool bReadOnly = true);
  147. BOOL IsColumnReadOnly(int nColumn);
  148. BOOL IsRowReadOnly(int nRow);
  149. BOOL IsCellReadOnly(int nRow, int nColumn);
  150. BOOL DisplayEditor(int nItem, int nSubItem);
  151. void HideEditor(BOOL bUpdate = TRUE);
  152. int GetColumnCount(void);
  153. ///////////////////////////////////////////////////////////////////////////
  154. //Display stuff
  155. ///////////////////////////////////////////////////////////////////////////
  156. void SetRowColors(int nItem, COLORREF clrBk, COLORREF clrText);
  157. void SetColumnColors(int nColumn, COLORREF clrBack, COLORREF clrText);
  158. void SetCellColors(int nRow, int nColumn, COLORREF clrBack, COLORREF clrText);
  159. ///////////////////////////////////////////////////////////////////////////
  160. //Item stuff
  161. ///////////////////////////////////////////////////////////////////////////
  162. BOOL AddItem(int ItemIndex, int SubItemIndex, LPCTSTR ItemText, int ImageIndex=-1);
  163. void DeleteSelectedItems(void);
  164. void HandleDeleteKey(BOOL bHandle = TRUE);
  165. void SelectItem(int nItem, BOOL bSelect);
  166. BOOL DeleteAllColumns(void);
  167. BOOL Reset(void);
  168. ///////////////////////////////////////////////////////////////////////////
  169. //Sorting stuff
  170. ///////////////////////////////////////////////////////////////////////////
  171. void SetColumnSorting(int nColumn, Sort eSort, Comparer eSortType = String);
  172. void SetColumnSorting(int nColumn, Sort eSort, PFNLVCOMPARE fnCallBack);
  173. BOOL SortOnColumn(int nColumn, BOOL bChangeOrder = FALSE);
  174. protected:
  175. afx_msg BOOL OnNMDblclk(NMHDR *pNMHDR, LRESULT *pResult);
  176. virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
  177. virtual void PreSubclassWindow();
  178. virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
  179. virtual BOOL PreTranslateMessage(MSG* pMsg);
  180. afx_msg BOOL OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);
  181. afx_msg void OnHdnItemclick(NMHDR *pNMHDR, LRESULT *pResult);
  182. int GetItemIndexFromData(DWORD_PTR dwData);
  183. CListCtrlEx::ColumnInfo* GetColumnInfo(int nColumn);
  184. CellInfo* GetCellInfo(int nItem, int nSubItem);
  185. BOOL SetCellData(int nItem, int nSubItem, DWORD_PTR dwData);
  186. DWORD_PTR GetCellData(int nRow, int nColumn);
  187. virtual BOOL OnAddNew(void){return FALSE;}
  188. BOOL m_bInvokeAddNew;
  189. public:
  190. HACCEL m_hAccel;
  191. };