Export (0) Print
Expand All

Lab 6: Adding ActiveX Controls in InfoPath 2003

Office 2003

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

Summary: Learn how to write and deploy a custom Microsoft ActiveX control that meets all requirements for use in Microsoft Office InfoPath 2003. Discover how to add the control to the InfoPath Controls task pane, and how to insert it into a form. (9 printed pages)

Microsoft Corporation

April 2004

Updated August 2007

Applies to: Microsoft Office InfoPath 2003

Contents

Download the odc_INF03_Labs.exe sample file.

  • Basic understanding of Microsoft Visual C++.

  • Familiarity with the Microsoft Visual Studio IDE.

At Contoso Corporation, sales representatives use a series of related InfoPath forms to collect sales information. Many of these forms include a text box that must be incremented by one each time a drug is sold. Currently, the text box must be updated by clicking into the text box and then typing a number. Sales representatives can save time if they just clicked the control to increment it.

As a form designer in the IT department, you can create a custom Microsoft ActiveX control that would exhibit this custom behavior. Creating and distributing your own custom ActiveX control can save time and efforts for other form designers.

In this lab, learn how to perform the following tasks:

  • Create an ActiveX control that can be used in InfoPath.

  • Add the ActiveX control to the Controls task pane.

  • Insert the ActiveX control into your form.

  • Install and register the ActiveX controls on users' computers.

Before you start this lab, install Microsoft Visual Studio .NET 2003. In addition, you must obtain a code signing certificate from a trusted certification authority (CA).

Exercise 1: Create an ActiveX Control

Contoso sales representatives have to keep track of the number of drugs sold. Updating a text box by typing can be cumbersome and prone to error, when you consider that sales representatives have to increment by one many times over. An ActiveX control that self-increments on a mouse click reduces time spent and reduce errors.

To create an ActiveX control

  1. Open Visual Studio.

  2. To create a project, on the File menu, click New, and then click Project.

  3. Select Visual C++ Project as the project type, and ATL Project as the template.

  4. Type ActiveX for the name, and select a location in which to save the source.

    The ATL Project Wizard starts.

  5. Click Application Settings.

  6. Clear the Attributed text box. Leave the rest of settings as they are.

    NoteNote

    InfoPath ActiveX controls can also be attributed ATL controls, but this lab uses a non-attributed control.

    Visual Studio creates an empty ATL project where we can add empty ActiveX controls.

  7. Right-click the ActiveX project, click Add, and then click Add Class.

    Figure 1. Adding a class to the project

    Adding a class to the project
  8. In the Add Class dialog box, click the ATL Control, and then click Open.

    The ATL Control Wizard generates the appropriate code. This helps simplify your coding.

    On the first pageof the wizard, in the Short Name text box, type SampleControl.

  9. Click Options. Select the Connection Points check box.

  10. Click Interfaces.

  11. In the Not Supported list, double-click IPropertyNotifySink.

    This moves the interface over to the Supported list. The IPropertyNotifySink interface is important because it is the method that the ActiveX control uses to notify InfoPath to get a new value from the control.

  12. Click Stock Properties.

  13. In the Not Supported list, double-click Enabled.

    This moves this property to the Supported list. The Enabled property is necessary for InfoPath to disable the ActiveX control (when necessary, conditional formatting, after form has been digitally signed, and so on).

  14. Click Finish.

ActiveX controls that are used in InfoPath have restrictions that are stricter than ActiveX controls used in Microsoft Internet Explorer. For example, InfoPath requires that ActiveX controls be marked as both safe for scripting and safe for initialization. If you write custom ActiveX controls for use in forms, you must implement the IObjectSafety interface so that InfoPath recognizes that a particular control is marked as safe for scripting and safe for initialization.

To mark the control as safe for scripting and initialization using the IObjectSafety interface

  1. In Solution Explorer, open the SampleControl.h file from the Header Files folder.

  2. At the beginning of the class definition, find the line that reads:

    class ATL_NO_VTABLE CsampleControl
    
    

    Then add this line after the colon(:) with the list of other interfaces. This should be around line 12.

    public IObjectSafetyImpl<CSampleControl, 
        INTERFACESAFE_FOR_UNTRUSTED_DATA | 
        INTERFACESAFE_FOR_UNTRUSTED_CALLER>,
    
    
  3. Add this line in the COM Interface Map:

    COM_INTERFACE_ENTRY(IObjectSafety)
    
    
  4. You do not need to write any code for the IObjectSafety interface because the steps that were completed use the stub implementation.

InfoPath interacts with ActiveX controls in a form by using binding and enabled properties. InfoPath uses the binding property to store and retrieve data, and the enabled property to either enable or disable a control.

Caution   ActiveX controls that implement IObjectSafety to mark the control as safe for scripting and initialization will also work in Microsoft Internet Explorer without prompting the user, even if the Web page containing the control is located in the Internet Zone. For more information about ActiveX control security, see Designing Secure ActiveX Controls.

To specify properties for an ActiveX control

  1. Switch Solution Explorer to Class View by using the tabs in the bottom of the window or from the View menu.

  2. Right-click ISampleControl.

  3. On the Add menu, click Add Property.

    When the project was created, you added the Enabled stock property so that you only need a binding property.

  4. Set the Property Type to LONG and the Property Name to Value.

    NoteNote

    You do not have to name the property Value, but it is helpful to do this because InfoPath automatically recognizes properties with this name as the binding property for the ActiveX control.

  5. Click Finish.

You have to keep track of the current count. Start by creating a variable that stores the running total.

To add functionality to the control

  1. Locate the class definition (in the SampleControl.h file) and after the opening curly brace({) and the public declaration, add the following.

    long m_nCurrentValue;
    
    
  2. Also, you should initialize it to zero, so in the class constructor, add the following.

    m_nCurrentValue=0;
    
    
  3. Locate the OnDraw method in the SampleControl.h file. There are two lines that create the pszText variable, followed by a TextOut method call. Replace those two lines with the following lines.

    WCHAR    *pwszDisplayString = new WCHAR[MAX_CONTROL_TEXT];
            wsprintfW(pwszDisplayString,L"Value: %d",m_nCurrentValue);
            TextOutW(di.hdcDraw, 
                (rc.left + rc.right) / 2, 
                ((rc.top + rc.bottom) / 2), 
                pwszDisplayString, 
                lstrlenW(pwszDisplayString));    
            delete[MAX_CONTROL_TEXT] pwszDisplayString;
    
    

    These lines make the control draw the current value of the control instead of drawing the static string "ATL Control 7.0".

  4. The drawing code refers to a MAX_CONTROL_TEXT constant, which you should specify at the beginning of the code (just after the includes) in SampleControl.h.

    const int MAX_CONTROL_TEXT = 100;    
    
  5. In the Properties window, change the property grid to show the windows messages, as shown in Figure 2.

    Figure 2. Changing the property grid to show the window messages

    Changing property grid to show window messages
  6. Select the WM_RBUTTONUP item and click the drop-down list.

  7. Select <Add> OnRButtonUp.

  8. Repeat these steps for WM_LBUTTONUP.

  9. Now that the stubs for these two methods are created, write the code to handle clicks and increment or decrement the current count.

    LRESULT CSampleControl::OnLButtonUp(UINT /*uMsg*/, WPARAM /*wParam*/, 
        LPARAM /*lParam*/, BOOL& /*bHandled*/)
    {
            m_nCurrentValue++;
            FireViewChange();
            FireOnChanged(1);
    
            return 0;
    }
    
    LRESULT CSampleControl::OnRButtonUp(UINT /*uMsg*/, WPARAM /*wParam*/, 
        LPARAM /*lParam*/, BOOL& /*bHandled*/)
    {
            m_nCurrentValue--;
            FireViewChange();
            FireOnChanged(1);
    
            return 0;
    }
    
    
  10. Finally, you must expose the m_nCurrentValue variable to the Value property. You do this by writing get_Value and put_Value as follows.

    
    STDMETHODIMP CSampleControl::get_Value(LONG* pVal)
    {
        *pVal = m_nCurrentValue;
    
        return S_OK;
    }
    
    STDMETHODIMP CSampleControl::put_Value(LONG newVal)
    {
        m_nCurrentValue = newVal;
    
        return S_OK;
    }
    

At this point, the control does not do anything functionally useful. Here we add the functionality to the control and give it some rendering functionality, and also the ability to react to user clicks.

To compile the ActiveX control

  • On the Build menu, click Build Solution.

Exercise 2: Making ActiveX Controls Available to Users

If you use a custom ActiveX control in a form, you must ensure that the control is installed and registered on users' computers. If you do not do this, users will be unable to open the form. You can use InfoPath to package an installation .cab file with your form template. This .cab file installs the ActiveX control (with the user's permission) and registers it for use. The following exercise shows you how to create this file.

Obtaining the necessary command line tools

Certain command line tools are necessary for signing the package file. If you do not have the following files, you can download them from the Web. The following tools are included with the Internet Development Software Development Kit (SDK) and the .NET Framework SDK. If you already have these SDKs installed on your computer, you do not have to download anything extra.

If you do not have a code signing certificate from a certification authority, you have to obtain a test certificate.

To obtain a test certificate

  1. Open a Command Prompt window.

  2. The makecert.exe command line tool can generate a test certificate for testing. At the command prompt, type makecert newCert.cer –sv privatekey.pvk.

  3. When you are prompted for a password, click None.

  4. Because this certificate is only good for testing, your computer does not have the necessary certification authority (CA) certificate root on your computer. To turn this on, at the command prompt, type setreg 1 true.

Now that you have a certificate installed on your computer, you have to prepare it for signing. If your certificate file is a .cer file, you have to generate a .spc file (if you obtained the certificate as described earlier, you have a newCert.cer file).

To prepare your certificate for signing

  1. Open a Command Prompt window.

  2. An .spc file can be created by running the cert2spc.exe command line tool on your .cer file. At the command prompt, type Cert2Spc newCert.cer output.spc.

You are ready to sign the ActiveX control.

To create a redistributable CAB file and digitally signing the CAB file

  1. In your existing Visual Studio solution, add a Setup project. On the File menu, point to New, and then click Project.

  2. Name your project Cab1.

  3. Click Add to Solution.

  4. For the Project Types, select Setup and Deployment Projects.

  5. For the Template, select Cab Project, and then click OK.

  6. In Solution Explorer, right-click the cab project (this should be a node at the bottom of Solution Explorer), point to Add, and click Project Output.

    Figure 3. Project Output menu item

    Project Output menu item
  7. Select the Primary Output, and then click OK.

  8. Right-click the Setup project and then click Properties.

  9. Select Authenticode signing.

  10. To populate the Certificate File, click Browse and select the .spc file that you created.

  11. To populate Private Key File, click Browse and select the .pvk file that you created.

  12. The timestamp server URL is optional, but if you have an Internet connection, you should specify this URL: http://timestamp.verisign.com/scripts/timstamp.dll.

  13. Click OK.

  14. On the Build menu, click Rebuild Solution.

Exercise 3: Add ActiveX Control to InfoPath(H3)

Now that the ActiveX control is complete and you have packaged it, it is ready to be used in InfoPath. In the following exercise, you learn how to add the control to the Controls task pane.

To add the ActiveX control to the Controls task pane

  1. Create a blank form.

  2. On the Insert menu, click More Controls.

  3. At the bottom of the Controls task pane, click Add or Remove Custom Controls.

  4. In the Add or Remove Custom Controls dialog box, click Add.

  5. On the first page of the Add Custom Control Wizard, in the Select a control list, click the ActiveX control that you created (SampleControl Class), and then click Next.

  6. On the next page, leave Don't include a .cab file selected. If you want to deploy the form to other users, use the setup cab that you created earlier.

  7. To continue, click Next.

  8. In the Binding property list, click the Value property.

    The ActiveX control uses this binding property to receive and store XML data.

  9. Click Next.

  10. In the Enable or Disable property list, click Enabled, and then make sure that the value is set to true.

    This step is necessary for InfoPath forms that use digital signatures or conditional formatting.

  11. Click Next.

    You must now specify binding options for the ActiveX control. Although there are three types of binding options, for this lab, you bind the control to a field with a simple data type. This data type is appropriate when the ActiveX control interacts with single values. By default, Text (string) is selected in the Select one or more data types list, and it also appears as the default data type.

  12. Change both to Whole Number (integer) because your control outputs numbers. InfoPath uses the default data type when you insert the ActiveX control into your form from the Controls task pane.

    Figure 4. Add Custom Controls Wizard

    Add Custom Controls Wizard

Now that you have added the ActiveX control to the Controls task pane, you can add it to your form.

To add the ActiveX control to your form

  1. Drag the newly-added control onto the form.

  2. Save the form and open it in the editor.

    Your ActiveX control now appears in your InfoPath form. Clicking it with the left mouse button increments the value in the box. Clicking it with the right mouse button decrements the value.

In these exercises, you learned how to write a custom ActiveX control that meets all requirements for use in InfoPath. You also learned how to deploy the control to users' computers, how to add the control to the InfoPath Controls task pane, and how to insert it into the form.

Show:
© 2014 Microsoft