-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPictureCtrl.cpp
More file actions
316 lines (274 loc) · 6.68 KB
/
PictureCtrl.cpp
File metadata and controls
316 lines (274 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////
// PictureCtrl.cpp
//
// Author: Tobias Eiseler
//
// E-Mail: tobias.eiseler@sisternicky.com
//
// Function: A MFC Picture Control to display
// an image on a Dialog, etc.
///////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "PictureCtrl.h"
#include <GdiPlus.h>
using namespace Gdiplus;
//Macro to release COM Components
#ifdef SAFE_RELEASE
#undef SAFE_RELEASE
#endif
#define SAFE_RELEASE(x) do{\
if((x) != NULL)\
{\
while((x)->Release() != 0);\
(x) = NULL;\
}\
}while(0)
CPictureCtrl::CPictureCtrl(void)
:CStatic()
, m_pStream(NULL)
, m_bIsPicLoaded(FALSE)
, m_gdiplusToken(0)
{
GdiplusStartupInput gdiplusStartupInput;
GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
}
CPictureCtrl::~CPictureCtrl(void)
{
//Tidy up
FreeData();
GdiplusShutdown(m_gdiplusToken);
}
BOOL CPictureCtrl::LoadFromStream(IStream *piStream)
{
//Set success error state
SetLastError(ERROR_SUCCESS);
FreeData();
//Check for validity of argument
if(piStream == NULL)
{
SetLastError(ERROR_INVALID_ADDRESS);
return FALSE;
}
//Allocate stream
DWORD dwResult = CreateStreamOnHGlobal(NULL, TRUE, &m_pStream);
if(dwResult != S_OK)
{
SetLastError(dwResult);
return FALSE;
}
//Rewind the argument stream
LARGE_INTEGER lInt;
lInt.QuadPart = 0;
piStream->Seek(lInt, STREAM_SEEK_SET, NULL);
//Read the lenght of the argument stream
STATSTG statSTG;
dwResult = piStream->Stat(&statSTG, STATFLAG_DEFAULT);
if(dwResult != S_OK)
{
SetLastError(dwResult);
SAFE_RELEASE(m_pStream);
return FALSE;
}
//Copy the argument stream to the class stream
piStream->CopyTo(m_pStream, statSTG.cbSize, NULL, NULL);
//Mark as loaded
m_bIsPicLoaded = TRUE;
Invalidate();
RedrawWindow();
return TRUE;
}
BOOL CPictureCtrl::LoadFromStream(BYTE* pData, size_t nSize)
{
//Set success error state
SetLastError(ERROR_SUCCESS);
FreeData();
//Allocate stream
DWORD dwResult = CreateStreamOnHGlobal(NULL, TRUE, &m_pStream);
if(dwResult != S_OK)
{
SetLastError(dwResult);
return FALSE;
}
//Copy argument data to the stream
dwResult = m_pStream->Write(pData, (ULONG)nSize, NULL);
if(dwResult != S_OK)
{
SetLastError(dwResult);
SAFE_RELEASE(m_pStream);
return FALSE;
}
//Mark as loaded
m_bIsPicLoaded = TRUE;
Invalidate();
RedrawWindow();
return TRUE;
}
BOOL CPictureCtrl::LoadFromFile(CString &szFilePath)
{
//Set success error state
SetLastError(ERROR_SUCCESS);
FreeData();
//Allocate stream
DWORD dwResult = CreateStreamOnHGlobal(NULL, TRUE, &m_pStream);
if(dwResult != S_OK)
{
SetLastError(dwResult);
return FALSE;
}
//Open the specified file
CFile cFile;
CFileException cFileException;
if(!cFile.Open(szFilePath, CStdioFile::modeRead | CStdioFile::typeBinary, &cFileException))
{
SetLastError(cFileException.m_lOsError);
SAFE_RELEASE(m_pStream);
return FALSE;
}
//Copy the specified file's content to the stream
BYTE pBuffer[1024] = {0};
while(UINT dwRead = cFile.Read(pBuffer, 1024))
{
dwResult = m_pStream->Write(pBuffer, dwRead, NULL);
if(dwResult != S_OK)
{
SetLastError(dwResult);
SAFE_RELEASE(m_pStream);
cFile.Close();
return FALSE;
}
}
//Close the file
cFile.Close();
//Mark as Loaded
m_bIsPicLoaded = TRUE;
Invalidate();
RedrawWindow();
return TRUE;
}
// BOOL CPictureCtrl::LoadFromResource(HMODULE hModule, LPCTSTR lpName, LPCTSTR lpType)
// {
// //Set success error state
// SetLastError(ERROR_SUCCESS);
// FreeData();
//
// //Locate the resource
// HRSRC hResource = FindResource(hModule, lpName, lpType);
// if(hResource == NULL)
// {
// return FALSE;
// }
//
// //Get the size of the resource
// DWORD dwResourceSize = SizeofResource(hModule, hResource);
// if(dwResourceSize == 0)
// {
// return FALSE;
// }
//
// //Load the Resource
// HGLOBAL hGlobalResource = LoadResource(hModule, hResource);
// if(hGlobalResource == NULL)
// {
// return FALSE;
// }
//
// //Lock the resource and get the read pointer
// BYTE* pRecource = (BYTE*)LockResource(hGlobalResource);
// if(pRecource == NULL)
// {
// return FALSE;
// }
//
// //Allocate the Stream
// DWORD dwResult = CreateStreamOnHGlobal(NULL, TRUE, &m_pStream);
// if(dwResult != S_OK)
// {
// FreeResource(hGlobalResource);
// SetLastError(dwResult);
// pRecource = NULL;
// return FALSE;
// }
//
// //Copy the resource data to the stream
// dwResult = m_pStream->Write(pRecource, dwResourceSize, NULL);
// if(dwResult != S_OK)
// {
// FreeResource(hGlobalResource);
// SAFE_RELEASE(m_pStream);
// SetLastError(dwResult);
// return FALSE;
// }
//
// //Tidy up
// // FreeResource(hGlobalResource);
//
// //Mark as loaded
// m_bIsPicLoaded = TRUE;
//
// Invalidate();
// RedrawWindow();
//
// return TRUE;
// }
//Overload - Single load function
BOOL CPictureCtrl::Load(CString &szFilePath)
{
return LoadFromFile(szFilePath);
}
BOOL CPictureCtrl::Load(IStream* piStream)
{
return LoadFromStream(piStream);
}
BOOL CPictureCtrl::Load(BYTE* pData, size_t nSize)
{
return LoadFromStream(pData, nSize);
}
// BOOL CPictureCtrl::Load(HMODULE hModule, LPCTSTR lpName, LPCTSTR lpType)
// {
// return LoadFromResource(hModule, lpName, lpType);
// }
void CPictureCtrl::FreeData()
{
m_bIsPicLoaded = FALSE;
SAFE_RELEASE(m_pStream);
}
void CPictureCtrl::PreSubclassWindow()
{
CStatic::PreSubclassWindow();
ModifyStyle(0, SS_OWNERDRAW);
}
void CPictureCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
//Check if pic data is loaded
if(m_bIsPicLoaded)
{
//Get control measures
RECT rc;
this->GetClientRect(&rc);
Graphics graphics(lpDrawItemStruct->hDC);
Image image(m_pStream);
graphics.DrawImage(&image, (INT)rc.left, (INT)rc.top, (INT)(rc.right-rc.left), (INT)(rc.bottom-rc.top));
}
}
BOOL CPictureCtrl::OnEraseBkgnd(CDC *pDC)
{
if(m_bIsPicLoaded)
{
//Get control measures
RECT rc;
this->GetClientRect(&rc);
Graphics graphics(pDC->GetSafeHdc());
LARGE_INTEGER liSeekPos;
liSeekPos.QuadPart = 0;
m_pStream->Seek(liSeekPos, STREAM_SEEK_SET, NULL);
Image image(m_pStream);
graphics.DrawImage(&image, (INT)rc.left, (INT)rc.top, (INT)(rc.right-rc.left), (INT)(rc.bottom-rc.top));
return TRUE;
}
else
{
return CStatic::OnEraseBkgnd(pDC);
}
}