using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WebBrowser1
{
///
/// Form1 の概要の説明です。
///
public class Form1 : System.Windows.Forms.Form
{
private AxSHDocVw.AxWebBrowser axWebBrowser1;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.StatusBarPanel statusBarPanel1;
private System.Windows.Forms.ToolBar toolBar1;
private System.Windows.Forms.ToolBarButton toolBarButtonBack;
private System.Windows.Forms.ToolBarButton toolBarButtonForward;
private System.Windows.Forms.ToolBarButton toolBarButtonStop;
private System.Windows.Forms.ToolBarButton toolBarButtonNavigate;
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.StatusBarPanel statusBarPanel2;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.Splitter splitter1;
private System.Windows.Forms.ToolBarButton toolBarButtonRefresh;
private System.ComponentModel.IContainer components;
public Form1()
{
//
// Windows フォーム デザイナ サポートに必要です。
//
InitializeComponent();
//
// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
//
}
///
/// 使用されているリソースに後処理を実行します。
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows フォーム デザイナで生成されたコード
///
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
///
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.axWebBrowser1 = new AxSHDocVw.AxWebBrowser();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
this.statusBarPanel2 = new System.Windows.Forms.StatusBarPanel();
this.toolBar1 = new System.Windows.Forms.ToolBar();
this.toolBarButtonBack = new System.Windows.Forms.ToolBarButton();
this.toolBarButtonForward = new System.Windows.Forms.ToolBarButton();
this.toolBarButtonStop = new System.Windows.Forms.ToolBarButton();
this.toolBarButtonNavigate = new System.Windows.Forms.ToolBarButton();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.splitter1 = new System.Windows.Forms.Splitter();
this.toolBarButtonRefresh = new System.Windows.Forms.ToolBarButton();
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).BeginInit();
this.SuspendLayout();
//
// axWebBrowser1
//
this.axWebBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
this.axWebBrowser1.Enabled = true;
this.axWebBrowser1.Location = new System.Drawing.Point(0, 51);
this.axWebBrowser1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axWebBrowser1.OcxState")));
this.axWebBrowser1.Size = new System.Drawing.Size(400, 229);
this.axWebBrowser1.TabIndex = 0;
this.axWebBrowser1.StatusTextChange += new AxSHDocVw.DWebBrowserEvents2_StatusTextChangeEventHandler(this.axWebBrowser1_StatusTextChange);
this.axWebBrowser1.CommandStateChange += new AxSHDocVw.DWebBrowserEvents2_CommandStateChangeEventHandler(this.axWebBrowser1_CommandStateChange);
this.axWebBrowser1.DocumentComplete += new AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(this.axWebBrowser1_DocumentComplete);
this.axWebBrowser1.ProgressChange += new AxSHDocVw.DWebBrowserEvents2_ProgressChangeEventHandler(this.axWebBrowser1_ProgressChange);
//
// comboBox1
//
this.comboBox1.Dock = System.Windows.Forms.DockStyle.Top;
this.comboBox1.Location = new System.Drawing.Point(0, 28);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(400, 20);
this.comboBox1.TabIndex = 2;
this.comboBox1.Text = "http://ukamen.hp.infoseek.co.jp/";
this.comboBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.comboBox1_KeyPress);
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 280);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
this.statusBarPanel1,
this.statusBarPanel2});
this.statusBar1.ShowPanels = true;
this.statusBar1.Size = new System.Drawing.Size(400, 22);
this.statusBar1.TabIndex = 7;
this.statusBar1.DrawItem += new System.Windows.Forms.StatusBarDrawItemEventHandler(this.statusBar1_DrawItem);
//
// statusBarPanel1
//
this.statusBarPanel1.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
this.statusBarPanel1.Width = 192;
//
// statusBarPanel2
//
this.statusBarPanel2.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
this.statusBarPanel2.Style = System.Windows.Forms.StatusBarPanelStyle.OwnerDraw;
this.statusBarPanel2.Text = "statusBarPanel2";
this.statusBarPanel2.Width = 192;
//
// toolBar1
//
this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
this.toolBarButtonBack,
this.toolBarButtonForward,
this.toolBarButtonStop,
this.toolBarButtonNavigate,
this.toolBarButtonRefresh});
this.toolBar1.DropDownArrows = true;
this.toolBar1.ImageList = this.imageList1;
this.toolBar1.Location = new System.Drawing.Point(0, 0);
this.toolBar1.Name = "toolBar1";
this.toolBar1.ShowToolTips = true;
this.toolBar1.Size = new System.Drawing.Size(400, 28);
this.toolBar1.TabIndex = 8;
this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
//
// toolBarButtonBack
//
this.toolBarButtonBack.Enabled = false;
this.toolBarButtonBack.ImageIndex = 0;
this.toolBarButtonBack.ToolTipText = "Back";
//
// toolBarButtonForward
//
this.toolBarButtonForward.Enabled = false;
this.toolBarButtonForward.ImageIndex = 1;
this.toolBarButtonForward.ToolTipText = "Forward";
//
// toolBarButtonStop
//
this.toolBarButtonStop.ImageIndex = 2;
this.toolBarButtonStop.ToolTipText = "Stop";
//
// toolBarButtonNavigate
//
this.toolBarButtonNavigate.ImageIndex = 4;
this.toolBarButtonNavigate.ToolTipText = "Go";
//
// imageList1
//
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(264, 40);
this.progressBar1.Name = "progressBar1";
this.progressBar1.TabIndex = 9;
//
// splitter1
//
this.splitter1.Dock = System.Windows.Forms.DockStyle.Top;
this.splitter1.Location = new System.Drawing.Point(0, 48);
this.splitter1.Name = "splitter1";
this.splitter1.Size = new System.Drawing.Size(400, 3);
this.splitter1.TabIndex = 10;
this.splitter1.TabStop = false;
//
// toolBarButtonRefresh
//
this.toolBarButtonRefresh.ImageIndex = 3;
this.toolBarButtonRefresh.ToolTipText = "Refresh";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(400, 302);
this.Controls.Add(this.axWebBrowser1);
this.Controls.Add(this.splitter1);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.toolBar1);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.statusBar1);
this.Name = "Form1";
this.Text = "シンプルブラウザ";
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).EndInit();
this.ResumeLayout(false);
}
#endregion
///
/// アプリケーションのメイン エントリ ポイントです。
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
///
/// 移動する
///
private void go()
{
object url = this.comboBox1.Text;
object flags = null;
object targetFrame = null;
object postData = null;
object header = null;
this.axWebBrowser1.Navigate2(ref url, ref flags, ref targetFrame, ref postData, ref header);
}
///
/// 中止
///
private void stop()
{
this.axWebBrowser1.Stop();
}
///
/// 進む
///
private void forward()
{
this.axWebBrowser1.GoForward();
}
///
/// 戻る
///
private void back()
{
this.axWebBrowser1.GoBack();
}
///
/// 戻る
///
private void refresh()
{
// データがロードされていないときに呼び出すと、
// TargetInvocationExceptionがあがるので、無視する。
try
{
this.axWebBrowser1.Refresh2();
}
catch
{
}
}
///
/// ページの更新が終わった。
///
private void axWebBrowser1_DocumentComplete(object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
{
// フォームにページタイトル、コンボボックスにURLを表示する。
displayPageInformation();
// コンボボックスにURLを追加する。
insertUrl();
}
///
/// フォームにページタイトル、コンボボックスにURLを表示する。
///
private void displayPageInformation()
{
this.Text = this.axWebBrowser1.LocationName;
this.comboBox1.Text = this.axWebBrowser1.LocationURL;
}
///
/// コンボボックスにURLを追加する。
///
private void insertUrl()
{
// コンボボックスのURLと入力したURLが違うなら、追加する。
if(comboBox1.Items.Count == 0 || (string)comboBox1.Items[0] != comboBox1.Text)
comboBox1.Items.Insert(0, comboBox1.Text);
// MaxDropDownItems より多い場合は、最後のものを削除する。
if(comboBox1.Items.Count > comboBox1.MaxDropDownItems)
comboBox1.Items.RemoveAt(comboBox1.Items.Count-1);
}
///
/// ツールバーボタンを押した。
///
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
if(e.Button == toolBarButtonBack)
back();
else if(e.Button == toolBarButtonForward)
forward();
else if(e.Button == toolBarButtonStop)
stop();
else if(e.Button == toolBarButtonNavigate)
go();
else if(e.Button == toolBarButtonRefresh)
refresh();
}
///
/// コンボボックスでリターンキーを押した。
///
private void comboBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar == '\r')
{
// リターンを押したときの音を消す。
e.Handled = true;
// 移動する。
go();
}
}
///
/// コンボボックスからURLを変更した。
///
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
// 移動する。
go();
}
private void axWebBrowser1_CommandStateChange(object sender, AxSHDocVw.DWebBrowserEvents2_CommandStateChangeEvent e)
{
const uint CSC_UPDATECOMMANDS = 0xFFFFFFFF;
const uint CSC_NAVIGATEFORWARD = 0x00000001;
const uint CSC_NAVIGATEBACK = 0x00000002;
switch((uint)e.command)
{
case CSC_NAVIGATEFORWARD:
this.toolBarButtonForward.Enabled = e.enable;
break;
case CSC_NAVIGATEBACK:
this.toolBarButtonBack.Enabled = e.enable;
break;
}
}
///
/// ProgressBarをStatusBar の statusBarPanel2 の中に表示する。
///
///
///
private void statusBar1_DrawItem(object sender, System.Windows.Forms.StatusBarDrawItemEventArgs sbdevent)
{
if (sbdevent.Panel == this.statusBarPanel2)
{
StatusBar sb = (StatusBar)sender;
Rectangle rect = sbdevent.Bounds;
this.progressBar1.SetBounds(sb.Location.X + rect.X, sb.Location.Y + rect.Y,
rect.Width, rect.Height);
}
}
///
/// プログレスバーに、進度を表示する。
///
///
///
private void axWebBrowser1_ProgressChange(object sender, AxSHDocVw.DWebBrowserEvents2_ProgressChangeEvent e)
{
this.progressBar1.Maximum = e.progressMax;
this.progressBar1.Value = e.progress;
}
///
/// ステータスが変化した。
///
///
///
private void axWebBrowser1_StatusTextChange(object sender, AxSHDocVw.DWebBrowserEvents2_StatusTextChangeEvent e)
{
System.Diagnostics.Debug.WriteLine(e.text);
this.statusBarPanel1.Text = e.text;
}
}
} |