11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 5. Common Controls<br />

Custom Resource<br />

The AVI data can be included in the application as a resource. However, Developer Studio does not<br />

support this kind of resource directly. So we have to treat it as a custom resource. We can create AVI<br />

resource from a “*.avi” file through following steps: 1) Execute Insert | Resource command, then click<br />

“Import” button from the “Insert resource” dialog box. 2) From the popped up “File open” dialog box,<br />

browse and select a “*.avi” file and open it (we can use “5.17\CCtl\search.avi” or any other “*.avi” file for<br />

this purpose). 3) When we are asked to provide the resource type, input “AVI”. 4) Name the resource ID as<br />

IDR_AVI.<br />

Sample Implementation<br />

In the dialog box’s initialization stage, we need to initialize the animate control, progress control and<br />

set timer as follows:<br />

……<br />

BOOL CCCtlDlg::OnInitDialog()<br />

{<br />

}<br />

ASSERT(m_animateCtrl.Open(IDR_AVI));<br />

ASSERT(m_animateCtrl.Play(0, -1, -1));<br />

m_progressCtrl.SetRange(0, 50);<br />

m_progressCtrl.SetStep(2);<br />

SetTimer(PROGRESS_CTRL, 500, NULL);<br />

return TRUE;<br />

First, function CAnimateCtrl::Open(…) is called to open the animation resource, then function<br />

CAnimateCtrl::Play(…) is called to play the AVI data. When doing this, we pass 0 to its first parameter<br />

and -1 to the second parameter, this will let the animation be played from the first frame to the last frame.<br />

The third parameter is also -1, this means the animation will be played again and again without being<br />

stopped.<br />

Then we initialize the range of progress control from 0 to 50, incremental step 2, and a timer with time<br />

out period of 500 milliseconds is set.<br />

Message WM_TIMER can be handled by adding message handlers, this can be easily implemented<br />

through using Class Wizard. In the sample, this member function is implemented as follows:<br />

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

{<br />

if(nIDEvent == PROGRESS_CTRL)<br />

{<br />

m_progressCtrl.StepIt();<br />

}<br />

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

}<br />

The only parameter of this function is nIDEvent, which indicates the ID of the timer that has just timed<br />

out. If we have two or more timers set within the same window, by examining this ID we know which<br />

timer has timed out. In the sample, when timer times out, we simply call function<br />

CProgressCtrl::StepIt() to advance the progress bar one step forward.<br />

Summary:<br />

1) A spin control must work together with another control, which is called the “Buddy” of the spin<br />

control. Usually the “Buddy” is an edit box, but it could be any other types of controls such as button<br />

or static control.<br />

2) To set buddy automatically, we must make sure that the buddy window is the previous window of the<br />

spin control in Z-order.<br />

1) The buddy can also be set by calling function CSpinButtonCtrl::SetBuddy(…).<br />

139

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

Saved successfully!

Ooh no, something went wrong!