wWeb.cs
using System; using System.Drawing; using System.Windows.Forms; using System.Collections.Generic; public class WWebBrowser : WCtrl { ToolStripTextBox addr; Panel panel; WebBrowser web; public WWebBrowser( bool toolbar ) { ctrl = web = new WebBrowser(); web.Anchor = AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Left; web.Dock = DockStyle.Fill; web.ScrollBarsEnabled = true; panel = new Panel(); panel.Anchor = AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Left; panel.Dock = DockStyle.Fill; panel.Controls.Add(web); if( toolbar ) { makeToolBar(); } } public ToolStripButton addToolbarButton(ToolStrip tool, String text, EventHandler handler) { ToolStripButton button = new ToolStripButton(); button.Name = text; button.Text = text; button.Click += handler; tool.Items.Add(button); return button; } public ToolStripTextBox addToolbarTextBox(ToolStrip tool, String text, KeyEventHandler handler) { ToolStripTextBox textbox = new ToolStripTextBox(); textbox.Name = text; textbox.Text = ""; textbox.Anchor = AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom | AnchorStyles.Left; textbox.Dock = DockStyle.Fill; if (handler != null) { textbox.KeyDown += handler; } tool.Items.Add(textbox); return textbox; } private void makeToolBar() { ToolStrip tool2 = new ToolStrip(); this.addr = addToolbarTextBox(tool2, "addr", addrHandler); addToolbarButton(tool2, "go", goHandler).Alignment = ToolStripItemAlignment.Right; panel.Controls.Add(tool2); ToolStrip tool1 = new ToolStrip(); addToolbarButton(tool1, "reload", reloadHandler); addToolbarButton(tool1, "back", backHandler); addToolbarButton(tool1, "forward", forwardHandler); addToolbarButton(tool1, "text", textHandler); addToolbarButton(tool1, "html", htmlHandler); panel.Controls.Add(tool1); } public void loadUrl(String url) { web.Navigate(url); } public String getHtml() { return web.DocumentText; } public String getInnerText() { List<String> htmls = getText("html"); return htmls[0]; } public String getInnerHtml() { List<String> htmls = getHtmlText("html"); return htmls[0]; } public List<String> getText(String tag) { //html, body, img ... List<String> htmls = new List<String>(); HtmlElementCollection elm = web.Document.GetElementsByTagName(tag); foreach( HtmlElement html in elm ) { htmls.Add(html.InnerText); } return htmls; } public List<String> getHtmlText(String tag) { List<String> htmls = new List<String>(); HtmlElementCollection elm = web.Document.GetElementsByTagName(tag); foreach( HtmlElement html in elm ) { htmls.Add(html.InnerHtml); } return htmls; } private void reloadHandler(object source, EventArgs e) { web.Refresh(); } private void backHandler(object source, EventArgs e) { if( web.CanGoBack ) { web.GoBack(); } } private void forwardHandler(object source, EventArgs e) { if (web.CanGoForward) { web.GoForward(); } } private void textHandler(object source, EventArgs e) { Clipboard.SetText(getInnerText()); } private void htmlHandler(object source, EventArgs e) { Clipboard.SetText(getInnerHtml()); } private void addrHandler(object source, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { web.Navigate(addr.Text); } } private void goHandler(object source, EventArgs e) { web.Navigate(addr.Text); } }
댓글 없음:
댓글 쓰기