1. WebView Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | using System; using System.Windows.Forms; using System.Collections.Generic; namespace CsLib { class CsWebView { Panel panel; WebBrowser web; ToolStripTextBox addr; public CsWebView() { Initialize(); } public CsWebView(Boolean toolbar) { Initialize(); if (toolbar) { makeToolbar(); } } private void Initialize() { 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); } public Panel get() { return panel; } 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; } public void makeToolbar() { ToolStrip tool = new ToolStrip(); addToolbarButton(tool, "reload", reloadHandler); addToolbarButton(tool, "back", backHandler); addToolbarButton(tool, "forward", forwardHandler); addToolbarButton(tool, "text", textHandler); addToolbarButton(tool, "html", htmlHandler); ToolStrip tool2 = new ToolStrip(); addr = addToolbarTextBox(tool2, "addr", addrHandler); addToolbarButton(tool2, "go", goHandler); panel.Controls.Add(tool2); panel.Controls.Add(tool); } 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); } } } |