-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPercent.cpp
More file actions
610 lines (489 loc) · 14.6 KB
/
Percent.cpp
File metadata and controls
610 lines (489 loc) · 14.6 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
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
// percent.cpp : implementation file
//
#include "stdafx.h"
#include "resource.h"
#include "percent.h"
// for CFTPercentDialog's CFtpView pointer
#include "ftp.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CPercentDialog dialog
CPercentDialog::CPercentDialog(CWnd* pParent /*=NULL*/)
: CDialog(CPercentDialog::IDD, pParent)
{
//{{AFX_DATA_INIT(CPercentDialog)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_bULONG = FALSE;
}
void CPercentDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPercentDialog)
//}}AFX_DATA_MAP
}
void CPercentDialog::Initialize (CString Title, CString Text, COLORREF Color)
{
// Initialize member variables.
mTitle = Title;
mText = Text;
mColor = Color;
mCurrentPercent = 0;
m_ulCurrentPercent = 0;
m_bULONG = FALSE;
// Create the dialog.
CDialog::Create (CPercentDialog::IDD);
}
void CPercentDialog::SetPercent (int nPercent)
{
// Set the current percent value.
mCurrentPercent = nPercent;
// the percent is not an unsigned long int, so set m_bULONG to FALSE
m_bULONG = FALSE;
// Invalidate the percent bar to force update with new value.
InvalidateRect(mBarRect, FALSE);
UpdateWindow();
}
void CPercentDialog::SetULPercent(unsigned long int ulPercent)
{
// Set the current percent value
m_ulCurrentPercent = ulPercent;
m_bULONG = TRUE;
// Invalidate the percent bar to force update with new value
InvalidateRect(mBarRect, FALSE);
UpdateWindow();
}
void CPercentDialog::CalculatePercent(int nAmount, int nTotal)
{
// To calculate percent, multiply nAmount by 100, then divide
// the product by nTotal.
int nPercent = ::MulDiv(nAmount, 100, nTotal);
// If the value of nPercent is greater than 100, then set the
// value of nPercent to be equal to 100.
if (nPercent > 100)
nPercent = 100;
SetPercent(nPercent);
return;
}
void CPercentDialog::CalculateULPercent(unsigned long int ulAmount,
unsigned long int ulTotal)
{
// To calculate percent, multiply ulAmount by 100, then divide
// the product by ulTotal
unsigned long int ulDividend = ulAmount * 100;
unsigned long int ulPercent = ulDividend / ulTotal;
// Now ulPercent contains the percentage value
// The value of ulPercent can't be > 100, so if it is greater, then
// we need to set ulPercent equal to 100.
if (ulPercent > 100)
ulPercent = 100;
SetULPercent(ulPercent);
return;
}
BEGIN_MESSAGE_MAP(CPercentDialog, CDialog)
//{{AFX_MSG_MAP(CPercentDialog)
ON_WM_PAINT()
ON_WM_SYSCOMMAND()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPercentDialog message handlers
void CPercentDialog::OnPaint()
{
CPaintDC PaintDC(this); // device context for painting
CDC MemDC;
CBitmap Bitmap, *pOldBitmap;
CPen GrayPen(PS_SOLID, 1, ::GetSysColor(COLOR_BTNSHADOW));
CPen WhitePen(PS_SOLID, 1, ::GetSysColor(COLOR_BTNHIGHLIGHT));
CBrush GrayBrush(::GetSysColor(COLOR_BTNFACE));
int Width;
unsigned long int ulWidth;
CRect Rect;
char Junk[10];
TEXTMETRIC TextMetric;
// Create a device context to work on.
MemDC.CreateCompatibleDC(&PaintDC);
// Create the brush to draw the bar and set the text color
CBrush Brush(mColor);
MemDC.SetTextColor(RGB(255,255,255));
// Draw the inset rectangle for the bar.
CPen* pOldPen = PaintDC.SelectObject(&GrayPen);
PaintDC.MoveTo(BARX, BARY);
PaintDC.LineTo(BARX + BARWIDTH, BARY);
PaintDC.MoveTo(BARX, BARY);
PaintDC.LineTo(BARX, BARY + BARHEIGHT);
PaintDC.SelectObject(pOldPen);
pOldPen = PaintDC.SelectObject(&WhitePen);
PaintDC.MoveTo(BARX + BARWIDTH, BARY + BARHEIGHT);
PaintDC.LineTo(BARX + BARWIDTH, BARY);
PaintDC.MoveTo(BARX + BARWIDTH, BARY + BARHEIGHT);
PaintDC.LineTo(BARX, BARY + BARHEIGHT);
PaintDC.SelectObject(pOldPen);
// Create the bitmap to draw the bar on.
Bitmap.CreateCompatibleBitmap(&PaintDC, BARWIDTH - 1, BARHEIGHT - 1);
pOldBitmap = (CBitmap *)MemDC.SelectObject(&Bitmap);
// Draw the background for the bar.
Rect.SetRect(0, 0, BARWIDTH - 1, BARHEIGHT - 1);
MemDC.FillRect(Rect, &GrayBrush);
// Draw the percentange bar.
if (!m_bULONG)
{
Width = mCurrentPercent * (BARWIDTH - 1) / 100;
Rect.SetRect(0, 0, Width, BARHEIGHT - 1);
MemDC.FillRect(Rect, &Brush);
}
else
{
ulWidth = m_ulCurrentPercent * (BARWIDTH - 1) / 100;
Rect.SetRect(0,0, (unsigned long int)(int)ulWidth, BARHEIGHT - 1);
MemDC.FillRect(Rect, &Brush);
}
// Display the percentage as text.
MemDC.SetBkMode(TRANSPARENT);
MemDC.GetTextMetrics(&TextMetric);
if (!m_bULONG)
{
wsprintf (Junk, "%d%%", mCurrentPercent);
}
else
{
wsprintf(Junk, "%lu%%", m_ulCurrentPercent);
}
MemDC.TextOut(BARWIDTH / 2 - (TextMetric.tmAveCharWidth * lstrlen (Junk) / 2), BARHEIGHT / 2 - (TextMetric.tmHeight / 2), (LPCSTR) Junk, lstrlen (Junk));
// Blt the bar to the dialog.
PaintDC.BitBlt(BARX + 1, BARY + 1, BARWIDTH - 1, BARHEIGHT - 1, &MemDC, 0, 0, SRCCOPY);
// Clean up.
MemDC.SelectObject(pOldBitmap);
Bitmap.DeleteObject();
Brush.DeleteObject();
GrayBrush.DeleteObject();
GrayPen.DeleteObject();
}
void CPercentDialog::OnCancel()
{
ShowWindow(SW_HIDE);
UpdateWindow();
DestroyWindow();
return;
}
void CPercentDialog::OnOK()
{
ShowWindow(SW_HIDE);
UpdateWindow();
DestroyWindow();
return;
}
BOOL CPercentDialog::OnInitDialog()
{
CDialog::OnInitDialog();
CWnd *pWnd, *pBarWnd;
CRect Rect;
// Set the window caption text.
SetWindowText(mTitle);
// Set the dialog text.
pWnd = GetDlgItem(IDC_PERCENTTEXT);
pWnd->SetWindowText(mText);
// Get the coordinates for the percentage bar.
pBarWnd = GetDlgItem (IDC_BAR);
pBarWnd->GetWindowRect (Rect);
ScreenToClient (Rect);
BARX = Rect.left;
BARY = Rect.top;
BARWIDTH = Rect.Width ();
BARHEIGHT = Rect.Height ();
mBarRect.SetRect (BARX + 1, BARY + 1, BARX + BARWIDTH, BARY + BARHEIGHT);
return TRUE; // return TRUE unless you set the focus to a control
}
void CPercentDialog::OnSysCommand(UINT nID, LPARAM lParam)
{
// This function deals with commands chosen from the Control menu
// If the user chooses the Close command, we need to know about it
// so we can close this window properly
switch (nID & 0xFFF0)
{
case SC_CLOSE:
// Destroy this window
ShowWindow(SW_HIDE);
UpdateWindow();
DestroyWindow();
break;
case SC_SIZE:
case SC_MOVE:
case SC_MINIMIZE:
case SC_MAXIMIZE:
case SC_NEXTWINDOW:
case SC_PREVWINDOW:
case SC_RESTORE:
case SC_TASKLIST:
CWnd::OnSysCommand(nID, lParam);
break;
default:
CWnd::OnSysCommand(nID, lParam);
break;
}
}
void CPercentDialog::PostNcDestroy()
{
ASSERT(m_hWnd == NULL);
delete this; // remove this object from memory
}
/////////////////////////////////////////////////////////////////////////////
// CFTPercentDialog dialog
CFTPercentDialog::CFTPercentDialog(CWnd* pParent /*=NULL*/)
: CDialog(CFTPercentDialog::IDD, pParent)
{
//{{AFX_DATA_INIT(CFTPercentDialog)
//}}AFX_DATA_INIT
}
void CFTPercentDialog::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFTPercentDialog)
DDX_Control(pDX, IDC_FILE_BYTES, m_ctlBytes);
DDX_Text(pDX, IDC_FILE_PATH, m_strFilePath);
DDX_Text(pDX, IDC_WAIT_TEXT, m_strWaitText);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CFTPercentDialog, CDialog)
//{{AFX_MSG_MAP(CFTPercentDialog)
ON_WM_PAINT()
ON_BN_CLICKED(ID_CANCEL_CALL, OnCancelCall)
ON_WM_MOVE()
ON_WM_SYSCOMMAND()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFTPercentDialog helper functions
void CFTPercentDialog::Initialize(CString strFilePath, CString strFileName,
CFtpView* pView, BOOL bUpload)
{
// Initialize member variables.
CString text, str;
str = strFileName;
strFileName.MakeUpper();
if (!bUpload)
{
AfxFormatString1(text, IDS_DL_WAIT_TEXT, strFileName);
}
else
{
AfxFormatString1(text, IDS_UL_WAIT_TEXT, strFileName);
}
strFileName = str;
m_strWaitText = text;
m_strFilePath = strFilePath;
m_pView = pView;
// Sets percent to 0.
m_ulCurrentPercent = 0;
// Create the dialog.
CDialog::Create(CFTPercentDialog::IDD);
}
void CFTPercentDialog::SetPercent(unsigned long int ulPercent)
{
// Set the current percent value
m_ulCurrentPercent = ulPercent;
// Invalidate the percent bar to force update with new value
InvalidateRect(mBarRect, FALSE);
UpdateWindow();
}
void CFTPercentDialog::CalculatePercent(unsigned long int ulBytes,
unsigned long int ulFileSize)
{
// To calculate percent, multiply ulBytes by 100, then divide
// the product by ulFileSize
unsigned long int ulDividend = 0, ulPercent = 0;
if (ulFileSize != 200 && ulBytes > 0)
{
ulDividend = ulBytes * 100;
ulPercent = ulDividend / ulFileSize;
}
if (ulBytes == 0)
{
// We've started the download/upload
char szFormat[512] = "%lu of %lu bytes transferred.";
char szInfo[512];
sprintf(szInfo, szFormat, ulBytes, ulFileSize);
m_ctlBytes.SetWindowText(szInfo);
SetPercent(0);
return;
}
// If the file size is 200, that means Internet Navigator was unable
// to determine the file's size on the server or on the local machine.
if (ulFileSize != 200)
{
char szFormat[512] = "%lu of %lu bytes transferred.";
char szInfo[512];
sprintf(szInfo, szFormat, ulBytes, ulFileSize);
TRACE2("%lu of %lu bytes transferred.\r\n", ulBytes, ulFileSize);
m_ctlBytes.SetWindowText(szInfo);
if (ulPercent > 100)
ulPercent = 100;
SetPercent(ulPercent);
}
else
{
// If the file size given is 200, we assume that the
// file size is in error, and therefore inaccurate. In this
// case we just will show the user the number of bytes transferred.
char szFormat[512] = "%lu bytes transferred.";
char szInfo[512];
sprintf(szInfo, szFormat, ulBytes);
TRACE1("%lu bytes transferred.\r\n", ulBytes);
m_ctlBytes.SetWindowText(szInfo);
// Since we don't know when 100% of the file has been downloaded,
// always set the percent to 0; the purpose of this dialog box then
// is to just show the user the path of the file and how many bytes
// have been transferred so far.
ulPercent = 0;
SetPercent(0);
}
return;
}
/////////////////////////////////////////////////////////////////////////////
// CFTPercentDialog message handlers
BOOL CFTPercentDialog::OnInitDialog()
{
CDialog::OnInitDialog();
CWnd *pWnd, *pBarWnd;
CRect Rect;
// Set the window caption text.
SetWindowText("File Transfer Progress");
// Get the coordinates for the percentage bar.
pBarWnd = GetDlgItem (IDC_BAR);
pBarWnd->GetWindowRect (Rect);
ScreenToClient (Rect);
BARX = Rect.left;
BARY = Rect.top;
BARWIDTH = Rect.Width ();
BARHEIGHT = Rect.Height ();
mBarRect.SetRect (BARX + 1, BARY + 1, BARX + BARWIDTH, BARY + BARHEIGHT);
return TRUE; // return TRUE unless you set the focus to a control
}
void CFTPercentDialog::OnPaint()
{
CPaintDC PaintDC(this); // device context for painting
CDC MemDC;
CBitmap Bitmap, *pOldBitmap;
CPen GrayPen(PS_SOLID, 1, ::GetSysColor(COLOR_BTNSHADOW));
CBrush GrayBrush(::GetSysColor(COLOR_BTNFACE));
CPen WhitePen(PS_SOLID, 1, ::GetSysColor(COLOR_BTNHIGHLIGHT));
int Width;
unsigned long int ulWidth;
CRect Rect;
char Junk[10];
TEXTMETRIC TextMetric;
// Create a device context to work on.
MemDC.CreateCompatibleDC(&PaintDC);
// Create the brush to draw the bar and set the text color
CBrush Brush(RGB(0,0,128));
MemDC.SetTextColor(RGB(255,255,255));
// Draw the inset rectangle for the bar.
CPen* pOldPen = PaintDC.SelectObject(&GrayPen);
PaintDC.MoveTo(BARX, BARY);
PaintDC.LineTo(BARX + BARWIDTH, BARY);
PaintDC.MoveTo(BARX, BARY);
PaintDC.LineTo(BARX, BARY + BARHEIGHT);
PaintDC.SelectObject(pOldPen);
pOldPen = PaintDC.SelectObject(&WhitePen);
PaintDC.MoveTo(BARX + BARWIDTH, BARY + BARHEIGHT);
PaintDC.LineTo(BARX + BARWIDTH, BARY);
PaintDC.MoveTo(BARX + BARWIDTH, BARY + BARHEIGHT);
PaintDC.LineTo(BARX, BARY + BARHEIGHT);
PaintDC.SelectObject(pOldPen);
// Create the bitmap to draw the bar on.
Bitmap.CreateCompatibleBitmap(&PaintDC, BARWIDTH - 1, BARHEIGHT - 1);
pOldBitmap =(CBitmap *) MemDC.SelectObject(&Bitmap);
// Draw the background for the bar.
Rect.SetRect(0, 0, BARWIDTH - 1, BARHEIGHT - 1);
MemDC.FillRect(Rect, &GrayBrush);
// Draw the percentange bar.
ulWidth = m_ulCurrentPercent * (BARWIDTH - 1) / 100;
Rect.SetRect(0,0, (unsigned long int)(int)ulWidth, BARHEIGHT - 1);
MemDC.FillRect(Rect, &Brush);
// Display the percentage as text.
MemDC.SetBkMode(TRANSPARENT);
MemDC.GetTextMetrics(&TextMetric);
wsprintf(Junk, "%lu%%", m_ulCurrentPercent);
MemDC.TextOut(BARWIDTH / 2 - (TextMetric.tmAveCharWidth * lstrlen (Junk) / 2), BARHEIGHT / 2 - (TextMetric.tmHeight / 2), (LPCSTR) Junk, lstrlen (Junk));
// Blt the bar to the dialog.
PaintDC.BitBlt(BARX + 1, BARY + 1, BARWIDTH - 1, BARHEIGHT - 1, &MemDC, 0, 0, SRCCOPY);
// Clean up.
MemDC.SelectObject(pOldBitmap);
Bitmap.DeleteObject();
Brush.DeleteObject();
GrayBrush.DeleteObject();
GrayPen.DeleteObject();
WhitePen.DeleteObject();
}
void CFTPercentDialog::OnCancelCall()
{
// Send the WM_COMMAND message, wParam == ID_CANCEL_CALL, to the
// CFtpView via our m_pView pointer
ShowWindow(SW_HIDE);
UpdateWindow();
if (m_pView)
{
m_pView->GetParentFrame()->UpdateWindow();
m_pView->PostMessage(WM_COMMAND, ID_CANCEL_CALL, 0L);
return;
}
DestroyWindow();
return;
}
void CFTPercentDialog::OnMove(int x, int y)
{
CDialog::OnMove(x, y);
return;
}
void CFTPercentDialog::PostNcDestroy()
{
ASSERT(m_hWnd == NULL);
delete this; // remove this object from memory
}
void CFTPercentDialog::OnCancel()
{
// Close this window properly
ShowWindow(SW_HIDE);
UpdateWindow();
DestroyWindow();
return;
}
void CFTPercentDialog::OnOK()
{
// Close this window properly
ShowWindow(SW_HIDE);
UpdateWindow();
DestroyWindow();
return;
}
void CFTPercentDialog::OnSysCommand(UINT nID, LPARAM lParam)
{
// This function deals with commands chosen from the Control menu
// If the user chooses the Close command, we need to know about it
// so we can close this window properly
switch (nID & 0xFFF0)
{
case SC_CLOSE:
// Destroy this window
ShowWindow(SW_HIDE);
UpdateWindow();
DestroyWindow();
break;
case SC_SIZE:
case SC_MOVE:
case SC_MINIMIZE:
case SC_MAXIMIZE:
case SC_NEXTWINDOW:
case SC_PREVWINDOW:
case SC_RESTORE:
case SC_TASKLIST:
CWnd::OnSysCommand(nID, lParam);
break;
default:
CWnd::OnSysCommand(nID, lParam);
break;
}
}