-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSplashWindow.cpp
486 lines (403 loc) · 15.9 KB
/
SplashWindow.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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
* Copyright (c) 2004 Apple Enterprises. This code is
* proprietary and a trade secret of Apple Enterprises.
*
* $Workfile: SplashWindow.cpp $
*
* $Creator: Chris Apple $
*
* $Description: Initial splash screen. $
*
* $Log: /AppleEnterprises/CodeExamples/SplashMFC/SplashMfcSingleDoc/SplashWindow.cpp $
*
* 1 4/22/09 4:11p Capple
* Initial Checkin.
*
* 1 11/05/04 1:27p Capple
* Initial Checkin.
*
* $NoKeywords: $
*/
#include "stdafx.h"
#include "resource.h"
#include "SplashWindow.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// Splash Screen class
CSplashWindow* CSplashWindow::m_pSplashWindow;
CString CSplashWindow::m_productNameString;
CString CSplashWindow::m_companyNameString;
CString CSplashWindow::m_versionNumberString;
CString CSplashWindow::m_versionString;
CString CSplashWindow::m_copyrightString;
CString CSplashWindow::m_commentsString;
CString CSplashWindow::m_statusMessage;
int CSplashWindow::m_millisecondsToDisplay; // 0 ==> until mouse click or keystroke
// create rectangle that product name has to fit in
const int CSplashWindow::m_productNameVerticalOffset = 60; // empty space between top border and Product Name
const int CSplashWindow::m_productNameVerticalHeight = 100; // maximum height of Product Name
const int CSplashWindow::m_productNameLeftMargin = 10; // distance from left side to place name, company, copyright and version
const int CSplashWindow::m_productNameRightMargin = 10; // distance from right side to place name, company, copyright and version
const CString CSplashWindow::m_productNameFontName = "Tahoma"; // name of font for application name
CSize CSplashWindow::m_productNamePointSize = CSize(-1,-1); // point size used for the application name, (-1,-1) ==> Calculate point size
COLORREF CSplashWindow::m_productNameTextColor = RGB(49, 80 , 0);// color used for text
const BOOL CSplashWindow::m_displayCompanyName = TRUE; // true if displaying companyName
const BOOL CSplashWindow::m_displayVersion = TRUE; // true if displaying version
const BOOL CSplashWindow::m_displayCopyright = TRUE; // true if displaying copyright
const BOOL CSplashWindow::m_displayComments = FALSE; // true if displaying comments
// create rectangle that strings in body have to fit in
const int CSplashWindow::m_bodyVerticalOffset = 160; // empty space between top border and top of body
const int CSplashWindow::m_bodyVerticalHeight = 120; // maximum height of body
const int CSplashWindow::m_bodyLeftMargin = 200; // distance from left side to place company name, copyright, version and comment
const int CSplashWindow::m_bodyRightMargin = 10; // distance from right side to place company name, copyright, version and comment
const CString CSplashWindow::m_bodyFontName = "Tahoma"; // name of font for company name, copyright and version
CSize CSplashWindow::m_bodyPointSize = CSize(-1,-1); // point size used for company name, copyright and version, (-1,-1) ==> Calculate point size
COLORREF CSplashWindow::m_bodyTextColor = RGB(109,140,44); // color used for company name, copyright and version (-1 ==> use application name color)
// create rectangle for status line string
const int CSplashWindow::m_statusVerticalOffset = 250; // empty space between top border and top of status string
const int CSplashWindow::m_statusVerticalHeight = 50; // maximum height of status string
const int CSplashWindow::m_statusLeftMargin = 350; // distance from left side to place status string
const int CSplashWindow::m_statusRightMargin = 10; // distance from right side to place status string
const CString CSplashWindow::m_statusMessageFontName = "Tahoma"; // name of font for status message while starting
CSize CSplashWindow::m_statusMessagePointSize = CSize(-1,-1); // point size used for status message while starting, (-1,-1) ==> Calculate point size
COLORREF CSplashWindow::m_statusMessageTextColor = RGB(0,0,255);// color used for status message while starting (-1 ==> use application name color)
CSplashWindow::CSplashWindow()
{
GetVersionStrings();
}
CSplashWindow::~CSplashWindow()
{
// Clear the static window pointer.
ASSERT(m_pSplashWindow == this);
m_pSplashWindow = NULL;
}
BEGIN_MESSAGE_MAP(CSplashWindow, CWnd)
//{{AFX_MSG_MAP(CSplashWindow)
ON_WM_CREATE()
ON_WM_PAINT()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void CSplashWindow::ShowSplashScreen( CWnd* pParentWnd /*= NULL*/, LPCTSTR statusMessage /*= NULL*/, int millisecondsToDisplay /*= 0*/ )
{
if ( m_statusMessage.IsEmpty() ) {
m_statusMessage.Empty();
}
if ( statusMessage ) {
m_statusMessage = statusMessage;
}
m_millisecondsToDisplay = millisecondsToDisplay;
// Allocate a new splash screen, and create the window.
if ( m_pSplashWindow == NULL ) {
m_pSplashWindow = new CSplashWindow;
if (!m_pSplashWindow->Create(pParentWnd)) {
delete m_pSplashWindow;
m_pSplashWindow = NULL;
}
}
// Set a timer to destroy the splash screen.
if ( millisecondsToDisplay ) {
m_pSplashWindow->SetTimer( 1, m_millisecondsToDisplay, NULL );
}
m_pSplashWindow->ShowWindow ( SW_SHOW );
m_pSplashWindow->UpdateWindow();
m_pSplashWindow->InvalidateRect( NULL, FALSE );
// clear message queue
MSG msg;
while (PeekMessage(&msg, m_pSplashWindow->m_hWnd, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
BOOL CSplashWindow::PreTranslateAppMessage(MSG* pMsg)
{
if (m_pSplashWindow == NULL)
return FALSE;
// If we get a keyboard or mouse message, hide the splash screen.
if (pMsg->message == WM_KEYDOWN ||
pMsg->message == WM_SYSKEYDOWN ||
pMsg->message == WM_LBUTTONDOWN ||
pMsg->message == WM_RBUTTONDOWN ||
pMsg->message == WM_MBUTTONDOWN ||
pMsg->message == WM_NCLBUTTONDOWN ||
pMsg->message == WM_NCRBUTTONDOWN ||
pMsg->message == WM_NCMBUTTONDOWN)
{
m_pSplashWindow->HideSplashScreen();
return TRUE; // message handled here
}
return FALSE; // message not handled
}
BOOL CSplashWindow::Create(CWnd* pParentWnd /*= NULL*/)
{
if (!m_bitmap.LoadBitmap(IDB_SPLASH))
return FALSE;
BITMAP bm;
m_bitmap.GetBitmap(&bm);
int xPos = 0;
int yPos = 0;
int width = bm.bmWidth;
int height = bm.bmHeight;
// if parent window, center it on the parent window. otherwise center it on the screen
CRect parentRect;
if ( pParentWnd == NULL ) {
GetDesktopWindow()->GetWindowRect (parentRect );
} else {
pParentWnd->GetWindowRect ( parentRect );
}
xPos = parentRect.left + (parentRect.right - parentRect.left)/2 - (width/2);
yPos = parentRect.top + (parentRect.bottom - parentRect.top)/2 - (height/2);
BOOL result = CreateEx(0,
AfxRegisterWndClass(0, AfxGetApp()->LoadStandardCursor(IDC_ARROW)),
NULL, WS_POPUP | WS_VISIBLE, xPos, yPos, width, height, pParentWnd->GetSafeHwnd(), NULL);
if ( !result )
return FALSE;
// if no parent window, make it a topmost, so eventual application window will appear under it
if ( pParentWnd == NULL ) {
SetWindowPos( &wndTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE );
}
return TRUE;
}
void CSplashWindow::HideSplashScreen()
{
// Destroy the window, and update the mainframe.
if ( m_pSplashWindow != NULL ) {
m_pSplashWindow->DestroyWindow();
CWnd* mainWnd = AfxGetMainWnd();
if ( mainWnd && IsWindow(mainWnd->m_hWnd) )
mainWnd->UpdateWindow();
}
}
void CSplashWindow::PostNcDestroy()
{
// Free the C++ class.
delete this;
}
int CSplashWindow::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// Center the window.
CenterWindow();
// Set a timer to destroy the splash screen.
if ( m_millisecondsToDisplay ) {
SetTimer(1, m_millisecondsToDisplay, NULL);
}
return 0;
}
CSize CSplashWindow::FindFontPointSize( CPaintDC &dc, LPCTSTR fontName, const CStringArray &stringsToCheck, CSize maximumSize )
{
CFont font;
int pointSize = 8;
CSize previousLargest = CSize(0,0);
CSize largest = CSize(0,0);
int numberOfStringsToCheck = stringsToCheck.GetSize();
maximumSize.cy /= numberOfStringsToCheck;
while ( 1 ) {
CFont* originalFont = dc.SelectObject(&font);
font.CreatePointFont( pointSize*10, fontName, &dc);
dc.SelectObject(&font);
previousLargest = largest;
for ( int i=0; i<numberOfStringsToCheck; i++ ) {
CSize szProduct = dc.GetTextExtent( stringsToCheck[i] );
if ( largest.cx < szProduct.cx ) {
largest = szProduct;
}
}
dc.SelectObject(&originalFont);
font.DeleteObject();
if ( largest.cx > maximumSize.cx )
break;
if ( largest.cy > maximumSize.cy )
break;
pointSize += 2;
}
pointSize -= 2;
CSize ret = CSize ( pointSize, previousLargest.cy );
return ret;
}
void CSplashWindow::OnPaint()
{
CPaintDC dc(this);
CDC dcImage;
if (!dcImage.CreateCompatibleDC(&dc))
return;
BITMAP bm;
m_bitmap.GetBitmap(&bm);
// Paint the image.
CBitmap* pOldBitmap = dcImage.SelectObject(&m_bitmap);
dc.BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &dcImage, 0, 0, SRCCOPY);
dcImage.SelectObject(pOldBitmap);
// calculate height of strings
int windowHeight = bm.bmHeight;
int windowWidth = bm.bmWidth;
// get point sizes of different strings
CStringArray stringsToCheck;
SIZE sectionSize;
// draw product name
int productNameLeftMargin = m_productNameLeftMargin;
int productNameRightMargin = windowWidth - m_productNameRightMargin;
sectionSize.cx = productNameRightMargin - productNameLeftMargin;
sectionSize.cy = m_productNameVerticalHeight;
if ( m_productNamePointSize == CSize(-1,-1) ) {
stringsToCheck.RemoveAll();
stringsToCheck.Add(m_productNameString);
m_productNamePointSize = CSplashWindow::FindFontPointSize( dc, m_productNameFontName, stringsToCheck, sectionSize );
}
CFont productNameFont;
productNameFont.CreatePointFont(m_productNamePointSize.cx*10, m_productNameFontName, &dc);
CFont* originalFont = dc.SelectObject(&productNameFont);
int topOfText = m_productNameVerticalOffset;
int bottomOfText = topOfText + m_productNameVerticalHeight;
CRect productNameRect = CRect(productNameLeftMargin,topOfText, productNameRightMargin, bottomOfText );
dc.SetTextColor( m_productNameTextColor );
dc.SetBkMode(TRANSPARENT);
dc.DrawText(m_productNameString, productNameRect, DT_VCENTER|DT_CENTER|DT_SINGLELINE );
// draw body
int bodyLeftMargin = m_bodyLeftMargin;
int bodyRightMargin = windowWidth - m_bodyRightMargin;
sectionSize.cx = bodyRightMargin - bodyLeftMargin;
sectionSize.cy = m_bodyVerticalHeight;
if ( m_bodyPointSize == CSize(-1,-1) ) {
stringsToCheck.RemoveAll();
if ( m_displayCompanyName )
stringsToCheck.Add(m_companyNameString);
if ( m_displayVersion )
stringsToCheck.Add(m_versionString);
if ( m_displayCopyright )
stringsToCheck.Add(m_copyrightString);
if ( m_displayComments )
stringsToCheck.Add(m_commentsString);
m_bodyPointSize = CSplashWindow::FindFontPointSize( dc, m_bodyFontName, stringsToCheck, sectionSize );
}
CFont bodyFont;
bodyFont.CreatePointFont(m_bodyPointSize.cx*10, m_bodyFontName, &dc);
dc.SetTextColor( (m_bodyTextColor == -1) ? m_productNameTextColor : m_bodyTextColor );
dc.SetBkMode(TRANSPARENT);
dc.SelectObject(&bodyFont);
int singleStringOfBodyHeight = m_bodyPointSize.cy;
topOfText = m_bodyVerticalOffset;
bottomOfText = topOfText + singleStringOfBodyHeight;
if ( m_displayCompanyName ) {
CRect companyNameRect = CRect(bodyLeftMargin, topOfText, bodyRightMargin, bottomOfText );
dc.DrawText(m_companyNameString, companyNameRect, DT_VCENTER|DT_CENTER|DT_SINGLELINE );
topOfText += singleStringOfBodyHeight;
bottomOfText += singleStringOfBodyHeight;
}
if ( m_displayVersion ) {
CRect versionRect = CRect(bodyLeftMargin, topOfText, bodyRightMargin, bottomOfText );
dc.DrawText(m_versionString, versionRect, DT_VCENTER|DT_CENTER|DT_SINGLELINE );
topOfText += singleStringOfBodyHeight;
bottomOfText += singleStringOfBodyHeight;
}
if ( m_displayCopyright ) {
CRect copyrightRect = CRect(bodyLeftMargin, topOfText, bodyRightMargin, bottomOfText );
dc.DrawText(m_copyrightString, copyrightRect, DT_VCENTER|DT_CENTER|DT_SINGLELINE );
topOfText += singleStringOfBodyHeight;
bottomOfText += singleStringOfBodyHeight;
}
if ( m_displayComments ) {
CRect commentsRect = CRect(bodyLeftMargin, topOfText, bodyRightMargin, bottomOfText );
dc.DrawText(m_commentsString, commentsRect, DT_VCENTER|DT_CENTER|DT_SINGLELINE );
topOfText += singleStringOfBodyHeight;
bottomOfText += singleStringOfBodyHeight;
}
// draw status
if ( !m_statusMessage.IsEmpty() ) {
int statusLeftMargin = m_statusLeftMargin;
int statusRightMargin = windowWidth - m_statusRightMargin;
sectionSize.cx = statusRightMargin - statusLeftMargin;
sectionSize.cy = m_statusVerticalHeight;
if ( m_statusMessagePointSize == CSize(-1,-1) ) {
stringsToCheck.RemoveAll();
stringsToCheck.Add(m_statusMessage);
m_statusMessagePointSize = CSplashWindow::FindFontPointSize( dc, m_statusMessageFontName, stringsToCheck, sectionSize );
}
topOfText = m_statusVerticalOffset;
bottomOfText = topOfText + m_statusVerticalHeight;
CRect statusRect = CRect(statusLeftMargin,topOfText, statusRightMargin, bottomOfText );
CFont statusFont;
statusFont.CreatePointFont(m_statusMessagePointSize.cx*10, m_statusMessageFontName, &dc);
dc.SetTextColor( (m_statusMessageTextColor == -1) ? m_productNameTextColor : m_statusMessageTextColor );
dc.SetBkMode(TRANSPARENT);
dc.SelectObject(&statusFont);
dc.DrawText(m_statusMessage, statusRect, DT_VCENTER|DT_CENTER|DT_SINGLELINE );
}
dc.SelectObject(&originalFont);
productNameFont.DeleteObject();
bodyFont.DeleteObject();
}
void CSplashWindow::OnTimer(UINT nIDEvent)
{
// Destroy the splash screen window.
HideSplashScreen();
}
void CSplashWindow::GetVersionStrings()
{
DWORD dwHandle; // ignored
char *buf; // pointer to buffer to receive file-version info.
char buf1[1024]; // pointer to buffer to store version string.
CString nameStr;
nameStr.Format("%s.EXE", AfxGetApp()->m_pszExeName);
char *name = (char *)((const char *)nameStr);
// Get the size of the version information.
DWORD verSize = GetFileVersionInfoSize(
name, // pointer to filename string
&dwHandle // pointer to variable to receive zero
);
if (verSize != 0)
{
buf = new char[verSize + 1];
BOOL res = GetFileVersionInfo(
name, // pointer to filename string
NULL, // ignored
verSize, // size of buffer
buf // pointer to buffer to receive file-version info.
);
ASSERT(res);
UINT ver = (UINT)verSize;
LPVOID lplpBuffer;
BOOL doThis = VerQueryValue (buf, TEXT("\\StringFileInfo\\040904b0\\ProductName"), &lplpBuffer, &ver);
if ( doThis ) {
strncpy(buf1, (char *)lplpBuffer, ver);
m_productNameString = (CString)buf1;
}
doThis = VerQueryValue (buf, TEXT("\\StringFileInfo\\040904b0\\CompanyName"), &lplpBuffer, &ver);
if ( doThis ) {
strncpy(buf1, (char *)lplpBuffer, ver);
m_companyNameString = (CString)buf1;
}
doThis = VerQueryValue (buf, TEXT("\\StringFileInfo\\040904b0\\LegalCopyright"), &lplpBuffer, &ver);
if ( doThis ) {
strncpy(buf1, (char *)lplpBuffer, ver);
m_copyrightString = (CString)buf1;
}
doThis = VerQueryValue (buf, TEXT("\\StringFileInfo\\040904b0\\Comments"), &lplpBuffer, &ver);
if ( doThis ) {
strncpy(buf1, (char *)lplpBuffer, ver);
m_commentsString = (CString)buf1;
}
doThis = VerQueryValue (buf, TEXT("\\StringFileInfo\\040904b0\\ProductVersion"), &lplpBuffer, &ver);
if ( doThis ) {
strncpy(buf1, (char *)lplpBuffer, ver);
int versionNumbers[4];
int numberOfValues = sscanf ( buf1, "%d,%d,%d,%d", &versionNumbers[0],&versionNumbers[1], &versionNumbers[2], &versionNumbers[3] );
CString number;
m_versionNumberString = "";
for ( int i=0; i<numberOfValues; i++ ) {
if ( m_versionNumberString.IsEmpty() && (versionNumbers[i] == 0) )
continue;
if ( !m_versionNumberString.IsEmpty() )
m_versionNumberString += ".";
number.Format("%d", versionNumbers[i] );
m_versionNumberString += number;
}
m_versionString = "Version " + m_versionNumberString;
}
delete buf;
}
}