MenuXP.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // CustMenu.h: interface for the CMenuXP class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #if !defined(AFX_MENUXP_H__8FC060B7_2454_11D5_99BD_5254AB339987__INCLUDED_)
  5. #define AFX_CUSTMENU_H__8FC060B7_2454_11D5_99BD_5254AB339987__INCLUDED_
  6. #if _MSC_VER > 1000
  7. #pragma once
  8. #endif // _MSC_VER > 1000
  9. ////////////////////////////////////////////////////////////////////////////////////////////
  10. // CMenuXP is a class derived from CMenu, using ownerdraw technology.
  11. // I named it MenuXP because I once expected it to be like the menus
  12. // in OfficeXP and WindowsXP, but I failed to accomplish it. The big
  13. // difficulty is I cannot convert the 3D border of the menu into flat.
  14. // So I giveup. And I hope it still usefull to you.
  15. //
  16. // I construt the class from scribble, but some of the drawing code is copy from the class
  17. // CCoolMenuManager. I also use a class named CBCGKeyHelper from BCGControlBar to show
  18. // accelerator key text.
  19. //
  20. // Usage:
  21. // Seee Example
  22. //
  23. // Features:
  24. // 1. Menu with icons, like in office 97
  25. // 2. A sidebar in any level of the popup menu
  26. // 3. Support button style menuitem, it appear in some drawing toolbars of office suite
  27. // 4. All colors and font and size can be customized
  28. //
  29. // Use it as you like. bug report and improvement is welcome.
  30. //
  31. // Created: 10/24/2001
  32. // Author: Yao Zhifeng yzf_nuaa@sina.com
  33. //
  34. ////////////////////////////////////////////////////////////////////////////////////////////
  35. //The ownerdraw data
  36. class CMenuXPItem
  37. {
  38. public:
  39. DWORD m_dwMagicNum; //A magic number to distingush our data
  40. DWORD m_dwID; //Menu ID
  41. bool m_bSeparator; //Separator
  42. bool m_bSideBar; //A gradient sidebar
  43. bool m_bButtonOnly; //Button only style item
  44. CString m_strText; //Menu item text
  45. HICON m_hIcon; //Menu icon
  46. int m_nSize; //Height of the item(Width of the sidebar if m_bSideBar is true)
  47. public:
  48. CMenuXPItem()
  49. {
  50. m_dwMagicNum = 0x0505a0a0;
  51. m_dwID = 0;
  52. m_bSeparator = false;
  53. m_bSideBar = false;
  54. m_bButtonOnly = false;
  55. m_hIcon = NULL;
  56. m_nSize = 16;
  57. };
  58. virtual ~CMenuXPItem()
  59. {
  60. if (m_hIcon)
  61. ::DestroyIcon(m_hIcon);
  62. }
  63. BOOL IsMyData(void) { return m_dwMagicNum == 0x0505a0a0; };
  64. };
  65. ///////////////////////////////////////////////////////////////////////////////////////////////
  66. // For convenient, derive some class from CMenuXPItem,
  67. // and do the initialization in constructor
  68. class CMenuXPText : public CMenuXPItem //Normal item with text and an optional icon
  69. {
  70. public:
  71. CMenuXPText(DWORD dwID, LPCTSTR strText, HICON hIcon = NULL) : CMenuXPItem()
  72. {
  73. m_dwID = dwID;
  74. m_strText = strText;
  75. m_hIcon = hIcon;
  76. }
  77. };
  78. class CMenuXPSeparator : public CMenuXPItem //A separator item
  79. {
  80. public:
  81. CMenuXPSeparator() : CMenuXPItem()
  82. {
  83. m_bSeparator = true;
  84. }
  85. };
  86. class CMenuXPSideBar : public CMenuXPItem //A gradient sidebar
  87. {
  88. public:
  89. CMenuXPSideBar(int nSize = 32, LPCTSTR strText = NULL, HICON hIcon=NULL, DWORD dwID=0) : CMenuXPItem()
  90. {
  91. m_dwID = dwID;
  92. m_bSideBar = true;
  93. m_strText = strText;
  94. m_hIcon = hIcon;
  95. m_nSize = nSize;
  96. m_dwID = dwID;
  97. }
  98. };
  99. class CMenuXPButton : public CMenuXPItem //A button only item
  100. {
  101. public:
  102. CMenuXPButton(DWORD dwID, HICON hIcon = NULL) : CMenuXPItem()
  103. {
  104. m_dwID = dwID;
  105. m_bButtonOnly = true;
  106. m_hIcon = hIcon;
  107. }
  108. };
  109. ////////////////////////////////////////////////////////////////////////////////////////
  110. // Class CMenuXP, an ownerdraw menu
  111. class CMenuXP : public CMenu
  112. {
  113. DECLARE_DYNAMIC(CMenuXP)
  114. CPen m_pen;
  115. public:
  116. int CYBUTTONMARGIN;
  117. CMenuXP();
  118. virtual ~CMenuXP();
  119. //Menu style(Default: STYLE_OFFICE)
  120. typedef enum
  121. {
  122. STYLE_OFFICE, //Draw a float button around the icon
  123. STYLE_STARTMENU, //show selected bar below the icon
  124. STYLE_XP //use different color for the icon area
  125. } MENUSTYLE;
  126. //Below is the functions to build the menu
  127. BOOL AddSideBar(CMenuXPSideBar *pItem);
  128. BOOL AppendODMenu2(UINT nFlags, CMenuXPItem *pItem, ACCEL *pAccel=0);
  129. BOOL AppendSeparator(void);
  130. BOOL AppendODPopup(UINT nFlags, CMenuXP *pPopup, CMenuXPItem *pItem);
  131. void Break(void); //change a column(the next item added will be in a new column)
  132. void BreakBar(void); //change a column with a break line(same as Break, except that a break line is drawn between two columns)
  133. protected:
  134. CFont m_fontMenu;
  135. COLORREF m_clrBackGround; //Background color
  136. COLORREF m_clrSelectedBar; //selected bar color
  137. COLORREF m_clrText; //Text color
  138. COLORREF m_clrSelectedText; //selected text color
  139. COLORREF m_clrDisabledText; //disabled text color
  140. COLORREF m_clrSideBarStart; //Start color of the gradient sidebar
  141. COLORREF m_clrSideBarEnd; //end color of the gradient sidebar
  142. COLORREF m_clrIconArea; //Background color of the button(icon) area
  143. BOOL m_bBreak; //if true, next item inserted into the menu will be added with the sytle MF_MENUBREAK
  144. BOOL m_bBreakBar; //if true, next item inserted into the menu will be added with the sytle MF_MENUBARBREAK
  145. MENUSTYLE m_Style; //menu style(currently support office or startmenu style)
  146. public: //User these functions to change the default attribute of the menu
  147. void SetBackColor(COLORREF clr) { m_clrBackGround = clr; }
  148. void SetSelectedBarColor(COLORREF clr) { m_clrSelectedBar = clr; }
  149. void SetTextColor(COLORREF clr) { m_clrText = clr; }
  150. void SetSelectedTextColor(COLORREF clr) { m_clrSelectedText = clr; }
  151. void SetDisabledTextColor(COLORREF clr) { m_clrDisabledText = clr; }
  152. void SetSideBarStartColor(COLORREF clr) { m_clrSideBarStart = clr; }
  153. void SetSideBarEndColor(COLORREF clr) { m_clrSideBarEnd = clr; }
  154. void SetIconAreaColor(COLORREF clr) { m_clrIconArea = clr; }
  155. void SetMenuStyle(MENUSTYLE style) { m_Style = style; }
  156. BOOL SetMenuFont(LOGFONT lgfont);
  157. //Find the popupmenu from a menuitem id, you may not need it
  158. CMenuXP *FindSubMenuFromID(DWORD dwID);
  159. public:
  160. virtual void DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct );
  161. virtual void MeasureItem( LPMEASUREITEMSTRUCT lpMeasureItemStruct );
  162. static LRESULT OnMenuChar(UINT nChar, UINT nFlags, CMenu* pMenu);
  163. protected:
  164. virtual void DrawBackGround(CDC *pDC, CRect rect, BOOL bSelected, BOOL bDisabled);
  165. virtual void DrawButton(CDC *pDC, CRect rect, BOOL bSelected, BOOL bDisabled, BOOL bChecked);
  166. virtual void DrawIcon(CDC *pDC, CRect rect, HICON hIcon, BOOL bSelected, BOOL bDisabled);
  167. virtual void DrawSideBar(CDC *pDC, CRect rect, HICON hIcon, CString strText);
  168. virtual void DrawText(CDC *pDC, CRect rect, CString strText, BOOL bSelected, BOOL bDisabled, BOOL bBold);
  169. virtual void DrawCheckMark(CDC *pDC, CRect rect, BOOL bSelected);
  170. virtual void DrawMenuText(CDC& dc, CRect rc, CString text, COLORREF color);
  171. virtual void DrawIconArea(CDC *pDC, CRect rect, BOOL bSelected, BOOL bDisabled, BOOL bChecked);
  172. void Clear(void); //Clean all memory and handles
  173. //helpers
  174. HBITMAP CreateGradientBMP(HDC hDC,COLORREF cl1,COLORREF cl2,int nWidth,int nHeight,int nDir,int nNumColors);
  175. void DrawEmbossed(CDC *pDC, HICON hIcon, CRect rect, BOOL bColor = FALSE);
  176. void FillRect(CDC *pDC, const CRect& rc, COLORREF color);
  177. };
  178. #endif // !defined(AFX_MENUXP_H__8FC060B7_2454_11D5_99BD_5254AB339987__INCLUDED_)