Click here to Skip to main content
Click here to Skip to main content
Go to top

Using ActiveX Controls Example: Insert Internet Explorer into your Dialogs

, 14 Mar 2003
Rate this:
Please Sign up or sign in to vote.
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 Smile | :)

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

Share

About the Author

Hazem Nasereddin
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

 
QuestionHow to refresh the content. PinmemberNIKHIL788 Agrawal28-Dec-12 7:17 
GeneralGet the URL content after Insert Internet Explorer into Dialogs Pinmemberlittlecheer15-Jul-08 22:57 
QuestionWhat is Offline Browsing PinmemberVikrant for VC++13-Nov-06 4:42 
QuestionHow to Save the WebPage PinmemberVikrant for VC++13-Nov-06 4:40 
GeneralUsing Activex control in com/DLL PinmemberAvikram28-Dec-05 0:40 
I have used a activex control in my com/dll about box. It works fine when i run it on my pc. But when i want to open the same project on another pc an message is displayed that ActiveX control is not registered.what i have to do that my dll can work on another pcs.
GeneralRe: Using Activex control in com/DLL Pinmemberpriyanka s31-Aug-06 21:38 
GeneralRe: Using Activex control in com/DLL PinmemberMr.SeYeu13-Jul-08 20:31 
GeneralRe: Using Activex control in com/DLL PinmemberShailendra Sason31-Aug-08 21:18 
GeneralSearching in current webpage Pinmemberpiet3107825-Dec-05 21:50 
GeneralAn active x control on this page might be unsafe... Pinsusscristraum@yahoo.com7-Jun-05 6:37 
GeneralRe: An active x control on this page might be unsafe... PinmemberVikrant for VC++13-Nov-06 4:38 
GeneralCreating activeX control for own database Pinmembersansanhtwe13-May-05 17:48 
Generalactivex for web [ urgent help needed ] Pinmemberrajiv mathew3-Mar-05 4:41 
GeneralTo get History from IE PinmemberDhaval_p1-Feb-05 22:01 
GeneralResize PinmemberBlackDragon23-Dec-04 10:17 
GeneralEmbed this dialog into a dll PinmemberQinsheng Lai16-May-04 14:10 
General.VB not VC Pinmemberdazza0007-May-04 15:46 
QuestionHow to create a dll which supports for web PinsussAnonymous2-May-04 20:51 
QuestionCan Any 1 suggest me PinmemberRajeshkumar17-Apr-04 8:51 
QuestionStay in one instance ??? Pinmemberiosens17-Mar-04 12:33 
GeneralDLL NEWBIE Pinmembertechmiss1-Mar-04 0:40 
QuestionHow de we scroll the page automatically? PinsussAnonymous22-Nov-03 8:32 
GeneralUsing frames Pinmembertrudy_la_fee10-Sep-03 0:27 
QuestionCan help me? Pinmemberkyoshiro19-Apr-03 23:16 
AnswerRe: Can help me? PinmemberHazem Nasereddin20-Apr-03 2:29 
GeneralRe: Can help me? Pinmemberkyoshiro21-Apr-03 6:47 
GeneralRe: Can help me? PinmemberHazem Nasereddin21-Apr-03 20:27 
GeneralCan help me? Pinmembersanthoshuvaraja29-Apr-05 22:58 
QuestionMicrosoft Web Browser Object into DialogBox, without MFC? PinmemberAdrian Bacaianu8-Apr-03 21:47 
QuestionNon MFC dialogs ? Pinmember.S.Rod.17-Mar-03 21:11 
AnswerRe: Non MFC dialogs ? PinmemberHazem Nasereddin18-Mar-03 5:27 
GeneralRe: Non MFC dialogs ? Pinmember.S.Rod.18-Mar-03 6:01 
AnswerRe: Non MFC dialogs ? PinmemberAdrian Bacaianu8-Apr-03 21:52 
GeneralRe: Non MFC dialogs ? Pinmember.S.Rod.9-Apr-03 4:11 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 15 Mar 2003
Article Copyright 2003 by Hazem Nasereddin
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid