sábado, 24 de agosto de 2013

Browser Navegador em WPF

Neste artigo, vou mostrar como criar um navegador bem simples, com WPF (Windows Presentation Foundation, veja mais sobre WPF aqui), espero que gostem.


Nosso Código:
<Window x:Class="WpfBrowser.MainWindow"
       Title="Navegador - WPF - www.EdgarEsteves.com.br" Height="Auto" Width="Auto" Loaded="Window_Loaded" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="324" d:DesignWidth="561" SizeToContent="WidthAndHeight">
    <Grid x:Name="LayoutRoot" Width="Auto">
        <Grid.RowDefinitions>
            <RowDefinition Height="10"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="28"></ColumnDefinition>
            <ColumnDefinition Width="501*"></ColumnDefinition>
            <ColumnDefinition Width="10"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid HorizontalAlignment="Stretch" Margin="45,15,0,0" Grid.Row="1" Grid.Column="1" VerticalAlignment="Stretch">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="356*" />
                <ColumnDefinition Width="108*" />
            </Grid.ColumnDefinitions>
            <WebBrowser x:Name="myBrowser" Margin="-55,6,0,0" Cursor="Arrow" Grid.ColumnSpan="2" />
        </Grid>
        <Grid HorizontalAlignment="Stretch" Margin="0,15,456,0" Grid.Row="1" VerticalAlignment="Stretch" Grid.ColumnSpan="2">
            <Grid.RowDefinitions>
                <RowDefinition Height="40"/>
                <RowDefinition Height="40"/>
            </Grid.RowDefinitions>
            <Grid HorizontalAlignment="Stretch" Margin="0,15,0,0" Grid.Row="0"  VerticalAlignment="Stretch">
               <Border Height="20" BorderThickness="2" CornerRadius="5,5,0,0" VerticalAlignment="Top"   />
            </Grid>

            <Grid HorizontalAlignment="Stretch" Margin="0,15,0,0" Grid.Row="1"  VerticalAlignment="Stretch"></Grid>
        </Grid>
        <Button Content="IR" Grid.Column="1" Grid.RowSpan="2" Height="23" HorizontalAlignment="Left" Margin="358,4,0,0" Name="btnIr" VerticalAlignment="Top" Width="75" Click="btnIr_Click" />
        <TextBox Grid.Column="1" Grid.RowSpan="2" Height="23" HorizontalAlignment="Left" Margin="105,3,0,0" Name="txtURL" VerticalAlignment="Top" Width="247" />
        <Button Content="Voltar" Height="23" HorizontalAlignment="Left" Margin="0,4,0,0" Name="btnVoltar" VerticalAlignment="Top" Width="51" Grid.RowSpan="2" Grid.ColumnSpan="2" Click="btnVoltar_Click" />
        <Button Content="Próximo" Height="23" HorizontalAlignment="Left" Margin="29,4,0,0" Name="btnProximo" VerticalAlignment="Top" Width="51" Grid.Column="1" Grid.RowSpan="2" Click="btnProximo_Click" />
    </Grid>
</Window>


E agora o C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfBrowser
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            string urlInicial = "http://www.EdgarEsteves.com.br";
            myBrowser.Navigate(new Uri(urlInicial.ToString()));
            txtURL.Text = urlInicial.ToString();
        }

        private void btnIr_Click(object sender, RoutedEventArgs e)
        {
            if (txtURL.Text.Substring(0, 7) == "http://")
            {
                myBrowser.Navigate(txtURL.Text.ToString());
            }
            else
            {
                MessageBox.Show("Favor Adicionar a URL completa exemplo: http://www.edgaresteves.com.br");
            }
        }

        private void btnVoltar_Click(object sender, RoutedEventArgs e)
        {
            if (myBrowser.CanGoBack)
            {
                myBrowser.GoBack();
            }
            else
            {
                MessageBox.Show("Não poderá Voltar");
            }
        }

        private void btnProximo_Click(object sender, RoutedEventArgs e)
        {
            if (myBrowser.CanGoForward)
            {
                myBrowser.GoForward();
            }
            else
            {
                MessageBox.Show("Não poderá ir Adiante");
            }
        }
    }
}


Bons estudos

0 comentários:

Postar um comentário