Select the com components tab as shown in figure 19 1

Info icon This preview shows pages 732–735. Sign up to view the full content.

View Full Document Right Arrow Icon
the Choose Toolbox Items dialog box. Select the COM Components tab, as shown in Figure 19-1 . You can select any number of controls—here we’re just selecting the Adobe PDF Reader. Figure 19-1. Adding a COM component 708 | Chapter 19: Interop with COM and Win32
Image of page 732

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
When you click OK, you should see the component in the new tab in the Toolbox, as Figure 19-2 shows. Figure 19-2. ActiveX control in the toolbox Now you can drag this control onto your form. Figure 19-3 shows how the control looks on a form in the designer. You can set its size and position just like you would any normal control. Windows Forms layout concepts like anchoring work too—we could anchor the control to all four sides of the window to make it resize as the window resizes. Figure 19-3. Form with PDF ActiveX control Left to its own devices, this particular control won’t do anything—we have to give it a PDF file to load before it will even show a UI, and that means using the control’s API. Fortunately, one of the things Visual Studio did for us when we dragged the control onto the form was to import the component’s type library . A COM type library contains metadata—it lists the available classes, and describes their methods, properties, and events. This is similar in concept to the .NET metadata we explored in Chapter 17 , but the details are all very different. Fortunately, the differences are not a problem, because when Visual Studio imported the type library, it generated a DLL containing the same information but as a .NET component. This makes it possible to use the PDF compo- nent from C#. You can see this generated library by expanding the References section of the Solution Explorer, as Figure 19-4 shows. Importing ActiveX Controls | 709
Image of page 733
AcroPDFLib is the imported type library. You’ll see a DLL like this when you import any kind of COM component into a .NET project. But there’s a second item there, AxAcroPDFLib , and it is specific to ActiveX controls. (Not all COM components are designed for UI use.) This ActiveX-specific generated DLL contains a class that derives from a special base class called AxHost , which is a Windows Forms control that can host an ActiveX control. Visual Studio generates a class called AxAcroPDF that derives from AxHost , and puts it in that AxAcroPDFLib . It’s this AxAcroPDF class that has ended up on our form. This ActiveX wrapper provides .NET-callable versions of all the meth- ods the control makes available to COM programs. So the upshot is that we can write C# in our code behind that invokes methods, sets properties, and handles events from this ActiveX control. In other words, it makes it feel just like a normal control, and that’s the point—Visual Studio has neatly hidden the fact that COM and .NET work very differently under the covers by generating these interop libraries for us. Example 19-1 shows a how we can program the control— it shows the form’s constructor in the code behind, and after the usual call to InitializeComponent , we’re setting the control’s src property, to tell it from where we’d like it to fetch a PDF file.
Image of page 734

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 735
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern