Creating a Web Browser in WPF

Introduction

This blog will help you to create a web browser in WPF (Windows Presentation Foundation).

Prerequisites

  • Visual Studio 2013 with update 4 (or) Visual Studio 2015

Follow the following steps now:

Step 1: Open Visual Studio 2015 and Create a New Project.



Step 2: Select WPF (Windows Presentation Foundation), name your project and select the location where it has to be saved.



Step 3: Goto MainWindow.xaml and paste the following code for the web browser:

  1. <Window x:Class="WPFWebControl.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="My Web Browser" WindowState="Normal" Loaded="Window_Loaded" WindowStyle="ThreeDBorderWindow" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="529" Width="731">  
  2.     <Grid>  
  3.         <Button Content="<<" Height="23" HorizontalAlignment="Left" Margin="10,5,0,0" Name="MyBack" VerticalAlignment="Top" Width="25" ToolTip="Backword" Click="MyBack_Click" />  
  4.         <WebBrowser Height="445" HorizontalAlignment="Left" Margin="10,33,0,0" Name="MyWebBrowser" VerticalAlignment="Top" Width="687" LoadCompleted="MyWebBrowser_LoadCompleted" />  
  5.         <TextBox Height="23" Margin="103,5,12,0" Name="MyTextBox" VerticalAlignment="Top" />  
  6.         <Button Content="|>" Height="23" HorizontalAlignment="Left" Margin="41,5,0,0" Name="MyGo" VerticalAlignment="Top" Width="25" ToolTip="Go" Click="MyGo_Click" />  
  7.         <Button Content=">>" Height="23" HorizontalAlignment="Right" Margin="0,5,612,0" Name="MyForward" VerticalAlignment="Top" Width="25" ToolTip="Forward" Click="MyForward_Click" />   
  8.    </Grid>  
  9. </Window>  


Step 4: Now copy the following code in MainWindow.xaml.cs:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Windows;  
  6. using System.Windows.Controls;  
  7. using System.Windows.Data;  
  8. using System.Windows.Documents;  
  9. using System.Windows.Input;  
  10. using System.Windows.Media;  
  11. using System.Windows.Media.Imaging;  
  12. using System.Windows.Navigation;  
  13. using System.Windows.Shapes;  
  14. namespace WPFWebControl  
  15. {  
  16.     public partial class MainWindow: Window  
  17.     {  
  18.         public MainWindow()  
  19.         {  
  20.             InitializeComponent();  
  21.         }  
  22.         private void Window_Loaded(object sender, RoutedEventArgs e)  
  23.         {  
  24.             try  
  25.             {  
  26.                 MyWebBrowser.Source = new Uri("http://www.microsoft.com");  
  27.             }  
  28.             catch (Exception ex)  
  29.             {  
  30.                 MessageBox.Show(ex.Message);  
  31.             }  
  32.         }  
  33.         private void MyBack_Click(object sender, RoutedEventArgs e)  
  34.         {  
  35.             try  
  36.             {  
  37.                 MyWebBrowser.GoBack();  
  38.             }  
  39.             catch (Exception ex)  
  40.             {  
  41.                 MessageBox.Show(ex.Message);  
  42.             }  
  43.         }  
  44.         private void MyForward_Click(object sender, RoutedEventArgs e)  
  45.         {  
  46.             try  
  47.             {  
  48.                 MyWebBrowser.GoForward();  
  49.             }  
  50.             catch (Exception ex)  
  51.             {  
  52.                 MessageBox.Show(ex.Message);  
  53.             }  
  54.         }  
  55.         private void MyGo_Click(object sender, RoutedEventArgs e)  
  56.         {  
  57.             try  
  58.             {  
  59.                 MyWebBrowser.Source = new Uri("http://" + MyTextBox.Text);  
  60.             }  
  61.             catch (Exception ex)  
  62.             {  
  63.                 MessageBox.Show(ex.Message);  
  64.             }  
  65.         }  
  66.         private void MyWebBrowser_LoadCompleted(object sender, NavigationEventArgs e)  
  67.         {  
  68.             MessageBox.Show("Completed.");  
  69.         }  
  70.     }  
  71. }  


Step 5: Now run the project by clicking on start at the top of Visual Studio 2015.