Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cWinResource.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cWinResource_H
7 #define _INC_cWinResource_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "WinTypes.h"
15 
16 namespace GrayLib
17 {
18  class GRAYLIB_LINK cWinResMod // : public IResourceLoader
19  {
23 
24  typedef cWinResMod THIS_t;
25 
26  protected:
27  HMODULE m_hModule; // usually cAppState::get_HModule() //!< cOSModule but no open/close ref.
28 
29  public:
30  cWinResMod(HMODULE hModule) noexcept
31  : m_hModule(hModule)
32  {
34  }
35 
36 #ifdef _MFC_VER
37  cWinResMod(const GChar_t* pszResourceName, const GChar_t* pResourceType) noexcept
38  : m_hModule(::AfxFindResourceHandle(pszResourceName, pResourceType))
39  {
40  // MFC CWinApp::LoadIcon
41  // pResourceType = ATL_RT_GROUP_CURSOR or ATL_RT_GROUP_ICON
42  }
43 #endif
44 
45  static constexpr const GChar_t* MakeIntResource(RESOURCEID_t id) noexcept
46  {
49  return (const GChar_t*)((UINT_PTR)((WORD)(id)));
50  }
51  static constexpr bool IsIntResource(const GChar_t* pszText) noexcept
52  {
54  return ((((ULONG_PTR)(pszText)) >> 16) == 0);
55  }
56  static constexpr RESOURCEID_t GetIntResource(const GChar_t* pszText) noexcept
57  {
59  return (((UINT_PTR)(pszText)) & 0x0FFFF);
60  }
61 
62  cString LoadStrID(RESOURCEID_t idString) noexcept;
63 
64 #ifdef _WIN32
65  HICON LoadIconX(const GChar_t* pszResourceName) const noexcept
66  {
71  return _GTN(::LoadIcon)(m_hModule, pszResourceName);
72  }
73  HICON LoadIconID(RESOURCEID_t id) const noexcept
74  {
78  return LoadIconX(THIS_t::MakeIntResource(id));
79  }
80  HCURSOR LoadCursorX(const GChar_t* pszResourceName) const noexcept
81  {
84  return _GTN(::LoadCursor)(m_hModule, pszResourceName);
85  }
86  HCURSOR LoadCursorID(RESOURCEID_t id) const noexcept
87  {
88  // RT_CURSOR or RT_GROUP_CURSOR
89  return LoadCursorX(THIS_t::MakeIntResource(id));
90  }
91 
92  HBITMAP LoadBitmapID(RESOURCEID_t id) const noexcept
93  {
94  // RT_BITMAP
95  // NOT part of MFC CWinApp
96  return _GTN(::LoadBitmap)(m_hModule, THIS_t::MakeIntResource(id));
97  }
98  HMENU LoadMenuID(RESOURCEID_t id) const noexcept
99  {
100  // RT_MENU
101  // NOT part of MFC CWinApp
102  return _GTN(::LoadMenu)(m_hModule, THIS_t::MakeIntResource(id));
103  }
104  HACCEL LoadAcceleratorsID(RESOURCEID_t id) const noexcept
105  {
106  // RT_ACCELERATOR
107  return _GTN(::LoadAccelerators)(m_hModule, THIS_t::MakeIntResource(id));
108  }
109 
110 #endif // _WIN32
111 
112  };
113 
114 #ifdef _WIN32
115  class cWinResource : public cWinResMod
116  {
120 
121  public:
122  HRSRC m_hFind;
123  HGLOBAL m_hData;
124  LPVOID m_pData;
125 
126  public:
127  cWinResource(HMODULE hModule = HMODULE_NULL) noexcept
128  : cWinResMod(hModule)
129  , m_hFind(HANDLE_NULL)
130  , m_hData(HANDLE_NULL)
131  , m_pData(nullptr)
132  {
133  // hModule = HMODULE_NULL = the current module.
134  }
135 
136  ~cWinResource() noexcept
137  {
138  Close();
139  }
140  bool FindRes(const GChar_t* pszResourceName, const void* pResType = RT_STRING) noexcept
141  {
146  if (pszResourceName == nullptr)
147  return false;
148  m_hFind = _GTN(::FindResource)(m_hModule, pszResourceName, (const GChar_t*)pResType);
149  if (m_hFind == HANDLE_NULL)
150  return false;
151  return true;
152  }
153 
154  bool LoadLock() noexcept
155  {
157  m_hData = ::LoadResource(m_hModule, m_hFind);
158  if (m_hData == HANDLE_NULL)
159  return false;
160  m_pData = ::LockResource(m_hData);
161  if (m_pData == nullptr)
162  return false;
163  return true;
164  }
165  bool FindLoadLock(const GChar_t* pszResourceName, const void* pResType = RT_RCDATA) noexcept
166  {
170  if (!FindRes(pszResourceName, pResType))
171  return false;
172  return LoadLock();
173  }
174  size_t SizeofResource() const noexcept
175  {
177  return ::SizeofResource(m_hModule, m_hFind);
178  }
179  void Close() noexcept
180  {
181  if (m_hData != HANDLE_NULL)
182  {
183  if (m_pData != nullptr)
184  {
185 #if 0 // ndef UNDER_CE
186  /*::*/UnlockResource(m_hData); // no need to call this?
187 #endif
188  m_pData = nullptr;
189  }
190 #ifndef UNDER_CE
191  ::FreeResource(m_hData);
192 #endif
193  m_hData = HANDLE_NULL;
194  }
195  }
196  };
197 #endif // _WIN32
198 
199 }
200 
201 #endif // _INC_cWinResource_H
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
#define _GTN(c)
_WIN32 name has a A or W for UTF8 or UNICODE (like _FNF)
Definition: StrConst.h:28
#define HANDLE_NULL
Invalid OS handle for _WIN32. Not invalid OS handle for linux.
Definition: cOSHandle.h:21
#define HMODULE_NULL
Definition: cOSModule.h:31
Definition: cWinResource.h:19
HMODULE m_hModule
Definition: cWinResource.h:27
static constexpr RESOURCEID_t GetIntResource(const GChar_t *pszText) noexcept
Definition: cWinResource.h:56
cWinResMod(HMODULE hModule) noexcept
Definition: cWinResource.h:30
static constexpr bool IsIntResource(const GChar_t *pszText) noexcept
Definition: cWinResource.h:51
static constexpr const GChar_t * MakeIntResource(RESOURCEID_t id) noexcept
Definition: cWinResource.h:45
Definition: cMesh.h:22
WORD RESOURCEID_t
A resource in .RC attached to a file/module. like MAKEINTRESOURCE(). https://docs....
Definition: cResourceBase.h:19
char GChar_t
My version of TCHAR, _TCHAR.
Definition: StrConst.h:26