GUIFramework 1.1.0
Framework for desktop GUI applications in C++.
Loading...
Searching...
No Matches
ImageButton.cpp
Go to the documentation of this file.
1#include "ImageButton.h"
2
6#include "GUIFramework.h"
7
8#pragma warning(disable: 6001)
9
10using namespace std;
11
12namespace gui_framework
13{
15
16 void ImageButton::drawImage()
17 {
18 int resourceType;
19 HANDLE imageHandle;
20 function<HANDLE()> load;
21
22 if (!pathToImage.empty())
23 {
24 load = [this]()
25 {
26 return LoadImageW(nullptr, pathToImage.wstring().data(), static_cast<uint32_t>(iType), imageWidth, imageHeight, LR_LOADFROMFILE);
27 };
28 }
29 else if (imageResource)
30 {
31 load = [this]()
32 {
33 return LoadImageW(GUIFramework::get()[resourceModuleName], MAKEINTRESOURCE(imageResource), static_cast<uint32_t>(iType), imageWidth, imageHeight, NULL);
34 };
35 }
36 else
37 {
38 return;
39 }
40
41 switch (iType)
42 {
43 case imageType::icon:
45
46 resourceType = IMAGE_ICON;
47
48 break;
49 case imageType::bitmap:
50 utility::appendStyle(handle, BS_BITMAP);
51
52 resourceType = IMAGE_BITMAP;
53
54 break;
55
56 default:
57 break;
58 }
59
60 imageHandle = load();
61
62 switch (iType)
63 {
64 case imageType::icon:
65 image = static_cast<HICON>(imageHandle);
66
67 break;
68 case imageType::bitmap:
69 image = static_cast<HBITMAP>(imageHandle);
70
71 break;
72
73 default:
74 break;
75 }
76
77 switch (dType)
78 {
80 SendMessageW(handle, BM_SETIMAGE, resourceType, reinterpret_cast<LPARAM>(imageHandle));
81
82 break;
83
85 utility::removeStyle(handle, resourceType == IMAGE_ICON ? BS_ICON : BS_BITMAP);
86
87 break;
88
90 utility::removeStyle(handle, resourceType == IMAGE_ICON ? BS_ICON : BS_BITMAP);
91
92 SendMessageW(handle, BM_SETIMAGE, resourceType, reinterpret_cast<LPARAM>(imageHandle));
93
94 break;
95
96 default:
97 break;
98 }
99 }
100
101 ImageButton::ImageButton(const wstring& buttonName, const filesystem::path& pathToImage, drawingType dType, imageType iType, uint16_t imageWidth, uint16_t imageHeight, const utility::ComponentSettings& settings, BaseComposite* parent, const function<void()>& onClick) :
102 BaseButton
103 (
104 buttonName,
105 L"",
106 settings,
107 styles::DefaultButtonStyles(),
108 parent,
109 onClick
110 ),
111 IResizableComponent
112 (
113 handle,
114 parent->getHandle()
115 ),
116 pathToImage(pathToImage),
117 dType(dType),
118 iType(iType),
119 imageWidth(imageWidth),
120 imageHeight(imageHeight)
121 {
122 this->drawImage();
123 }
124
125 ImageButton::ImageButton(const wstring& buttonName, const filesystem::path& pathToImage, drawingType dType, imageType iType, uint16_t imageWidth, uint16_t imageHeight, const utility::ComponentSettings& settings, BaseComposite* parent, const string& functionName, const string& moduleName) :
126 BaseButton
127 (
128 buttonName,
129 L"",
130 settings,
131 styles::DefaultButtonStyles(),
132 parent,
133 functionName,
134 moduleName
135 ),
136 IResizableComponent
137 (
138 handle,
139 parent->getHandle()
140 ),
141 pathToImage(pathToImage),
142 dType(dType),
143 iType(iType),
144 imageWidth(imageWidth),
145 imageHeight(imageHeight)
146 {
147 this->drawImage();
148 }
149
150 ImageButton::ImageButton(const wstring& buttonName, uint32_t imageResources, drawingType dType, imageType iType, uint16_t imageWidth, uint16_t imageHeight, const utility::ComponentSettings& settings, BaseComposite* parent, const function<void()>& onClick, const string& resourceModuleName) :
151 BaseButton
152 (
153 buttonName,
154 L"",
155 settings,
156 styles::DefaultButtonStyles(),
157 parent,
158 onClick
159 ),
160 IResizableComponent
161 (
162 handle,
163 parent->getHandle()
164 ),
165 imageResource(imageResource),
166 resourceModuleName(resourceModuleName),
167 dType(dType),
168 iType(iType),
169 imageWidth(imageWidth),
170 imageHeight(imageHeight)
171 {
172 this->drawImage();
173 }
174
175 ImageButton::ImageButton(const wstring& buttonName, uint32_t imageResources, drawingType dType, imageType iType, uint16_t imageWidth, uint16_t imageHeight, const utility::ComponentSettings& settings, BaseComposite* parent, const string& functionName, const string& moduleName, const string& resourceModuleName) :
176 BaseButton
177 (
178 buttonName,
179 L"",
180 settings,
181 styles::DefaultButtonStyles(),
182 parent,
183 functionName,
184 moduleName
185 ),
186 IResizableComponent
187 (
188 handle,
189 parent->getHandle()
190 ),
191 imageResource(imageResource),
192 resourceModuleName(resourceModuleName),
193 dType(dType),
194 iType(iType),
195 imageWidth(imageWidth),
196 imageHeight(imageHeight)
197 {
198 this->drawImage();
199 }
200
201 void ImageButton::setImage(const filesystem::path& pathToImage, drawingType dType, imageType iType, uint16_t imageWidth, uint16_t imageHeight)
202 {
203 this->pathToImage = pathToImage;
204 this->dType = dType;
205 this->iType = iType;
206 this->imageWidth = imageWidth;
207 this->imageHeight = imageHeight;
208
209 resourceModuleName.clear();
210 imageResource = NULL;
211
212 this->drawImage();
213 }
214
215 void ImageButton::setImage(uint32_t imageResource, drawingType dType, imageType iType, uint16_t imageWidth, uint16_t imageHeight, const string& resourceModuleName)
216 {
217 this->imageResource = imageResource;
218 this->resourceModuleName = resourceModuleName;
219 this->dType = dType;
220 this->iType = iType;
221 this->imageWidth = imageWidth;
222 this->imageHeight = imageHeight;
223
224 pathToImage.clear();
225
226 this->drawImage();
227 }
228
229 const filesystem::path& ImageButton::getPathToImage() const
230 {
231 return pathToImage;
232 }
233
234 uint16_t ImageButton::getImageWidth() const
235 {
236 return imageWidth;
237 }
238
239 uint16_t ImageButton::getImageHeight() const
240 {
241 return imageHeight;
242 }
243
244 ImageButton::drawingType ImageButton::getDrawingType() const
245 {
246 return dType;
247 }
248
249 ImageButton::imageType ImageButton::getImageType() const
250 {
251 return iType;
252 }
253
254 size_t ImageButton::getHash() const
255 {
256 return typeid(ImageButton).hash_code();
257 }
258
259 json::JSONBuilder ImageButton::getStructure() const
260 {
261 using json::utility::jsonObject;
262
263 json::JSONBuilder builder = BaseButton::getStructure();
264 jsonObject& current = get<jsonObject>(builder[utility::to_string(windowName, ISerializable::getCodepage())]);
265
266 current.setUnsignedInt("imageWidth"s, static_cast<uint64_t>(imageWidth));
267 current.setUnsignedInt("imageHeight"s, static_cast<uint64_t>(imageHeight));
268
269 current.setString("pathToImage", utility::getStringFromRawPath(pathToImage));
270 current.setString("resourceModuleName", resourceModuleName);
271
272 current.setUnsignedInt("drawingType"s, static_cast<uint64_t>(dType));
273 current.setUnsignedInt("imageType"s, static_cast<uint64_t>(iType));
274 current.setUnsignedInt("imageResource", imageResource);
275
276 return builder;
277 }
278
279 ImageButton::~ImageButton()
280 {
281 try
282 {
283 DeleteObject(any_cast<HBITMAP>(image));
284 }
285 catch (const bad_any_cast&)
286 {
287 DeleteObject(any_cast<HICON>(image));
288 }
289 }
290}
static GUIFramework & get()
Singleton instance access.
Button with image.
Definition ImageButton.h:12
ImageButton(const std::wstring &buttonName, const std::filesystem::path &pathToImage, drawingType dType, imageType iType, uint16_t imageWidth, uint16_t imageHeight, const utility::ComponentSettings &settings, BaseComposite *parent, const std::function< void()> &onClick=nullptr)
void removeStyle(HWND handle, LONG_PTR styleToRemove)
Remove WinAPI style.
Definition Utility.cpp:31
void appendStyle(HWND handle, LONG_PTR newStyle)
Append WinAPI style.
Definition Utility.cpp:21
utility::BaseLoadableHolder::imageType imageType