IgorShare Thoughts and Ideas

Consulting and Training

Integrating Web Browser with WPF Forms

Posted by Igor Moochnick on 12/24/2007

There are so many situations where you need to render an HTML in your forms. It may happen when you’d want, for example, to show an external web page, feed or a locally generated content like XML or an HTML. The Web Browser is extremely handy for the Web Automation (I’ll talk about it in future posts) since you have full HTML DOM available for control at your fingertips. So, having WPF now out there is not changing a thing – it’s still possible to host Web Browser inside a form.

Look how easy it is. Let’s say you want to show your blog on an application form. To do so:

  1. Create a WPF application with a Form.
  2. From the toolbox drop a WindowsFormsHost on the form. Give it a name, let’s say, “windowsFormsHost”. Make sure that the references to the WindowsFormsInteroperability and System.Windows.Forms were added to your project. To make a better presentation make the Margin = 0 – the control will fill in all the available form’s space.
  3. In the XAML source add “Loaded” attribute with “Window_Loaded” value to the tag.
  4. Add the following code to the Window_Loaded handler in the .cs file:
    WebBrowser wb = new WebBrowser();
    wb.Navigate(ConfigurationManager.AppSettings["Feed"]);
    windowsFormsHost.Child = wb;

Build, run and enjoy!

Source code can be found as usual on my site.

Advertisements

5 Responses to “Integrating Web Browser with WPF Forms”

  1. BOBKA said

    Прифет. Игорь как если ты хочешь сделать вызов ф-ии изнутри страницы, типа так надо определить обьект для скриптинга. Tipa однако в этом случае вылетает след. . Как ты с этим борешься?

    NonComVisibleBaseClass was detected
    Message: A QueryInterface call was made requesting the default IDispatch interface of COM visible managed class ‘netemb.Window1’. However since this class does not have an explicit default interface and derives from non COM visible class ‘System.Windows.Window’, the QueryInterface call will fail. This is done to prevent the non COM visible base class from being constrained by the COM versioning rules.

  2. BOBKA said

    Прифет. Игорь как если ты хочешь сделать вызов ф-ии изнутри страницы, типа window.external.foo(…) так надо определить обьект для скриптинга. Tipa webBrowser1.ObjectForScripting = this; однако в этом случае вылетает след. exception. Как ты с этим борешься?

  3. Try to mark your class with [ComVisibleAttribute] – http://msdn2.microsoft.com/en-us/library/system.runtime.interopservices.comvisibleattribute(VS.71).aspx

  4. BOBKA said

    http://blogs.msdn.com/dditweb/archive/2007/08/22/using-virtual-earth-in-your-wpf-application.aspx

  5. PsyMonk said

    Давно искал эту инфу, Спасибо вам огромное за Вашу работу.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: