Click here to Skip to main content
15,879,095 members
Articles / Desktop Programming / MFC
Article

Using ActiveX Controls Example: Insert Internet Explorer into your Dialogs

Rate me:
Please Sign up or sign in to vote.
4.43/5 (25 votes)
14 Mar 20034 min read 258.2K   4K   85   34
Inserting an ActiveX control (Internet Explorer) into your dialogs and handling relevant events.

Sample Image - WebBrowser1.png

Introduction

This article is intended for newbies to the ActiveX world. In a Step-By-Step approach, it demonstrates inserting and handling the Microsoft Internet Explorer as an ActiveX control. The article shows how to insert the control, how to handle events and how to pass data to it. This article may also help you, if you want to use any other ActiveX control.

Inserting the ActiveX control

  1. On the dialog which you want to add the ActiveX control, right click the mouse to get the pop-up menu. From the pop-up menu, select Insert ActiveX Control.

    You will get a menu, scroll down and select Microsoft Web Browser as shown in the next image, then press OK.

    Sample screenshot

    All the other items in this menu are ActiveX controls which you can use. You may want to try out a couple of these later on, I am sure you will find a few of them interesting.

  2. After you have finished step 1, you will notice that a new control has appeared on screen. The ID for this control is IDC_EXPLORER1. You should at this stage stretch this controller as much as you can because you will need the space. Click the mouse on this control to highlight it, then press Ctrl and W to start the MFC Class Wizard.
  3. Click on the second tab, titled Member Variables. Then click on the ID of our new controller IDC_EXPLORER1 to highlight it. Afterwards, click on button Add Variable. This should open a dialog that informs you that MS Dev. Studio wants to generate a C++ wrapper class for our ActiveX control. Press OK (twice), and let MS Studio do its work.
  4. After MS Studio is done creating the class, a menu titled Add Member Variable will pop up asking you to name the variable. Give the variable a name like m_WebBrowserCtrl.
  5. You will now see the member variable next to the ID of our controller. MAKE SURE YOU PRESS "OK" NOW, otherwise all the previous changes will be rolled back by MS Studio.
  6. On the dialog, double click the button OK, and create a member function OnOK. We are going to use this button to test our new control. There are many functions that we can use in the new ActiveX control, I will explain a bunch of them later on in this article, but for now we are concerned with the command that allows the Web Browsing component to browse to a (any) webpage. The function is Navigate.
    void Navigate(LPCSTR URL, VARIANT *Flags, 
      VARIANT * TargetFrameName, VARIANT *PostData, VARIANT *Headers)

    Don't worry about all the variables, if you simply want your new web component to browse to a web page, all you have to do is the following:

    m_WebBrowserCtrl.Navigate("http://www.codeproject.com", 
                  NULL, NULL, NULL, NULL); 

    Delete the old contents of the OnOK function and add the previous line in its place. Thus the OnOk function will look like this:

    void CWebBrowserDlg::OnOK() 
    {
      m_WebBrowserCtrl.Navigate("http://www.codeproject.com", 
                               NULL, NULL, NULL, NULL);
    }

    Now would be a good time to test your application. Once your dialog is opened, click on OK, this should browse into the Code Project main web page.

More functions

Let's now explore a few functions for this ActiveX control:

void Navigate2(VARIANT * URL, VARIANT *Flags, VARIANT * TargetFrameName, 
                                        VARIANT *PostData, VARIANT *Headers) 

The same as Navigate, only this function also allows you to load files.

void GoBack()
void GoForward()

These two functions can be used to go forward and backward between browsed pages. Obviously, each has to have its own button on the dialog.

void GoHome()

This function will browse to the web page selected as the User Homepage, in the User Options for Internet Explorer.

void GoSearch()

This function will go to the Microsoft Internet search website.

void Refresh()

This function will refresh the current webpage.

void Stop()

This function stops loading the current page.

CString GetLocationName()

This function gets the name of the location, which is the string that is shown at the title bar of the normal Internet Explorer instance.

CString GetLocationURL()

This function gets the URL or the web address of the current web page.

There are other functions which you can use, you can find them all in MSDN under CHtmlView.

Events and messages

This component offers a wide range of events. To access the events, you can use the class wizard. Click the ActiveX control on the dialog, then press Ctrl and W, or right click on the control and select Events.... You will see a list of possible events you may want to use. Here are a few of them to get you started:

BeforeNavigate2

This event is fired before a navigation process to a URL starts.

NavigateComplete2

This event is fired after a navigation process to a URL is done, and the page is loaded.

You can check all the other events, and use them in your applications as you might need, or see fit.

I hope this was as much fun to read as it was to write :)

Credits

For the animated images in the demo project, I used the article by Oleg Bykov. It is available on Code Project: Add GIF-animation to your MFC and ATL projects with the help of CPictureEx and CPictureExWnd .

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Product Manager
Jordan Jordan
Graduated in 1999 with a Bachelors Degree (Computer Engineering) from McGill University in Montreal, Canada. Participated in the development of The McGill Intelligent Classroom (www.cim.mcgill.ca/~jer/research/eclass/). 2000 Joined Hummingbird Communications (www.hcl.com) and worked in the development of HostExplorer and FTP-explorer. 2001 moved from Canada to Jordan. Worked at SigmaSoft, developing MFC-ObjectARX applications for AutoCAD and other AutoDesk products, as well as other MFC\COM applications. Worked for MECA www.meca.com developing their Instant Messenger. Now working for Esense Software developing Applications for cellular phones among many other things.

Comments and Discussions

 
GeneralRe: Can help me? Pin
kyoshiro21-Apr-03 6:47
kyoshiro21-Apr-03 6:47 
GeneralRe: Can help me? Pin
Hazem Nasereddin21-Apr-03 20:27
Hazem Nasereddin21-Apr-03 20:27 
GeneralCan help me? Pin
santhoshuvaraja29-Apr-05 22:58
santhoshuvaraja29-Apr-05 22:58 
QuestionMicrosoft Web Browser Object into DialogBox, without MFC? Pin
Adrian Bacaianu8-Apr-03 21:47
Adrian Bacaianu8-Apr-03 21:47 
QuestionNon MFC dialogs ? Pin
Stephane Rodriguez.17-Mar-03 21:11
Stephane Rodriguez.17-Mar-03 21:11 
AnswerRe: Non MFC dialogs ? Pin
Hazem Nasereddin18-Mar-03 5:27
Hazem Nasereddin18-Mar-03 5:27 
GeneralRe: Non MFC dialogs ? Pin
18-Mar-03 6:01
suss18-Mar-03 6:01 
AnswerRe: Non MFC dialogs ? Pin
Adrian Bacaianu8-Apr-03 21:52
Adrian Bacaianu8-Apr-03 21:52 
Hi!

I'm also very intersted in how to host that ActiveX in a non MFC dialog...
Please advise where to find in msdn how to deploy that...



Adrian Bacaianu
GeneralRe: Non MFC dialogs ? Pin
Stephane Rodriguez.9-Apr-03 4:11
Stephane Rodriguez.9-Apr-03 4:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.