-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgDlg.cpp
220 lines (179 loc) · 5.31 KB
/
ProgDlg.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
// ProgDlg.cpp : implementation file
// CG: This file was added by the Progress Dialog component
#include "stdafx.h"
#include "resource.h"
#include "ProgDlg.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CProgressDlg dialog
CProgressDlg::CProgressDlg(UINT nCaptionID)
{
m_nCaptionID = CG_IDS_PROGRESS_CAPTION;
if (nCaptionID != 0)
m_nCaptionID = nCaptionID;
m_bCancel=FALSE;
m_nLower=0;
m_nUpper=100;
m_nStep=10;
//{{AFX_DATA_INIT(CProgressDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_bParentDisabled = FALSE;
}
CProgressDlg::~CProgressDlg()
{
if(m_hWnd!=NULL)
DestroyWindow();
}
BOOL CProgressDlg::DestroyWindow()
{
ReEnableParent();
return CDialog::DestroyWindow();
}
void CProgressDlg::ReEnableParent()
{
if(m_bParentDisabled && (m_pParentWnd!=NULL))
m_pParentWnd->EnableWindow(TRUE);
m_bParentDisabled=FALSE;
}
BOOL CProgressDlg::Create(CWnd *pParent)
{
// Get the true parent of the dialog
m_pParentWnd = CWnd::GetSafeOwner(pParent);
// m_bParentDisabled is used to re-enable the parent window
// when the dialog is destroyed. So we don't want to set
// it to TRUE unless the parent was already enabled.
if((m_pParentWnd!=NULL) && m_pParentWnd->IsWindowEnabled())
{
m_pParentWnd->EnableWindow(FALSE);
m_bParentDisabled = TRUE;
}
if(!CDialog::Create(CProgressDlg::IDD,pParent))
{
ReEnableParent();
return FALSE;
}
return TRUE;
}
void CProgressDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CProgressDlg)
DDX_Control(pDX, CG_IDC_PROGDLG_PROGRESS, m_Progress);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CProgressDlg, CDialog)
//{{AFX_MSG_MAP(CProgressDlg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void CProgressDlg::SetStatus(LPCTSTR lpszMessage)
{
ASSERT(m_hWnd); // Don't call this _before_ the dialog has
// been created. Can be called from OnInitDialog
CWnd *pWndStatus = GetDlgItem(CG_IDC_PROGDLG_STATUS);
// Verify that the static text control exists
ASSERT(pWndStatus!=NULL);
pWndStatus->SetWindowText(lpszMessage);
}
void CProgressDlg::OnCancel()
{
m_bCancel=TRUE;
}
void CProgressDlg::SetRange(int nLower,int nUpper)
{
m_nLower = nLower;
m_nUpper = nUpper;
m_Progress.SetRange(nLower,nUpper);
}
int CProgressDlg::SetPos(int nPos)
{
PumpMessages();
int iResult = m_Progress.SetPos(nPos);
UpdatePercent(nPos);
return iResult;
}
int CProgressDlg::SetStep(int nStep)
{
m_nStep = nStep; // Store for later use in calculating percentage
return m_Progress.SetStep(nStep);
}
int CProgressDlg::OffsetPos(int nPos)
{
PumpMessages();
int iResult = m_Progress.OffsetPos(nPos);
UpdatePercent(iResult+nPos);
return iResult;
}
int CProgressDlg::StepIt()
{
PumpMessages();
int iResult = m_Progress.StepIt();
UpdatePercent(iResult+m_nStep);
return iResult;
}
void CProgressDlg::PumpMessages()
{
// Must call Create() before using the dialog
ASSERT(m_hWnd!=NULL);
MSG msg;
// Handle dialog messages
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(!IsDialogMessage(&msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
BOOL CProgressDlg::CheckCancelButton()
{
// Process all pending messages
PumpMessages();
// Reset m_bCancel to FALSE so that
// CheckCancelButton returns FALSE until the user
// clicks Cancel again. This will allow you to call
// CheckCancelButton and still continue the operation.
// If m_bCancel stayed TRUE, then the next call to
// CheckCancelButton would always return TRUE
BOOL bResult = m_bCancel;
m_bCancel = FALSE;
return bResult;
}
void CProgressDlg::UpdatePercent(int nNewPos)
{
CWnd *pWndPercent = GetDlgItem(CG_IDC_PROGDLG_PERCENT);
int nPercent;
int nDivisor = m_nUpper - m_nLower;
ASSERT(nDivisor>0); // m_nLower should be smaller than m_nUpper
int nDividend = (nNewPos - m_nLower);
ASSERT(nDividend>=0); // Current position should be greater than m_nLower
nPercent = nDividend * 100 / nDivisor;
// Since the Progress Control wraps, we will wrap the percentage
// along with it. However, don't reset 100% back to 0%
if(nPercent!=100)
nPercent %= 100;
// Display the percentage
CString strBuf;
strBuf.Format(_T("%d%c"),nPercent,_T('%'));
CString strCur; // get current percentage
pWndPercent->GetWindowText(strCur);
if (strCur != strBuf)
pWndPercent->SetWindowText(strBuf);
}
/////////////////////////////////////////////////////////////////////////////
// CProgressDlg message handlers
BOOL CProgressDlg::OnInitDialog()
{
CDialog::OnInitDialog();
m_Progress.SetRange(m_nLower,m_nUpper);
m_Progress.SetStep(m_nStep);
m_Progress.SetPos(m_nLower);
CString strCaption;
VERIFY(strCaption.LoadString(m_nCaptionID));
SetWindowText(strCaption);
return TRUE;
}