· To finish our ActiveX control, add the method, Beep() to our control. Open ClassWizard,
Automation tab and select Add Method. Name the method Beep and make its return
type void. Click OK and close the ClassWizard. Add the code to make the computer
beep, using the MFC MessageBeep() method.
void CBeepCtrl::Beep()
{
// TODO: Add your dispatch handler code here
MessageBeep(MB_OK);
}
· Now build Beep to create Beep.ocx. This also registers that control with Windows.
· Create a new dialog-based program with AppWizard now, naming this program Beeper.
· Add your Beep control to the dialog editor’s toolbox using the Project menu (Add to
Projects, Components and Controls Gallery). Open the Registered Controls folder, find
Beep and Insert. Accept default name CBeep and close. You will now have an OCX
control in your toolbox called Beep. Insert on your form, add an edit box to accept user
input and a label captioned “Enter an integer between 1 and 10, then click to hear that
number of beeps.”
· Use ClassWizard to connect a member variable, m_beeper, to the new Beep control and
a variable called counter of type int to the textbox. Set the minimum and maximum
values to 0 and 10. Now we can use the Beep() method using m_beeper.Beep().
· Use ClassWizard to add an event-handler to the Beep control’s Click event. In Message
Maps select IDC_BEEPCTRL1 and double click Click to add the new event handler
OnClickBeepctrl1().
· Add code to OnClickBeepctrl1() to get number from the edit box and call the Beep
method. Use a pre-test loop to call Beep() the asked for number of times.
void CbeeperDlg::OnClickBeepctrl1()
{
UpdateData(true);
while (counter > 0)
{
//A pre-test loop based on a
//counter condition.
m_beeper.Beep();
counter--; //Changes loop condition to
//terminate loop
}
}
No comments:
Post a Comment