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;
using System.IO;
namespace 调用web浏览器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load += new EventHandler(Form_Load);
this.button1.Click += new EventHandler(Savebtn_Click);
this.webBrowser1.DocumentCompleted += new
WebBrowserDocumentCompletedEventHandler(InsertScript);
}
private void Form_Load(object sender, EventArgs e)
{
string url = ConfigHelper.GetEditorUrl();
Uri u = new Uri(url);
webBrowser1.Url = u;
//webBrowser1.DocumentText = CreateHtmlString("", 500, 300);
}
/// <summary>
/// 在页面加载完成后,向页面中注入脚本
/// </summary>
private void InsertScript(object sender,WebBrowserDocumentCompletedEventArgs e)
{
}
/// <summary>
/// 动态写document
/// </summary>
/// <param name="s"></param>
/// <param name="width"></param>
/// <param name="height"></param>
/// <returns></returns>
private string CreateHtmlString(string s,int width,int height)
{
StringBuilder sb = new StringBuilder();
//sb.Append("<!doctype html>");
//sb.Append("<html>");
head
//sb.Append("<head>");
//sb.Append("<meta charset=\"utf-8\" />");
//string url = ConfigHelper.GetEditorUrl();
//if (url.EndsWith("/"))
//{
// url = url.Substring(0, url.Length - 1);
//}
//sb.Append("<script charset=\"utf-8\" src=\""+url+"/kindeditor-min.js\"></script>");
//sb.Append("<script charset=\"utf-8\" src=\""+url+"/lang/zh_CN.js\"></script>");
//sb.Append("<script>");
//sb.Append("var editor;\r\n");
//sb.Append("KindEditor.ready(function(K) {\r\n");
//sb.Append("editor = K.create('textarea[id=\"content\"]', {\r\n");
//sb.Append("allowFileManager : true\r\n");
//sb.Append("});\r\n");
//sb.Append("})\r\n");
//sb.Append("</script>");
//sb.Append("</head>");
body
//sb.Append("<body>");
//sb.Append("<textarea id=\"content\" style=\"width:"+width+";height:"+height+"\" cols=\"40\" rows=\"30\">");
//if (!string.IsNullOrEmpty(s))
//{
// sb.Append(s);
//}
//sb.Append("</textarea>");
//sb.Append("<script type=\"text/javascript\">\r\n");
//sb.Append("function GetHtml(){\r\n");
//sb.Append("var html = editor.html();\r\n");
//sb.Append("editor.sync();\r\n");
//sb.Append("document.getElementById(\"content\").value=html;\r\n");
//sb.Append("}\r\n");
//sb.Append("</script>\r\n");
//sb.Append("</body>");
//sb.Append("</html>");
return sb.ToString();
}
private void Savebtn_Click(object sender, EventArgs e)
{
HtmlDocument htd = webBrowser1.Document;
htd.InvokeScript("GetHtml");
string info =string.Empty;
HtmlElement he = webBrowser1.Document.GetElementById("content");
info = he.OuterText;
string path = AppDomain.CurrentDomain.BaseDirectory;
path = Path.Combine(path, "txt.txt");
using (StreamWriter sw = new StreamWriter(path))
{
sw.WriteLine(info);
sw.Close();
sw.Dispose();
}
MessageBox.Show("保存成功");
}
}
}
c#取webbrowser中的控件值(这里是KindEditor)
最新推荐文章于 2023-03-17 00:00:37 发布