PropertyGrid.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #pragma once
  2. // PG_PROPERTYSORT is an enumeration used by CPropertyGrid for determining of grouping style
  3. //
  4. enum PG_PROPERTYSORT
  5. {
  6. // to group items by category.
  7. pgsortCategorized,
  8. // to sort items alphabetical.
  9. pgsortAlphabetical,
  10. // disable sorting
  11. pgsortNoSort
  12. };
  13. //////////////////////////////////////////////////////////////////////
  14. // CPropertyGridView is a CListBox derived class.
  15. // It is an internal class used by Property Grid control
  16. //
  17. class CPropertyGridView : public CListBox
  18. {
  19. DECLARE_DYNAMIC(CPropertyGridView)
  20. public:
  21. CPropertyGridView();
  22. virtual ~CPropertyGridView();
  23. int GetDividerPos();
  24. CPropertyGridItem* ItemFromPoint(CPoint point);
  25. private:
  26. CPropertyGridItem* GetSelectedItem();
  27. CPropertyGridItem* AddCategory(CString strCaption);
  28. void SetPropertySort(PG_PROPERTYSORT sort, BOOL bRrefresh = FALSE);
  29. int InsertItem(CPropertyGridItem* pItem, int nIndex);
  30. int _DoExpand(CPropertyGridItem* pItem, int nIndex);
  31. void _DoCollapse(CPropertyGridItem* pItem);
  32. void _RefreshIndexes();
  33. void SwitchExpandState(int nItem);
  34. CPropertyGridItem* GetItem(int nIndex);
  35. int HitTest(CPoint point);
  36. void ResetContent();
  37. void ShowToolTip(CPoint pt);
  38. void _ShowToolTip(CRect rcBound, CRect rcText);
  39. CSize _GetTextExtent(CString str);
  40. void RelayToolTipEvent(MSG* pMsg);
  41. public:
  42. CPGListItems m_lstCategories;
  43. PG_PROPERTYSORT m_properetySort;
  44. COLORREF m_clrLine;
  45. COLORREF m_clrFore;
  46. COLORREF m_clrBack;
  47. double m_dDivider;
  48. CFont m_fontNormal;
  49. CFont m_fontBold;
  50. BOOL m_bTracking;
  51. CPropertyGridItem* m_pSelected;
  52. CToolTipCtrl m_wndTip;
  53. CString m_strTipText;
  54. CRect m_rcToolTip;
  55. CPropertyGridInplaceEdit m_wndEdit;
  56. CPropertyGridInplaceButton m_wndButton;
  57. CPropertyGridInplaceList m_wndListBox;
  58. protected:
  59. //{{AFX_VIRTUAL(CPropertyGridView)
  60. virtual void PreSubclassWindow();
  61. virtual void DrawItem(LPDRAWITEMSTRUCT);
  62. virtual void MeasureItem(LPMEASUREITEMSTRUCT) {}
  63. virtual int CompareItem(LPCOMPAREITEMSTRUCT) { return 0;}
  64. virtual BOOL PreTranslateMessage(MSG* pMsg);
  65. //}}AFX_VIRTUAL
  66. //{{AFX_MSG(CPropertyGridView)
  67. afx_msg BOOL OnEraseBkgnd(CDC* pDC);
  68. afx_msg void OnPaint();
  69. afx_msg void OnNcPaint();
  70. afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
  71. afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
  72. afx_msg void OnSelectionChanged();
  73. afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
  74. afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
  75. afx_msg void OnCaptureChanged(CWnd *pWnd);
  76. afx_msg void OnMouseMove(UINT nFlags, CPoint point);
  77. afx_msg void OnSize(UINT nType, int cx, int cy);
  78. afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
  79. afx_msg BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt);
  80. //}}AFX_MSG
  81. DECLARE_MESSAGE_MAP()
  82. friend class CPropertyGridItem;
  83. friend class CPropertyGrid;
  84. friend class CPropertyGridInplaceEdit;
  85. };
  86. //////////////////////////////////////////////////////////////////////
  87. // CPGToolBar从CToolBar派生,用作属性表的内置工具条
  88. //
  89. class CPGToolBar : public CToolBar
  90. {
  91. public:
  92. virtual void OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHndler) {}
  93. };
  94. //////////////////////////////////////////////////////////////////////
  95. // CPropertyGrid is a CWnd derived class. It is used to create a property grid
  96. // control similar to the property window in VS.NET.
  97. //
  98. // See the "PropertyGrid" demo for a example of usage.
  99. //
  100. class CPropertyGrid : public CWnd
  101. {
  102. DECLARE_DYNAMIC(CPropertyGrid)
  103. public:
  104. // Constructs a CPropertyGrid object.
  105. //
  106. CPropertyGrid();
  107. // Destroys a CPropertyGrid object, handles cleanup and de-allocation.
  108. //
  109. virtual ~CPropertyGrid();
  110. // Call this member function to create the Property Grid control. Returns
  111. // TRUE if successful, otherwise returns FALSE.
  112. //
  113. virtual BOOL Create(
  114. // Size of the control.
  115. const RECT& rect,
  116. // Parent of the control.
  117. CWnd* pParentWnd,
  118. // Identifier of the Property Grid control.
  119. UINT nID);
  120. public:
  121. // Call this member function to add new category to the Property Grid control.
  122. // Returns the pointer to the item object of the newly inserted category.
  123. //
  124. CPropertyGridItem* AddCategory(
  125. // Name of the category to add.
  126. CString strCaption);
  127. // 该函数用于设置属性项的排序方式
  128. //
  129. void SetPropertySort (
  130. // 新的排序方式,只能是下列中的一个:
  131. //
  132. // pgsortCategorized - 按分类顺序
  133. // pgsortAlphabetical - 按字母顺序
  134. // pgsortNoSort - 不排序
  135. //
  136. PG_PROPERTYSORT sort);
  137. // Call this member function to get the current grouping style for the Property Grid control.
  138. // Returns a PG_PROPERTYSORT value representing the current grouping style.
  139. //
  140. PG_PROPERTYSORT GetPropertySort();
  141. // Call this member function to show or hide toolbar control in Property Grid window.
  142. //
  143. void ShowToolBar(
  144. // TRUE to show toolbar control, or FALSE to hide it.
  145. BOOL bShow = TRUE);
  146. // Call this member function to show or hide description pane in Property Grid window.
  147. //
  148. void ShowHelp(
  149. // TRUE to show description pane, or FALSE to hide it.
  150. BOOL bShow = TRUE);
  151. // Call this member function to set custom colors of Property Grid control.
  152. //
  153. void SetCustomColors(
  154. // An RGB value that represents the background color of description pane.
  155. COLORREF clrHelpBack,
  156. // An RGB value that represents the text color of description pane.
  157. COLORREF clrHelpFore,
  158. // An RGB value that represents the color of separating lines in control.
  159. COLORREF clrViewLine,
  160. // An RGB value that represents the background color of Property Grid control.
  161. COLORREF clrViewBack,
  162. // An RGB value that represents the text color of Property Grid control.
  163. COLORREF clrViewFore );
  164. // Call this member function to set default colors of Property Grid control.
  165. //
  166. void SetStandardColors();
  167. // Call this member function to find item by caption.
  168. // Returns the pointer to the Item object.
  169. //
  170. CPropertyGridItem* FindItem(
  171. // Caption for the item to find
  172. CString strCaption);
  173. // Call this member function to find item by identifier.
  174. // Returns the pointer to the Item object.
  175. //
  176. CPropertyGridItem* FindItem(
  177. // Identifier for the item to find
  178. UINT nID);
  179. // Removes all items from the list box of a property grid control.
  180. //
  181. void ResetContent();
  182. // Call this member function to determine if the help pane is visible. Returns
  183. // TRUE if the help pane is visible, otherwise returns FALSE.
  184. //
  185. BOOL IsHelpVisible() const;
  186. // Call this member function to determine if the toolbar is visible. Returns
  187. // TRUE if the toolbar is visible, otherwise returns FALSE.
  188. //
  189. BOOL IsBarVisible() const;
  190. // Call this member function to return the height of the help pane when visible.
  191. // Returns an integer value that represents the height in pixels.
  192. //
  193. int GetHelpHeight() const;
  194. // Call this member function to return the background color for the property grid.
  195. // Returns an RGB value.
  196. //
  197. COLORREF GetHelpBackColor();
  198. // Call this member function to return the foreground color for the property grid.
  199. // Returns an RGB value.
  200. //
  201. COLORREF GetHelpForeColor();
  202. CPropertyGridView& GetGridView();
  203. CToolBar& GetToolBar();
  204. // 展开所有的属性项分类
  205. void ExpandAllCategories();
  206. // 折叠所有的属性项分类
  207. void CollapseAllCategories();
  208. private:
  209. void OnInvertTracker(CRect rc);
  210. int HitTest(CPoint pt);
  211. void OnSelectionChanged(CPropertyGridItem* pItem);
  212. void OnSortChanged();
  213. void Reposition(int cx, int cy);
  214. void CreateToolbar();
  215. void RefreshToolBarButtons();
  216. private:
  217. CPropertyGridView m_wndView;
  218. BOOL m_bHelpVisible;
  219. BOOL m_bToolBarVisible;
  220. int m_nHelpHeight;
  221. COLORREF m_clrHelpBack;
  222. COLORREF m_clrHelpFore;
  223. CRect m_rectTracker;
  224. CPGToolBar m_wndToolbar;
  225. protected:
  226. //{{AFX_VIRTUAL(CPropertyGrid)
  227. //}}AFX_VIRTUAL
  228. //{{AFX_MSG(CPropertyGrid)
  229. afx_msg BOOL OnEraseBkgnd(CDC* pDC);
  230. afx_msg void OnPaint();
  231. afx_msg void OnSetFocus(CWnd* pOldWnd);
  232. afx_msg void OnSize(UINT nType, int cx, int cy);
  233. afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
  234. afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
  235. afx_msg void OnSortAlphabetic();
  236. afx_msg void OnSortCategorized();
  237. afx_msg BOOL OnToolTipNotify( UINT id, NMHDR * pNMHDR, LRESULT * pResult );
  238. afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
  239. //}}AFX_MSG
  240. DECLARE_MESSAGE_MAP()
  241. friend class CPropertyGridView;
  242. };
  243. //////////////////////////////////////////////////////////////////////
  244. AFX_INLINE CPropertyGridItem* CPropertyGrid::FindItem(CString strCaption) {
  245. return m_wndView.m_lstCategories.FindItem(strCaption);
  246. }
  247. AFX_INLINE CPropertyGridItem* CPropertyGrid::FindItem(UINT nID) {
  248. return m_wndView.m_lstCategories.FindItem(nID);
  249. }
  250. AFX_INLINE CPropertyGridItem* CPropertyGrid::AddCategory(CString strCaption) {
  251. return m_wndView.AddCategory(strCaption);
  252. }
  253. AFX_INLINE void CPropertyGrid::SetPropertySort (PG_PROPERTYSORT sort) {
  254. m_wndView.SetPropertySort(sort);
  255. }
  256. AFX_INLINE PG_PROPERTYSORT CPropertyGrid::GetPropertySort () {
  257. return m_wndView.m_properetySort;
  258. }
  259. AFX_INLINE BOOL CPropertyGrid::IsHelpVisible() const {
  260. return m_bHelpVisible;
  261. }
  262. AFX_INLINE BOOL CPropertyGrid::IsBarVisible() const {
  263. return m_bToolBarVisible;
  264. }
  265. AFX_INLINE int CPropertyGrid::GetHelpHeight() const {
  266. return m_nHelpHeight;
  267. }
  268. AFX_INLINE COLORREF CPropertyGrid::GetHelpBackColor() {
  269. return m_clrHelpBack;
  270. }
  271. AFX_INLINE COLORREF CPropertyGrid::GetHelpForeColor() {
  272. return m_clrHelpFore;
  273. }
  274. AFX_INLINE CPropertyGridView& CPropertyGrid::GetGridView() {
  275. return m_wndView;
  276. }
  277. AFX_INLINE CToolBar& CPropertyGrid::GetToolBar() {
  278. return m_wndToolbar;
  279. }