Click here to Skip to main content
15,884,388 members
Articles / Desktop Programming / Windows Forms
Article

Using the WebBrowser as an extention possibility

Rate me:
Please Sign up or sign in to vote.
4.70/5 (18 votes)
22 Jun 2007BSD4 min read 86.4K   939   63   22
Creating your own extentions by using a simple WebBrowser object

Screenshot - MyActivitySDK.jpg

Introduction

We have all seen the nice little features of the Microsoft MSN Messenger Activity SDK (well, I have ;-). MSN provides us with the SDK, messengeractivitySDK.zip. With this SDK, the developer has the power to create little applications. Data can now easily be sent from one application to the other. I have seen a nice example on the Code Project. However, this article isn't about the implementation of the Messenger Activity SDK. Rather, it discusses taking advantage of the System.Windows.Forms.WebBrowser class in the .NET 2.0 Framework so you can create your own similar SDK.

Background

Now how do the MSN Activity SDK developers make it possible for others to create nice extensions like that fun "Tic Tac Toe" game they give as an example? They let the developer have a choice between VBScript and JavaScript. Actually, they don't give you the choice explicitly; Internet Explorer gives you the choice of scripting languages. I personally prefer JavaScript as a scripting language, so I will show this little example in JavaScript.

JavaScript has a special namespace called window.external. This namespace is extendable by other applications or, to express it in a way I would never understand, "window.external is a built-in DOM object provided for host access." MSN Messenger uses this extendable space to provide other developers with a means to make nice web applications that can communicate with the hosting application. This, in combination with the WebBrowser class being able to call any method from the loaded page, can give you a powerful way of letting developers make extensions without any dangerous safety threats. Your application cannot be compromised in this way because the world out there has no way to get inside. At the same time, you have all the power to make more or fewer methods and properties available.

Using the code

In the demo, I have tried to keep the code as clean as possible. I hope you can understand everything. Enough talking for now, though. Let's show you some gold. ;-)

To begin, you will need a form with a WebBrowser on it and, of course, a webpage to view. I assume you can handle the creation of the form with WebBrowser and the basic HTML page. So, what do we want to do?

  • We want to call a method from the web page.
  • We want to call a method from the hosting application.
  • We want to get a string from the hosting application.
  • We want to set a string in the hosting application from the web page.

Let's start with calling a method from the web page in the hosting application:

C#
private void myCallAMethodButton_Click(object sender, EventArgs e) 
{ 
    // You can call any method from the page you loaded... 
    // I have enclosed this call in a try-catch, because it will 
    // create a exception when there isn't a page loaded ... 
    try 
    { 
        // If you call a method, you can use a object array to 
        // supply parameters. My method only has 1 parameter, 
        // so I give 1 object in the array. 
        myWebBrowser.Document.InvokeScript("InvokeMethod",
            new object[] { "Wow, impressive !" }); 
    }
    catch (Exception e) 
    { 
        MessageBox.Show(e.Message);
    }
}

By the way, you should check out the Document property. It contains lots of valuable methods and other nice things. Let's continue while we have the spirit! First, let's call a method that is in the application, not in the HTML page. To call a method that is in the hosting application, you need to expose the class to the COM. Don't ask me why, it is just so.

C#
[System.Runtime.InteropServices.ComVisible (true)] 
public class WindowExternal 
{ }

Any method or property in this class will be exposed to the script all through the window.external object. To view the members of the class, you should download the source. There isn't a lot to view in this class, nothing you don't yet know about. Let's now apply the class that we just created, WindowExternal, to the WebBrowser object in the Form:

C#
public MainForm()
{ 
    InitializeComponent();
  
    // This is it, THE MOST IMPORTANT THING IN YOUR APPLICATION ! :-)
    myWebBrowser.ObjectForScripting = new WindowExternal(); 
}

You can assign any object to the ObjectForScripting property, even the form you are working in, as long the object is ComVisible!

Points of interest

People just don't know about the power JavaScript has. Simply learn the language and you will be able to do anything you want in C# or Delphi. This is now the end of my little article; I hope you have enjoyed reading it.

History

  • 24 September, 2006 -- This is the first version of my article and my first here on CodeProject, so there is not a real history yet. But you can be sure that there will be follow-ups!
  • 10 December, 2006 -- Article updated.
  • 22 June, 2007 -- Article edited and moved to the main CodeProject.com article base.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
Belgium Belgium
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionUsing a web page that is not not hosted in my applivation Pin
triplebit20-Jul-11 19:11
triplebit20-Jul-11 19:11 
AnswerThanks for your help Pin
dfg147817-Nov-09 16:03
dfg147817-Nov-09 16:03 
GeneralRunning the code in MAC OS Pin
miauwmiauwmiauw26-Jun-08 18:56
miauwmiauwmiauw26-Jun-08 18:56 
GeneralRe: Running the code in MAC OS Pin
Filip van der Meeren26-Jun-08 21:45
Filip van der Meeren26-Jun-08 21:45 
QuestionHow to call a script when you don't know its name? Pin
sduhd7-Apr-08 16:55
sduhd7-Apr-08 16:55 
AnswerRe: How to call a script when you don't know its name? Pin
Filip van der Meeren7-Apr-08 23:15
Filip van der Meeren7-Apr-08 23:15 
GeneralYou listed 4 goals, but demonstrated only two. Pin
Ashaman23-Jun-07 4:10
Ashaman23-Jun-07 4:10 
GeneralRe: You listed 4 goals, but demonstrated only two. Pin
Filip van der Meeren23-Jun-07 6:48
Filip van der Meeren23-Jun-07 6:48 
GeneralVery Cool Pin
merlin98122-Jun-07 11:43
professionalmerlin98122-Jun-07 11:43 
GeneralRe: Very Cool Pin
Filip van der Meeren23-Jun-07 7:11
Filip van der Meeren23-Jun-07 7:11 
QuestionEvents and managed object Pin
Airbornes10-May-07 10:22
Airbornes10-May-07 10:22 
AnswerRe: Events and managed object Pin
Filip van der Meeren23-Jun-07 7:03
Filip van der Meeren23-Jun-07 7:03 
First question:
--------------------------
I haven't been able to do so, yet Wink | ;-)


Second question:
--------------------------
You could use the newly created class, but not create an instance of it (without using a backdoor like the delegates invoke method or using some kind of reflection).

Me sorry:
--------------------------
Sorry that it took so long to answer your question... Due to exams and putting your mail asside for later processing I kind of forgot about the question.


Don't you also love the code?

GeneralClean and Crisp Pin
Siddharth Barman23-Mar-07 5:56
Siddharth Barman23-Mar-07 5:56 
GeneralRe: Clean and Crisp Pin
Filip van der Meeren5-Apr-07 22:53
Filip van der Meeren5-Apr-07 22:53 
QuestionHow to call function in form hosting webbrowser? Pin
Bill SerGio, The Infomercial King5-Feb-07 17:18
Bill SerGio, The Infomercial King5-Feb-07 17:18 
AnswerRe: How to call function in form hosting webbrowser? Pin
Filip van der Meeren9-Feb-07 23:21
Filip van der Meeren9-Feb-07 23:21 
Generalunedited Pin
Muammar©9-Dec-06 23:15
Muammar©9-Dec-06 23:15 
GeneralRe: unedited Pin
Filip van der Meeren9-Dec-06 23:46
Filip van der Meeren9-Dec-06 23:46 
Generalwell said Pin
Darchangel10-Oct-06 8:47
Darchangel10-Oct-06 8:47 
GeneralExcellent Article Pin
rajantawate1(http//www.tawateventures.com8-Oct-06 15:04
rajantawate1(http//www.tawateventures.com8-Oct-06 15:04 
GeneralRe: Excellent Article Pin
Filip van der Meeren8-Oct-06 20:29
Filip van der Meeren8-Oct-06 20:29 
GeneralRe: Excellent Article Pin
Quentin Pouplard10-Dec-06 6:03
Quentin Pouplard10-Dec-06 6:03 

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.