forked from Steve235lab/SHU-Traveller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SlideShowCtrl.cpp
274 lines (234 loc) · 5.95 KB
/
SlideShowCtrl.cpp
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
#include "SlideShowCtrl.h"
#define _SLIDE_TIMER_ID 444555777
CSlideShowCtrl::CSlideShowCtrl(void)
{
m_vImages.clear();
m_iActiveIndex = 0;
m_CurrentBmp = NULL;
}
CSlideShowCtrl::~CSlideShowCtrl(void)
{
}
BEGIN_MESSAGE_MAP(CSlideShowCtrl, CStatic)
ON_WM_TIMER()
ON_WM_PAINT()
END_MESSAGE_MAP()
HBITMAP CSlideShowCtrl::ReadJpegFile(LPWSTR& pFile)
{
HANDLE hFile;
HBITMAP hBmp;
DWORD dwSize;
DWORD dwRead;
HGLOBAL hMemJpeg;
LPSTREAM lpStream;
OLE_HANDLE hJpegBmp;
HRESULT hr;
LPPICTURE lpPicture = NULL;
void *pMemJpeg;
/* Open the file and get the size. */
if((hFile = CreateFile(pFile, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
return NULL;
if((dwSize = GetFileSize(hFile, NULL)) == 0xFFFFFFFF)
{
CloseHandle(hFile);
return NULL;
}
/* Allocate space for file, read it in, and then close the file again. */
if((hMemJpeg = GlobalAlloc(GMEM_MOVEABLE, dwSize)) == NULL)
{
CloseHandle(hFile);
return NULL;
}
if((pMemJpeg = GlobalLock(hMemJpeg)) == NULL)
{
CloseHandle(hFile);
GlobalFree(hMemJpeg);
return NULL;
}
if(!ReadFile(hFile, pMemJpeg, dwSize, &dwRead, NULL))
{
CloseHandle(hFile);
GlobalFree(hMemJpeg);
return NULL;
}
CloseHandle(hFile);
GlobalUnlock(hMemJpeg);
/* Create the stream and load the picture. */
if((hr = CreateStreamOnHGlobal(hMemJpeg, TRUE, &lpStream)) != S_OK)
{
GlobalFree(hMemJpeg);
return NULL;
}
if(OleLoadPicture(lpStream, dwSize, FALSE, IID_IPicture, (LPVOID*)(&lpPicture)) != S_OK)
{
GlobalFree(hMemJpeg);
lpStream->Release();
return NULL;
}
/* Get the handle to the image, and then copy it. */
if((lpPicture->get_Handle(&hJpegBmp)) != S_OK)
{
GlobalFree(hMemJpeg);
lpStream->Release();
lpPicture->Release();
return NULL;
}
if((hBmp = (HBITMAP)CopyImage((HANDLE *) hJpegBmp, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG)) == NULL)
{
GlobalFree(hMemJpeg);
lpStream->Release();
lpPicture->Release();
return NULL;
}
/* Free the original image and memory. */
GlobalFree(hMemJpeg);
lpStream->Release();
lpPicture->Release();
return hBmp;
}
void CSlideShowCtrl::AddImage(LPWSTR imagePath)
{
HBITMAP image = ReadJpegFile(imagePath);
AddImage(image);
}
void CSlideShowCtrl::AddImage(HBITMAP image)
{
if(image != NULL)
{
//Stretch image if needed to fit
CRect Rect;
GetClientRect(&Rect);
int frameW = Rect.Width();
int frameH = Rect.Height();
BITMAP bm;
GetObject(image, sizeof(bm),&bm);
int bmpW = bm.bmWidth;
int bmpH = bm.bmHeight;
double Ratio = 1.0;
if(bmpW > frameW)
Ratio = (double)frameW / (double)bmpW;
if(bmpH > frameH)
{
double Temp = frameH / (double)bmpH;
if (Temp < Ratio)
Ratio = Temp;
}
int width = (int)(bmpW*Ratio);
int height = (int)(bmpH*Ratio);
//Save it to vector
if(Ratio != 1.0)
m_vImages.push_back(ResizeBmp(image, width, height));
else
m_vImages.push_back(image);
}
}
void CSlideShowCtrl::ClearAllImages()
{
m_vImages.clear();
Stop();
}
void CSlideShowCtrl::Start()
{
if(m_vImages.empty())
return;
m_CurrentBmp = m_vImages[0];
Invalidate();
UpdateWindow();
if(m_vImages.size() > 1)
{
SetTimer(_SLIDE_TIMER_ID, 2000, NULL);
m_iActiveIndex++;
}
}
void CSlideShowCtrl::Stop()
{
KillTimer(_SLIDE_TIMER_ID);
m_CurrentBmp = NULL;
Invalidate();
UpdateWindow();
m_iActiveIndex = 0;
}
void CSlideShowCtrl::OnTimer(UINT nIDEvent)
{
if(m_iActiveIndex >= (int)m_vImages.size())
m_iActiveIndex = 0;
m_CurrentBmp = m_vImages[m_iActiveIndex++];
Invalidate();
UpdateWindow();
CStatic::OnTimer(nIDEvent);
}
void CSlideShowCtrl::OnPaint()
{
CPaintDC dc(this); // device context for painting
CRect Rect;
GetClientRect(&Rect);
CDC MemDC;
MemDC.CreateCompatibleDC(&dc);
CBitmap Bmp;
Bmp.CreateCompatibleBitmap(&dc,Rect.Width(),Rect.Height());
int SavedDC = MemDC.SaveDC();
MemDC.SelectObject(&Bmp);
MemDC.FillSolidRect(Rect,RGB(127,127,127));
dc.BitBlt(0,0,Rect.Width(),Rect.Height(),&MemDC,0,0,SRCCOPY);
if(m_CurrentBmp != NULL)
{
//Stratch to fit if needed
BITMAP bm;
GetObject(m_CurrentBmp, sizeof(bm),&bm);
int oriW = bm.bmWidth;
int oriH = bm.bmHeight;
double Ratio = 1.0;
if(oriW > Rect.Width())
Ratio = (double)Rect.Width() / (double)oriW;
if (oriH > Rect.Height())
{
double Temp = Rect.Height() / (double)oriH;
if (Temp < Ratio)
Ratio = Temp;
}
int X = (int)(Rect.Width() - (oriW*Ratio)) / 2;
int Y = (int)(Rect.Height() - (oriH*Ratio)) / 2;
CBitmap* pBitmap = CBitmap::FromHandle(m_CurrentBmp);
MemDC.SelectObject(pBitmap);
dc.BitBlt(X, Y, (int)(oriW*Ratio), (int)(oriH*Ratio), &MemDC, 0, 0, SRCCOPY);
}
MemDC.RestoreDC(SavedDC);
}
HBITMAP CSlideShowCtrl::ResizeBmp(HBITMAP hBmp,int cx,int cy)
{
HDC dc = *(GetDC());
HDC copyDC = CreateCompatibleDC(dc);
HDC srcDC = CreateCompatibleDC(dc);
int oriCx,oriCy;
//Get Original Size
BITMAP bm;
GetObject(hBmp, sizeof(bm),&bm);
oriCx = bm.bmWidth;
oriCy = bm.bmHeight;
RECT bmpRect;
bmpRect.top=0;
bmpRect.left=0;
bmpRect.right=cx;
bmpRect.bottom=cy;
RECT oriRect = bmpRect;
oriRect.right=oriCx;
oriRect.bottom=oriCy;
HRGN rgn1 = CreateRectRgnIndirect( &bmpRect );
HRGN rgn2 = CreateRectRgnIndirect( &oriRect );
SelectClipRgn(copyDC,rgn1);
SelectClipRgn(srcDC,rgn2);
DeleteObject(rgn1);
DeleteObject(rgn2);
HBITMAP copyBMP = CreateCompatibleBitmap(dc,cx,cy);
HBITMAP copyBMPold = (HBITMAP)SelectObject(copyDC,copyBMP);
HBITMAP srcBMPold = (HBITMAP)SelectObject(srcDC,hBmp);
if(oriCx!=cx || oriCy!=cy)
StretchBlt(copyDC,0,0,cx,cy,srcDC,0,0,oriCx,oriCy,SRCCOPY);
else
BitBlt(copyDC,0,0,cx,cy,srcDC,0,0,SRCCOPY);
copyBMP = (HBITMAP)SelectObject(copyDC, copyBMPold);
DeleteDC(copyDC);
DeleteDC(srcDC);
return copyBMP;
}