開発環境 Microsoft Visual C# 2010 Express (SP1)
実行環境 Microsoft Windows XP Home Edition (SP3)
プロジェクトの種類 Windows フォーム アプリケーション
プロジェクト名 WebBrowser

プロジェクト


プロジェクトの保存
[ソリューションのディレクトリを作成]はチェックを付けなくてもいい。

フォーム


Form1プロパティ
Text:WebBrowser
Size:800, 600

メニューとツールバー/ToolStripをForm1上にドロップ。
名前:toolStrip1

toolStrip1にTextBoxを追加。
名前:toolStripTextBox1
Size:400, 25

toolStrip1にButtonを追加。
名前:toolStripButton1

コンテナー/SplitContainerをForm1上にドロップ。
名前:splitContainer1
Orientation:Horizontal

コモン コントロール/TextBoxをPanel1上にドロップ。
名前:textBox1
Dock:Fill
Multiline:True
ScrollBars:Vertical

コモン コントロール/WebBrowserをPanel2上にドロップ。
名前:webBrowser1

イベントハンドラ


Form1にLoadイベントハンドラを追加。
toolStripButton1にClickイベントハンドラを追加。

Form1.cs
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;
 
namespace WebBrowser
{
	public partial class Form1 : Form
	{
		const string CRLF = "\r\n";
		const string TMPFILE = @"C:\tmp\hoge.html";
 
		public Form1()
		{
			InitializeComponent();
		}
 
		private void Form1_Load(object sender, EventArgs e)
		{
			toolStripTextBox1.Text = "http://menu.2ch.net/bbsmenu.html";
			Navigate(@"C:\tmp\hello.html");
		}
 
		private void toolStripButton1_Click(object sender, EventArgs e)
		{
			//webBrowser1.Navigate(toolStripTextBox1.Text);
			WebClient wc = new WebClient();
			wc.DownloadFile(toolStripTextBox1.Text, TMPFILE);
			Navigate(TMPFILE);
		}
 
		void Navigate(string urlString)
		{
 
			string[] strArray;
			strArray = File.ReadAllLines(urlString, Encoding.Default);
			string text = String.Join(CRLF, strArray) + CRLF;
 
			textBox1.Text = text;
			textBox1.Select(0, 0);
 
			webBrowser1.DocumentText = text;
		}
	}
}
 

hello.html
<html>
<head>
<title>サンプル</title>
</head>
<body>
<h1>テスト</h1>
</body>
</html>
 
最終更新:2012年11月15日 11:36