FlatPopupMenu.h
上传用户:szcanmei
上传日期:2007-01-02
资源大小:48k
文件大小:7k
源码类别:

菜单

开发平台:

Visual C++

  1. // A flat popup menu for controls
  2. // Copyright (c) 1999 Andy Brown <andy@mirage.dabsol.co.uk>
  3. // You may do whatever you like with this file, I just don't care.
  4. #ifndef _6911179a_6053_11d3_a4b7_00001c0022de
  5. #define _6911179a_6053_11d3_a4b7_00001c0022de
  6. // flat popup menu class
  7. class CFlatPopupMenu
  8. {
  9. public:
  10. // data members
  11.   enum
  12.   {
  13.     itemSeparator=0x1,                            // not valid with anything else
  14.     itemNotSelectable=0x2,                        // cannot select this item
  15.     itemBold=0x4,                                 // display in bold face text
  16.     itemGrayed=0x8                                // display grayed out
  17.   };
  18.   enum menuColor
  19.   {
  20.     colorBorder,                                  // menu border and separator line
  21.     colorBackground,                              // background fill
  22.     colorText,                                    // normal text
  23.     colorGrayedText,                              // grayed out text
  24.     colorHighlightText,                           // selected text
  25.     colorHighlight,                               // selected background
  26.     colorIconTransparent,                         // color that shows through bitmaps
  27.     colorLightShadow,                             // for edges facing the light source
  28.     colorDarkShadow,                              // for edges facing away from the light source
  29.     numColors
  30.   };
  31. public:
  32. // menu item class
  33.   class CItem
  34.   {
  35.   public:
  36.     int             m_Top;                        // top
  37.     int             m_Height;                     // height
  38.     DWORD           m_dwFlags;                    // various flags
  39.     std::string     m_strName;                    // text name
  40.     UINT            m_ItemID;                     // item ID
  41.     int             m_IconIndex;                  // index into the bitmap
  42.     CFlatPopupMenu *m_pPopup;                     // popup menu
  43.   protected:
  44.     void Assign(const CItem& src);                // copy src to this
  45.   public:
  46.     CItem();                                      // default constructor
  47.     CItem(const CItem& src);                      // copy constructor
  48.     CItem& operator=(const CItem& src);           // assignment operator
  49.   };
  50.   static bool        m_bClassRegistered;          // once-per-app window class registration flag
  51.   HINSTANCE          m_hInstance;                 // bitmaps are loaded from here
  52.   UINT               m_BitmapID;                  // bitmap resource ID
  53.   std::vector<CItem> m_Items;                     // items in this menu
  54.   std::string        m_strFont;                   // font name for items
  55.   int                m_FontSize;                  // font point-size for items
  56.   int                m_PopupDelay;                // popup menu delay
  57.   COLORREF           m_Colors[numColors];         // the color settings
  58. // internal state settings
  59.   enum
  60.   {
  61.     stateInactive,
  62.     stateShow,
  63.     stateTrack
  64.   } m_State;
  65.   int                m_Width;
  66.   int                m_FontHeight;
  67.   int                m_BoldFontHeight;
  68.   HWND               m_hWnd;
  69.   HWND               m_hWndCommand;
  70.   int                m_SelectedItem;
  71.   UINT               m_SelectedID;
  72.   bool               m_bWaitRightButton;
  73.   bool               m_bWaitLeftButton;
  74.   bool               m_bModal;
  75.   bool               m_bChild;
  76.   CFlatPopupMenu    *m_pPrevious;
  77. // GDI objects
  78.   HFONT              m_hFont;
  79.   HFONT              m_hBoldFont;
  80.   HPEN               m_hBorderPen;
  81.   HPEN               m_hLightShadowPen;
  82.   HPEN               m_hDarkShadowPen;
  83.   HPEN               m_hBackPen;
  84.   HPEN               m_hSelectedTextPen;
  85.   HPEN               m_hTextPen;
  86.   HBRUSH             m_hBackBrush;
  87.   HBRUSH             m_hSelectedBrush;
  88.   HBITMAP            m_hBitmap;
  89. private:
  90.   CFlatPopupMenu& operator=(const CFlatPopupMenu& src) { return *this; }    // not allowed
  91. protected:
  92.   bool RegisterClass(void);
  93.   void Cleanup(void);
  94.   void CreateObjects(void);
  95.   bool CheckOutsideMenu(HWND hWnd,const RECT& rect,short x,short y);
  96.   void DrawItem(HWND hWnd,HDC hDC,const int index,const CItem& item);
  97.   void DrawTransparentBitmap(HDC hdc,const int xSrcOffset,const int xStart,const int yStart);
  98.   void DrawSeparator(HDC hDC,int top);
  99.   int GetItem(short x,short y,const RECT& rect);
  100.   CItem *GetItem(const UINT itemid,const bool bByPosition);
  101.   void DestroyAll(void);
  102.   void ButtonDown(HWND hWnd,short x,short y,const bool bLeft);
  103.   void SetReturn(const UINT id);
  104. public:
  105.   CFlatPopupMenu();
  106.   ~CFlatPopupMenu();
  107.   void SetColor(const menuColor id,const COLORREF cr);
  108.   void SetFont(LPCTSTR pszFont);
  109.   void SetFontSize(const int size);
  110.   void SetPopupDelay(const int delay);
  111.   bool Create(HINSTANCE hInstance,const UINT bitmap_id=(UINT)-1);
  112.   bool AppendItem(const DWORD dwFlags,LPCTSTR pszName,const UINT itemid,const int icon=-1);
  113.   bool AppendPopup(const DWORD dwFlags,LPCTSTR pszName,CFlatPopupMenu& popup,const int icon=-1);
  114.   bool SetMenuItemText(const UINT itemid,LPCTSTR pszName,const bool bByPosition=false);
  115.   bool SetMenuItemFlags(const UINT itemid,const DWORD dwFlags,const bool bByPosition=false);
  116.   bool SetMenuItemIcon(const UINT itemid,const int icon,const bool bByPosition=false);
  117.   bool GetString(const UINT itemid,LPTSTR pszText,const UINT cchText,const bool bByPosition=false);
  118.   UINT Track(int x,int y,HWND hWnd,const bool bModal,const bool bPopup=false);
  119.   void OnPaint(HWND hWnd,HDC hDC);
  120.   void OnEraseBkgnd(HWND hWnd,HDC hDC);
  121.   void OnLButtonDown(HWND hWnd,short x,short y);
  122.   void OnRButtonDown(HWND hWnd,short x,short y);
  123.   void OnLButtonUp(HWND hWnd,short x,short y);
  124.   void OnRButtonUp(HWND hWnd,short x,short y);
  125.   void OnDestroy(HWND hWnd);
  126.   void OnMouseMove(HWND hWnd,short x,short y);
  127.   void OnTimer(HWND hWnd,unsigned short id);
  128.   void OnChar(HWND hWnd,TCHAR c);
  129. };
  130. // set the popup menu delay time
  131. inline void CFlatPopupMenu::SetPopupDelay(const int delay)
  132. {
  133.   m_PopupDelay=delay;
  134. }
  135. // Set a color
  136. inline void CFlatPopupMenu::SetColor(const menuColor id,const COLORREF cr)
  137. {
  138.   m_Colors[id]=cr;
  139. }
  140. // Set the font
  141. inline void CFlatPopupMenu::SetFont(LPCTSTR pszFont)
  142. {
  143.   m_strFont=pszFont;
  144. }
  145. // Set the font size
  146. inline void CFlatPopupMenu::SetFontSize(const int size)
  147. {
  148.   m_FontSize=size;
  149. }
  150. // WM_PAINT
  151. inline void CFlatPopupMenu::OnPaint(HWND hWnd,HDC hDC)
  152. {
  153.   std::vector<CItem>::const_iterator it;
  154.   int pos;
  155. // draw each item
  156.   pos=0;
  157.   for(it=m_Items.begin();it!=m_Items.end();it++)
  158.     DrawItem(hWnd,hDC,pos++,*it);
  159. }
  160. // WM_LBUTTONDOWN
  161. inline void CFlatPopupMenu::OnLButtonDown(HWND hWnd,short x,short y)
  162. {
  163.   ButtonDown(hWnd,x,y,true);
  164. }
  165. // WM_RBUTTONDOWN
  166. inline void CFlatPopupMenu::OnRButtonDown(HWND hWnd,short x,short y)
  167. {
  168.   ButtonDown(hWnd,x,y,false);
  169. }
  170. // Destroy all menus
  171. inline void CFlatPopupMenu::DestroyAll(void)
  172. {
  173.   CFlatPopupMenu *pMenu;
  174. // find the root
  175.   for(pMenu=this;pMenu->m_pPrevious;pMenu=pMenu->m_pPrevious);
  176.   DestroyWindow(pMenu->m_hWnd);
  177. }
  178. // CItem constructor
  179. inline CFlatPopupMenu::CItem::CItem()
  180. {
  181. }
  182. // CItem copy constructor
  183. inline CFlatPopupMenu::CItem::CItem(const CItem& src)
  184. {
  185.   Assign(src);
  186. }
  187. // CItem = operator
  188. inline CFlatPopupMenu::CItem& CFlatPopupMenu::CItem::operator=(const CItem& src)
  189. {
  190.   Assign(src);
  191.   return *this;
  192. }
  193. #endif