11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 9. Font<br />

Frame border<br />

is gray<br />

Figure 9-4. Set frame border to gray<br />

Variable m_perBar will be used to implement percentange bar, and m_nPercent will be used to record<br />

the current position of the percentage bar.<br />

Variable m_nPercent is initialized in the constructor:<br />

CProgDlg::CProgDlg(CWnd* pParent /*=NULL*/)<br />

: CDialog(CProgDlg::IDD, pParent)<br />

{<br />

//{{AFX_DATA_INIT(CProgDlg)<br />

//}}AFX_DATA_INIT<br />

m_nPercent=0;<br />

}<br />

In the sample, WM_INITDIALOG message handler is added to the application through using Class Wizard,<br />

and funtion CProgDlg::OnInitDialog() is implemented as follows:<br />

BOOL CProgDlg::OnInitDialog()<br />

{<br />

CDialog::OnInitDialog();<br />

m_perBar.SubclassDlgItem(IDC_STATIC_PROG, this);<br />

SetTimer(TIMER_ID, 100, NULL);<br />

return TRUE;<br />

}<br />

Control IDC_STATIC_PROG is changed to a progress bar through implementing subclass, then a timer is<br />

started to generate events that will be handled to advance the percentage bar.<br />

To handle time out events, in the sample, a WM_TIMER message handler is added throgh using Class<br />

Wizard. The corresponding member function CProgDlg::OnTimer(…) is implemented as follows:<br />

void CProgDlg::OnTimer(UINT nIDEvent)<br />

{<br />

if(nIDEvent == TIMER_ID)<br />

{<br />

m_perBar.SetPercentage(m_nPercent++);<br />

if(m_nPercent == 100)m_nPercent=0;<br />

}<br />

CDialog::OnTimer(nIDEvent);<br />

}<br />

If timer times out, we advance the percentage bar one step forward (1%); if the percentage bar reaches<br />

100%, we reset it to 0%.<br />

We must destroy timer when the application exits. The best place of doing this is when we receive<br />

WM_DESTROY message. This message handler can also be added through using Class Wizard. In the sample,<br />

the corresponding member function is implemented as follows:<br />

void CProgDlg::OnDestroy()<br />

{<br />

CDialog::OnDestroy();<br />

KillTimer(TIMER_ID);<br />

}<br />

For the purpose of testing the percentage bar, a new command Dialog | Progress is added to the<br />

application, whose command ID is ID_DIALOG_PROGRESS. A WM_COMMAND message handler is added to class<br />

249

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!