Click here to Skip to main content
Click here to Skip to main content

Using ActiveX Controls Example: Insert Internet Explorer into your Dialogs

By , 14 Mar 2003
 

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

About the Author

Hazem Nasereddin
Product Manager
Jordan Jordan
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionHow to refresh the content.memberNIKHIL788 Agrawal28 Dec '12 - 7:17 
I am was using this control to display some web content , but now what i see is the content doesnt get refreshed at all. I tried using "DeleteUrlCacheEntry" but no success. My content keeps updating frequently how can we update or refresh the webpage in our control. Can you please help me.
 
Thanks,
Nikhil.
GeneralGet the URL content after Insert Internet Explorer into Dialogsmemberlittlecheer15 Jul '08 - 22:57 
Hi all,
 
This article is very useful for me. However i face another problem.
 
I need to get the content of the HTML (URL) page. However the GetHTMLDocument return IDispatch instead of CString. I don't know how to handle this to get the content of the page. Sorry as I'm newbie to MFC, Can anybody help me?
 
Thanks a lot.
QuestionWhat is Offline BrowsingmemberVikrant for VC++13 Nov '06 - 4:42 
Hi Friends
As i understand that Offline Browsing means that browsing from our Local PC instead of the net connected.
Now Can we change the location where our active X Control should save the file ? (If it can Smile | :) )
 
Regards
QuestionHow to Save the WebPagememberVikrant for VC++13 Nov '06 - 4:40 
Hi
 
Any idea of how we can save the web page with proper names & image files etc?
 
Regards
GeneralUsing Activex control in com/DLLmemberAvikram28 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/DLLmemberpriyanka s31 Aug '06 - 21:38 
register your active x control on that pc.without registring it will not work on any pc
GeneralRe: Using Activex control in com/DLLmemberMr.SeYeu13 Jul '08 - 20:31 
Please help !
 
How can I register my active X control from dll file ?
 
http://img143.imageshack.us/img143/2939/tung1np.gif

GeneralRe: Using Activex control in com/DLLmemberShailendra Sason31 Aug '08 - 21:18 
to register goto the directory having activex from command prompt and type
regsvr32 youractivexname
This registers the activex
GeneralSearching in current webpagememberpiet3107825 Dec '05 - 21:50 
Hi...
 
Thank you for this very interesting acticle.
 
Perhaps you may help me to understand how to make a search option on this browser?
 
I crossed the Web for information but i didn't find anything.
 
Best regards.
 
-----
Pierre Liétar
GeneralAn active x control on this page might be unsafe...susscristraum@yahoo.com7 Jun '05 - 6:37 
is there any way to get rid of this message?

 
...none...
GeneralRe: An active x control on this page might be unsafe...memberVikrant for VC++13 Nov '06 - 4:38 
Call the Function SetSilent(TRUE)

GeneralCreating activeX control for own databasemembersansanhtwe13 May '05 - 17:48 
i am thinking about creating my own database by using vb source code. An activeX control which can create tables by means of files, arrays, algorithms and datastructure. Please contact me if you have any suggestions!
 
Thank you...
Generalactivex for web [ urgent help needed ]memberrajiv mathew3 Mar '05 - 4:41 
how to create an activex control for an online virus scanner...
the virus scanner is an f-prot free ware and i want to create an active x control which will display the drives and directories of the client so that client can select the directory to be scanned by f-prot antivirus.
 
we have no idea about how to do this but the site www.trendmicro.com
gives much insight about what we intend to do.
 
please help me out if its possible by u because its been quite trying and searching that i started to loose my confidence in making such an online virus scanner to be placed at my web site.
please reply -----even some hints will do a lot i think
GeneralTo get History from IEmemberDhaval_p1 Feb '05 - 22:01 
Hi,
Nice example.
I want to add button which gives me history from Internet explorer setting.
GeneralResizememberBlackDragon23 Dec '04 - 10:17 
Hi,
 
I'm quite a beginner with MFC, but I want to resize the Microsoft web browser while I resize the window...
 
It works well with a custom control, but I tried the same thing on the web control and it doesn't work.
GeneralEmbed this dialog into a dllmemberQinsheng Lai16 May '04 - 14:10 
I try to embed this dialog in a dll. The code is:
 
MY_EXPORT_API int ShowMeInBrowser(LPCSTR filePath)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
MyHTMLViewDlg dlg(filePath);
return dlg.DoModal();
}
Where MyHTMLViewDlg is the implementation of this sample, and
it will show filePath in the browser.
The problem I have is that the dialog never show up!!!
 
Could anybody give me a hint? Thanks.
General.VB not VCmemberdazza0007 May '04 - 15:46 
Hi
I find c too difficult
QuestionHow to create a dll which supports for websussAnonymous2 May '04 - 20:51 
who knows how to create a dll to insert into website such as insert a excel sheet into a web.
thanks.
QuestionCan Any 1 suggest mememberRajeshkumar17 Apr '04 - 8:51 
Any body help by providing solution by how place an URL link in a dialog box i created the using ATL object
QuestionStay in one instance ???memberiosens17 Mar '04 - 12:33 
Hi,
 
I implemented the browser with some other cool features to make dowload of images easy and automatic.
 
Unfortunately certain links are causing the creation od a new browser wimdow, which the is a "normal" IE6 instance.
 
Does anybody know how to prevent the creation of a new window.
( kind of a negative "open link in new window" but instead "stay always in this window").
 
What causes a link to open a new window anyway, and what makes it stay?
 
Eriks
GeneralDLL NEWBIEmembertechmiss1 Mar '04 - 0:40 
hi frnds,
i wanna learn advanced programming in VB, so far i havnt started, and API, COM, DLLs are French to me. I want some sites which can gimme a clear picture and concepts.Confused | :confused:
QuestionHow de we scroll the page automatically?sussAnonymous22 Nov '03 - 8:32 
Hi folks!
 
Does anybody know how can we get the page, once loaded, to scroll to a certain position (say x1,y1)? The idea is I want to insert a webbrowser which has a limited viewing display and I want to scroll the page so that the user gets to see only what is interesting on the page.
 
Any help is greatly appreciated.
 
Thanks!Confused | :confused:
GeneralUsing framesmembertrudy_la_fee10 Sep '03 - 0:27 
Hi,
 
I use the internet explorer ActiveX into my MFC Dialog based application. I want to navigate into a frameset.
<frameset cols="50%,50%">
<frame src="about:blank" name="left">
<frame src="about:blank" name="right">
</frameset>

I want, in my application, to change the source of both frames and I do not find how to do it!!
I know that I have to use de TargetFrameName as an COleVariant but the way I have tried to do it, it does not work!
COleVariant ve;
ve.SetString("right",VT_BSTRT);
m_WebBrowser->Navigate("http://www.google.fr",NULL, &ve, NULL, NULL);

I do not know how to do it a different way!
Please help!
Thanks a lot!
QuestionCan help me?memberkyoshiro19 Apr '03 - 23:16 
Hi, nice article. I'm a beginner who just started to learn visual c++, and i followed your instruction until...
void CWebBrowserDlg::OnOK()
{
m_WebBrowserCtrl.Navigate("http://www.codeproject.com", NULL, NULL, NULL, NULL);
}
which i excute and get the result. But about...
void GoBack()
void GoForward()
can u teach me how to make use of them ? i tried many ways trying to put them into the source code but i cant work...
Hope you could give some guidance. Thanks.
 
I'm a newbie to visual c++. Simpler terms please Smile | :)
AnswerRe: Can help me?memberHazem Nasereddin20 Apr '03 - 2:29 
Hi kyoshiro, Smile | :)
 
Did you try downloading the Demo project?
 
Both functions are shown there.
 
The syntax to use them is m_WebBrowserCtrl.GoBack();
 
But of course, this will only work after you navigate to more than one web page, otherwise you will not have a web page to "Go Back" to. Right?
 
In the demo project I use two counters to know exactelly how many pages have been visited, and where am I with regards to Going Back and Going Forward.
 
I hope this helped. Smile | :)
 
Welcome to the world of Visual C++ Smile | :)
GeneralRe: Can help me?memberkyoshiro21 Apr '03 - 6:47 
Hi Hazem Nasereddin, Smile | :)
Thanks for your help. I manage to do it. It is always helpful for beginers like with a articles like yours Smile | :)
I will now try to see and hope to learn something out of your source code. Hope that you can help me again if i encounter any problems. Thanks Smile | :)
GeneralRe: Can help me?memberHazem Nasereddin21 Apr '03 - 20:27 
You welcome anytime Smile | :)
GeneralCan help me?membersanthoshuvaraja29 Apr '05 - 22:58 
Help me to find a Tutorial about How to Create our own ActiveX in MFC Programming.
 
Bye! Roll eyes | :rolleyes:
QuestionMicrosoft Web Browser Object into DialogBox, without MFC?memberAdrian Bacaianu8 Apr '03 - 21:47 
Hy !
Finnaly i find your comments about non mfc hosting of shdocvw.dll....
But a sample wasnt posted on codeproject or elsewhere, or i dont find it...
 
I really need it, please advise me if you develop something like your article but without MFC, or if you know how...

 
Adrian Bacaianu
QuestionNon MFC dialogs ?member.S.Rod.17 Mar '03 - 21:11 
The fine thing would be to show how to insert an ActiveX in a non MFC dialog. For instance, standard WIN32 dialogs don't have any OLE container implementation inside and thus don't allow an ActiveX to live. That would be more than nice to come up with such implementation. Basically it's a work consisting in extracting the relevant code from the CDialog MFC implementation. I am just too lazy today to do it.
 

Showing how to insert an ActiveX in a MFC-based dialog is useless, as it's straight forward. The thorough procedure is even in the MSDEV programmer's guide documentation, one of the things you read if you are really intending to learn about programming.
AnswerRe: Non MFC dialogs ?memberHazem Nasereddin18 Mar '03 - 5:27 
Hello Rod,
 
You are right Smile | :)
Inserting an ActiveX into a WIN32 dialog would be real fine thing.
 
You are also right, this tutorial is straight forward. And Yes, you can find it in MSDEV programmer's guide documentation.
 
But, the tutorial is as the title suggests, for beginners. Not all beginners have access to such programming documentation. Also, not all of them would rather look into the documentation. Some would rather find a tutorial straight to the point on their beloved web page CodeProject. I do believe that the philosophy behind CodeProject does include offering developers beginner tutorials.
 
In any case, it seems that this is a common issue here on CodeProject: Advanced programmers seem to dislike it whenever someone posts a tutorial for beginners or a tutorial they already read in an MSDN article or something like that. This is dispite the fact that books get away with doing it, and the fact that beginners really appreciate it. Smile | :)
GeneralRe: Non MFC dialogs ?member.S.Rod.18 Mar '03 - 6:01 
I agree with you but only partially. I have to admit that it only reflects my personal thoughts and I don't speak on behalf on any other Cp contributor.
 
Why is Cp so great ? because it provides off-the-shelf code that you can grab, paste in your own code, and see it work without even bothering what it does inside. What made it possible (formerly at Codeguru before Cp) was obviously the nature of the MFC classes, it led to that amazing repository of MFC derived controls we have now.
 
And then it became boring, so tutorials have appeared. Here are a few reasons that justify usefulness of existing tutorials :
- the tutorial corners an issue that is covered nowhere else (read: no relevant result neither in MSDN nor google). An article such as "WIN32 API hooking" exemplifies that perfectly, so much that it could stand the development portal up from the article alone.
- the tutorial sheds a particular light on a known topic, and the explanations are so cristal clear (unlike a lot of topics on MSDN) that it's really an added value to make it available to the entire development community.
 

-- modified at 10:15 Wednesday 12th October, 2005
AnswerRe: Non MFC dialogs ?memberAdrian 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 ?member.S.Rod.9 Apr '03 - 4:11 
Adrian Bacaianu wrote:
Please advise where to find in msdn how to deploy that...
 
You are not likely to find it on MSDN, at least I can't remember any article on this. And this is a quite old topic by the way. Searching the MSJ archive instead of MSDN would be better. But still without any guarantee.
 
Seriously though, you already have the code to do this, it requires you to extract the appropriate code from the MFC CDialog implementation : it's all that have to do with OCC (Ole control container). Watch out MFC\src\dlgcore.cpp and MFC\src\occimpl.h
Good luck!
 
May be I'll look this out in a near future.

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

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