browser
Børre Stenseth
Moduler>GUI-komponenter>Nettleser

Nettleser

Hva
screen
Bruk en nettleser i programmet

Et enkelt program med et nettleser objekt.

Vi lager det nytt "project" av typen "Windows Application". Så bruker vi GUI-editoren til å lage en enkel form. Vi får igjen tre kildefiler:

  • Program.cs
  • Form1.cs
  • Form1.Designer.cs

Vi konsentrerer oss om den ene fila som inneholder vår handskrevne kode

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace control5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            // Produce a minimalistic page from scratch
            webBrowser1.DocumentText = @"<html>
            <body>
                <h1>Hallo</h1>
                <p>Velg en side</p>
             </body>
            </html>";
        }
        private void button_Click(object sender, EventArgs e)
        {
            if (sender.Equals(button1))
            {
                webBrowser1.Url =
                    new System.Uri("http://www.it.hiof.no/~borres/dn/");
            }
            else if (sender.Equals(button2))
            {
                webBrowser1.Url =
                    new System.Uri("http://www.google.com/");
            }
            else if (sender.Equals(button4))
            {
                // make a simple page that embeds a youtube video
                // info on embedding at: http://code.google.com/p/swfobject/
// simple embedding IE
                int W = webBrowser1.Width - 20;
                int H = webBrowser1.Height - 40;
                webBrowser1.DocumentText = String.Format(@"<html><body><p>
            <embed src=""http://www.youtube.com/v/WzSayxVM_E0&hl=en_US&fs=1&rel=0&border=1"" 
              type=""application/x-shockwave-flash"" 
              allowscriptaccess=""always"" 
              allowfullscreen=""false"" 
              width=""{0}"" height=""{1}"">
            </embed>                
            </p></body></html>", W, H);
// YouTube suggest iFrame:
//          webBrowser1.DocumentText = @"<html> <body> <p>
//        <iframe title=""YouTube video player"" 
//                  class=""youtube-player"" 
//                  type=""text/html"" width=""480"" height=""390"" 
//                  src=""http://www.youtube.com/embed/WzSayxVM_E0"" frameborder=""0"" allowFullScreen> 
//          </iframe>
//        </p></body></html>";        
                
                
// Youtube old version suggest:
//         webBrowser1.DocumentText = @"<html><body><p>
//         <object width=""340"" height=""285"">
//         <param name=""movie"" 
//         value=""http://www.youtube.com/v/WzSayxVM_E0&hl=en_US&fs=1&rel=0&border=1"">
//         </param>
//         <param name=""allowFullScreen"" value=""true""></param>
//         <param name=""allowscriptaccess"" value=""always""></param>
//         <embed 
//         src=""http://www.youtube.com/v/WzSayxVM_E0&hl=en_US&fs=1&rel=0&border=1"" 
//         type=""application/x-shockwave-flash"" allowscriptaccess=""always"" 
//         allowfullscreen=""true"" width=""340"" height=""285"">
//         </embed>
//         </object>                
//         </p></body></html>";
            }
            else if (sender.Equals(button3))
            {
                // has user written anythong meaningfull
                try
                {
                    System.Uri theUri = new System.Uri(textBox1.Text);
                    webBrowser1.Url = theUri;
                }
                catch (Exception ex)
                {
                    webBrowser1.DocumentText = String.Format(@"<html><body>
                    <h1>Uff</h1>
                    <p>Forsto ikke URL'en fordi:</p>
                    <p>{0}</p>
                    </body></html>", ex.Message);
                }
            }
        }
    }
}
Referanser
Prosjektet:
https://svn.hiof.no/svn/psource/Csharpspikes/control5
Vedlikehold
B.Stenseth, desember 2006
(Velkommen) Moduler>GUI-komponenter>Nettleser (Farger)