Go WTL Getting Started (6)---ActiveX controls __ Browsers

Source: Internet
Author: User
Tags blank page

[Source code download: http://download.csdn.net/source/3526786] Introduction

Here, I'll introduce ATL support for using ActiveX controls in a dialog box. Because ATL specializes in handling ActiveX controls, WTL does not involve other auxiliary classes. However, the ATL method that uses ActiveX controls is very different from MFC, so it needs to be highlighted. I'll explain how to use an ActiveX control and handle events to develop a program that does not lose functionality compared to programs generated by the MFC Class Wizard.
The example in this chapter demonstrates how to use the IE Browser Control, which has two benefits:
1 This control is available on every computer
2 It has a lot of methods and event handling, is a good example to do the demo

I certainly can't compare to people who spend a lot of time writing custom browsers based on browser controls, but, through this article, you know how to start writing custom browsers yourself. starting with the AppWizard Creating the Project

The WTL Program Creation Wizard can create a program that contains ActiveX controls. We create a iehoster new project and use a modeless dialog box form.
The generated code

In this section, we will see new code that has been generated by the wizard and not seen. The details of the ActiveX containment class are described in the next section.

First check the stdafx.h file, [CPP] view plain copy #include <atlbase.h> #include <atlapp.h> exter       n Cappmodule _module;      #include <atlcom.h> #include <atlhost.h> #include <atlwin.h> #include <atlctl.h> // .. Other WTL headers ...

Atlcom.h and atlhost.h are very important. They contain definitions of COM-related classes (such as smart pointer ccomptr), as well as window classes for containment controls.

Below, look at the Cmaindlg class in maindlg.h: [CPP] view plain copy class Cmaindlg:public Caxdialogimpl<cmaindlg> public cupdateui<cmaindlg>, public cmessagefilter, public Cidlehandler

Now, Cmaindlg is derived from CAxDialogImpl, which is the first step in making the dialog box support containment ActiveX controls.

Finally, add a new line of code to the program entry function WinMain (): [CPP] view plain copy int WINAPI _tWinMain (...)           {///... _module.init (NULL, hinstance);           <strong>atlaxwininit ();</strong> int nret = Run (Lpstrcmdline, ncmdshow);       _module.term ();   return nret; }

AtlAxWinInit () registers a window class named Atlaxwin, which ATL uses to create an containment window for ActiveX controls.
Because some of the changes in ATL7 require adding libid to the parameters of _module.init (), some people in the forum suggest using the following code: [CPP] view plain copy _module.init (NULL,   HINSTANCE, &libid_atllib); adding Controls with the Resource Editor

As with MFC programs, ATL can also use the Resource editor to add controls to the dialog box. First, click the right mouse button on the dialog editor and select Insert ActiveX Control from the pop-up menu:

VC will display the Control class table installed on the system, scroll the list select "Microsoft Web Browser", view the properties of the control, and set the ID to Idc_ie. The controls in the dialog box in the Resource editor should display this way:

If you compile the program now, you will see the browser Control in the dialog box, which will display a blank page because we have not yet told it which page to open.
In the next section, I'll introduce the ATL classes related to creating and enclosing ActiveX controls, and we'll also understand how these classes exchange information with browsers. ATL Classes for control Hosting

When you are enclosing ActiveX controls in a dialog box, you need to work with these two classes CAxDialogImpl and CAxWindow. They provide the interface methods that all control containers must implement, providing common functional functions, such as querying a particular COM interface for a control. CAxDialogImpl

The first class is CAxDialogImpl, so that your dialog box can contain ActiveX controls must derive from CAxDialogImpl. The CAxDialogImpl class overloads the Create () and DoModal () functions, which are called by global functions Atlaxcreatedialog () and AtlAxDialogBox (), respectively. Now that the Iehoster dialog is created by Create (), let's see what the Atlaxcreatedialog () does.

Atlaxcreatedialog () Loads the dialog resource and uses the secondary class _dialogsplithelper to traverse all the controls of the dialog box to find special entries created by the resource editor that represent this as an ActiveX control. For example, the following is the entry for the browser control in the iehoster.rc file: [CPP] view Plain copy Controls ", Idc_ie," { 8856F961-340A-11D0-A96B-00C04FD705A2} ", ws_tabstop,7,7,116,85

The first argument is the window text (the empty string), the second is the control's ID, and the third is the window's class name. The _dialogsplithelper::splitdialogtemplate () function finds a window class name that starts with ' {' To know that this is an entry that represents an ActiveX control. It creates a temporary dialog template in memory in which these special control entries are created by Atlaxwin window equivalents, and the new entry is in-memory equivalents: [CPP] view Plain copy controls

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.