Lab 11: Using an ActiveX Control in an Application

In Lab 7, you saw how the STUpload application uses two ActiveX controls—the Microsoft ADO Data Control and the Microsoft DataGrid Control—as part of its user interface. You are not going to implement any additional ActiveX controls for the STUpload application. Instead, you will learn how to use the OneArmedBandit control that you created in this chapter as part of a Windows application. This application, named VirtualSlot, is shown in Figure 11.8.

click to view at full size.

Figure 11.8 The VirtualSlot application

This application calls the Play() method exposed by the OneArmedBandit control, and handles the Jackpot event. While designing the application, you will use the property page to set the number of symbols displayed and the control colors.

If you have not completed the exercises in this chapter, you can use the OneArmedBandit control located in the Chapter 11\Lab folder. Take the fol- lowing steps to register this control on your computer:

  1. Copy the OneArmedBandit.ocx file to a suitable location on your hard drive.
  2. From the Start menu, open a command prompt and change to the directory that contains the OneArmedBandit.ocx file.
  3. Enter the following command to register the control on your computer:
  4. regsvr32 onearmedbandit.ocx

You are now ready to create the VirtualSlot application.

  • To create the VirtualSlot project
    1. In Visual C++, choose New from the File menu.
    2. Select the MFC AppWizard (exe) to create a new project named VirtualSlot. Click OK.
    3. In Step 1 of the AppWizard, click Dialog Based and then click Finish.
    4. Click OK to confirm the selection.

    The IDD_VIRTUALSLOT_DIALOG dialog template appears in the dialog editor. You will modify this to create the VirtualSlot application interface, which will consist of an instance of the OneArmedBandit control, a Play button, and a Close button.

  • To modify the IDD_VIRTUALSLOT_DIALOG dialog template
    1. Delete the //TODO static text.
    2. Change the OK button caption to Close. Assign a new ID, ID_PLAY, to the Cancel button and change the caption to Play.
    3. Rearrange the buttons as shown in Figure 11.8.

  • To insert the OneArmedBandit control into the project
    1. On the Project menu, click Add to Project, and then click Components and Controls.
    2. In the Components and Controls Gallery, double-click the Registered ActiveX Controls folder to display all ActiveX controls registered on your system.
    3. Click the OneArmedBandit Control, and then click Insert.
    4. Click OK to insert the component. Click OK to confirm that you want to generate the COneArmedBandit class for the control.
    5. Click Close to close the Components and Controls Gallery.

    Note that the generic OCX icon for the OneArmedBandit control is placed on the Controls toolbar. The MFC OneArmedBandit project you created in this chapter contained a bitmap resource IDB_ONEARMEDBANDIT that you could have modified to create a distinctive toolbar button.

    In ClassView, you see the COneArmedBandit class that has been created by the Components and Controls Gallery. This class contains wrapper functions that provide access to your control properties and methods. The Create() function allows you to dynamically create an instance of the control (within a CView object for example).

  • To place an instance of the COneArmedBandit control
    1. Click the OneArmedBandit button on the Controls toolbar and place the control on the dialog box. Resize the control and the dialog box so that they appear as shown in Figure 11.8.
    2. With the OneArmedBandit control selected, press ENTER to edit the control properties. Your familiar property pages will appear, along with the General and All pages that are implemented by the container.
    3. On the Control page, change the Number of Symbols to 7.
    4. On the Colors page, select colors for your ForeColor and BackColor properties.

    You will now modify the CVirtualSlotDlg dialog class to implement the Play button and handle the Jackpot event.

  • To implement the Play button
    1. Press CTRL+W to open ClassWizard. On the Member Variables tab, click the IDC_ONEARMEDBANDIT1 control ID. Click Add Variable to create a Control member variable of type COneArmedBandit, named m_bandit.
    2. On the Message Maps page, click the ID_PLAY object ID. Add a handler for the BN_CLICKED message named OnPlay().
    3. Click Edit Code. In the OnPlay() function implementation, replace the //TODO comment with the following line of code:
    4. m_bandit.Play();

  • To handle the Jackpot event
    1. On the Message Maps tab of ClassWizard, simply click the IDC_ ONEARMEDBANDIT1 control ID.
    2. Click the Jackpot message, and then click Add Function to create the OnJackpotOnearmedbanditctrl1() function.
    3. Click Edit Code. In the function implementation, replace the //TODO comment with the following line of code:
    4. AfxMessageBox("Jackpot!");

    5. You can now build and run the VirtualSlot application. Click the Play button to spin the one-armed bandit. Keep playing until you hit the jackpot!


    Microsoft Press - Desktop Applications with Microsoft Visual C++ 6. 0. MCSD Training Kit
    Desktop Applications with Microsoft Visual C++ 6.0 MCSD Training Kit
    ISBN: 0735607958
    EAN: 2147483647
    Year: 1999
    Pages: 95

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net