Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cWndMenu.h
Go to the documentation of this file.
1 //
4 //
5 #ifndef _INC_cWndMenu_H
6 #define _INC_cWndMenu_H
7 #ifndef NO_PRAGMA_ONCE
8 #pragma once
9 #endif
10 
11 #include "../WinAPI/WinTypes.h"
12 
13 #ifdef _WIN32
14 namespace GrayLib
15 {
16  class cWndMenuChild // GRAYLIB_LINK
17  {
21  public:
22  HMENU m_hMenu;
23 
24  public:
25  cWndMenuChild(HMENU hMenu = WINHANDLE_NULL)
26  : m_hMenu(hMenu)
27  {
28  }
29  cWndMenuChild(HMENU hMenu, int nPos)
30  : m_hMenu(::GetSubMenu(hMenu, nPos))
31  {
32  // Find a child menu. MF_POPUP
33  }
34 
35  bool isValidHandle() const
36  {
37  return(m_hMenu != WINHANDLE_NULL);
38  }
39  operator HMENU() const
40  {
41  return m_hMenu;
42  }
43 
44  int GetMenuItemCount() const
45  {
46  return ::GetMenuItemCount(m_hMenu);
47  }
48  HMENU GetSubMenuByPos(int nPos=0) const
49  {
50  // Get child menu by its position. MF_POPUP
51  // Child parents dont have command id's in windows. This seems weird but its true.
52  return ::GetSubMenu(m_hMenu, nPos);
53  }
54  UINT GetMenuItemID(int nPos) const
55  {
56  return ::GetMenuItemID(m_hMenu, nPos);
57  }
58  bool EnableMenuItem(UINT nPos, UINT uCmd)
59  {
61  return ::EnableMenuItem(m_hMenu, nPos, uCmd);
62  }
63  bool CheckMenuItem(UINT nPos, UINT uCmd)
64  {
66  return ::CheckMenuItem(m_hMenu, nPos, uCmd);
67  }
68  bool EnableMenuCmd(UINT nCmd, bool bActive)
69  {
70  return EnableMenuItem(nCmd, MF_BYCOMMAND | ((bActive) ? (MF_ENABLED) : (MF_GREYED | MF_DISABLED)));
71  }
72  bool CheckMenuCmd(UINT nCmd, bool bCheck)
73  {
74  return CheckMenuItem(nCmd, MF_BYCOMMAND | ((bCheck) ? (MF_CHECKED) : (MF_UNCHECKED)));
75  }
76 
77  void SetMenuCmdText(UINT nCmd, const GChar_t* pszText)
78  {
80  _GTN(::ModifyMenu)(m_hMenu, nCmd, MF_BYCOMMAND, nCmd, pszText);
81  }
82 
83  bool RemoveMenuChild(UINT uPosition, UINT uFlags = MF_BYPOSITION)
84  {
86  return ::RemoveMenu(m_hMenu, uPosition, uFlags);
87  }
88  bool InsertMenuChild(UINT uPosition, UINT uFlags, UINT uIDNewItem, const GChar_t* pszText)
89  {
90  // MF_POPUP
91  // uFlags = MF_BYPOSITION
92  return _GTN(::InsertMenu)( m_hMenu, uPosition, uFlags, uIDNewItem, pszText);
93  }
94 
95  void DestroyMenu()
96  {
97  // Manually destroy this.
98  if (m_hMenu != WINHANDLE_NULL)
99  {
100  ::DestroyMenu(m_hMenu);
101  m_hMenu = WINHANDLE_NULL;
102  }
103  }
104 
105  int TrackPopupMenu(UINT uFlags, int x, int y, HWND hWndParent)
106  {
110  return ::TrackPopupMenu(m_hMenu, uFlags, x, y, 0, hWndParent, nullptr);
111  }
112  };
113 
114  class cWndMenu : public cWndMenuChild // GRAYLIB_LINK
115  {
119  public:
120  cWndMenu(HMENU hMenu)
121  : cWndMenuChild(hMenu)
122  {
123  }
124  ~cWndMenu()
125  {
126  if (m_hMenu != WINHANDLE_NULL)
127  {
128  ::DestroyMenu(m_hMenu);
129  }
130  }
131  void AttachMenu(HMENU hMenu)
132  {
133  if (m_hMenu != hMenu)
134  {
135  DestroyMenu();
136  m_hMenu = hMenu;
137  }
138  }
139  };
140 
141 #ifndef _MFC_VER
142  typedef cWndMenuChild CMenu;
143 #endif
144 
145 };
146 #endif
147 #endif
#define _GTN(c)
_WIN32 name has a A or W for UTF8 or UNICODE (like _FNF)
Definition: StrConst.h:28
#define MF_GREYED
alternate for spelling the 'A' version.
Definition: WinTypes.h:123
#define WINHANDLE_NULL
HWND, HPEN, etc are NOT OS Handles. like HWND_DESKTOP. like HANDLEPTR_NULL. This is a WINAPI void* ha...
Definition: cOSHandle.h:23
Definition: cMesh.h:22
char GChar_t
My version of TCHAR, _TCHAR.
Definition: StrConst.h:26