using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
toolStripTextBox1.Text = "https://w...content-available-to-author-only...o.jp/";
webBrowser1.Navigating += new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
//webBrowser1.NewWindow += new CancelEventHandler(webBrowser1_NewWindow);
//treeView1.NodeMouseClick += new TreeNodeMouseClickEventHandler(treeView1_NodeMouseClick);
treeView1.AfterSelect += new TreeViewEventHandler(treeView1_AfterSelect);
}
void webBrowser1_NewWindow(object sender, CancelEventArgs e)
{
e.Cancel = true;
}
int objcount = 0;
HtmlElement oldelem;
string oldstring;
private void toolStripButton1_Click(object sender, EventArgs e)
{
try
{
objcount = 0;
this.Enabled = false;
webBrowser1.SuspendLayout();
webBrowser1.Navigate(toolStripTextBox1.Text);
}
catch
{
statusStrip1.Text = "URLが不正です。" +
toolStripTextBox1.Text;
this.Enabled = true;
}
}
void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
objcount++;
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
objcount--;
if (objcount == 0)
{
treeView1.SuspendLayout();
treeView1.Nodes.Clear();
TreeNode tnBody = new TreeNode();
tnBody.Text = "Body";
tnBody.Tag = webBrowser1.Document.Body;
this.NodeSet(tnBody, webBrowser1.Document.Body);
treeView1.Nodes.Add(tnBody);
//treeView1.ExpandAll();
this.Enabled = true;
//webBrowser1.ScrollBarsEnabled = false;
treeView1.ResumeLayout();
webBrowser1.ResumeLayout();
}
}
//htmlDocument再帰的に調べてノードを作成
void NodeSet(TreeNode tn, HtmlElement elem)
{
foreach (HtmlElement e in elem.Children)
{
TreeNode t = new TreeNode();
t.Text = e.TagName;
t.Tag = e;
tn.Nodes.Add(t);
this.NodeSet(t, e);
}
}
void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
//e.Node.Expand();
this.DataSet(e.Node);
}
void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
this.DataSet(e.Node);
}
void DataSet(TreeNode node)
{
if (oldelem != null) oldelem.Style = oldstring;
textBox1.Clear();
if (node.Tag == null) return;
HtmlElement elem = (HtmlElement)node.Tag;
//this.TextSet("InnerHtml:" + elem.InnerHtml);
//this.TextSet("InnerText:" + elem.InnerText);
//this.TextSet("OuterHtml:" + elem.OuterHtml);
//this.TextSet("OuterText:" + elem.OuterText);
textBoxInnerHtml.Text = elem.InnerHtml;
textBoxInnerText.Text = elem.InnerText;
textBoxOuterHtml.Text = elem.OuterHtml;
textBoxOuterText.Text = elem.OuterText;
this.TextSet("[Id ]:" + elem.Id);
if (elem.Name != string.Empty && elem.Name.Length != 0)
this.TextSet("[Name ]:" + elem.Name);
this.TextSet("[Style ]:" + elem.Style);
this.TextSet("[TagName ]:" + elem.TagName);
this.TextSet("[ClientRectangle]:" + elem.ClientRectangle.ToString());
this.TextSet("[OffsetRectangle]:" + elem.OffsetRectangle.ToString());
this.TextSet("[ScrollRectangle]:" + elem.ScrollRectangle.ToString());
this.TextSet("[Children ]:" + elem.Children.Count.ToString());
webBrowser1.Document.Window.ScrollTo(elem.OffsetRectangle.X, elem.OffsetRectangle.Y);
//HtmlElementのborder機能を使って枠線を実現してるので
//HtmlElementを切り替えた場合に元のStyleに戻すために
oldstring = elem.Style;
oldelem = elem;
elem.Style += "border:solid";
}
void TextSet(string s)
{
textBox1.AppendText(s + Environment.NewLine);
}
}
}