private void Form1_Load(object sender, EventArgs e)
{
string HTML_TEXT = "<html><head><title>未命名</title></head><body style=\"margin: 5px\"></body></html>";
webBrowser1.Navigate("about:blank");
webBrowser1.Document.OpenNew(true);
webBrowser1.Document.Write(HTML_TEXT);
webBrowser1.Document.Focus();
webBrowser1.Document.ExecCommand("EditMode", false, true);
IsModified = false;
}
using System;
using System.Text;
using System.Windows.Forms;
using mshtml;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string HTML_TEXT = "<html><head><title>未命名</title></head><body style=\"margin: 5px\"></body></html>";
webBrowser1.Navigate("about:blank");
webBrowser1.Document.OpenNew(true);
webBrowser1.Document.Write(HTML_TEXT);
webBrowser1.Document.Focus();
webBrowser1.Document.ExecCommand("EditMode", false, true);
IsModified = false;
}
private void NewDocument()
{
string HTML_TEXT = "<html><head><title>未命名</title></head><body style=\"margin: 5px\"></body></html>";
webBrowser1.Document.OpenNew(true);
webBrowser1.Document.Write(HTML_TEXT);
webBrowser1.Document.Focus();
webBrowser1.Document.ExecCommand("EditMode", false, true);
IsModified = false;
}
bool __modified = false;
bool firsttime = true;
private string s ="";
/// <summary>
/// Webbrowser文档内容是否被修改
/// </summary>
public bool IsModified
{
get
{
return __modified;
}
set
{
__modified = value;
if (value == false)
s = webBrowser1.DocumentText;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
#region firsttime 说明
/*
这是我的给的HTML
html><head><title>未命名</title></head><body style=\"margin: 5px\"></body></html>
而实际出来的HTML是这样
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>未命名</TITLE>
<META content="text/html; charset=unicode" http-equiv=Content-Type>
<META name=GENERATOR content="MSHTML 11.00.9600.16384"></HEAD>
<BODY style="MARGIN: 5px"></BODY></HTML>
*/
#endregion
if (firsttime == true)
{
s = webBrowser1.DocumentText;
firsttime = false;
}
//test
if (webBrowser1.DocumentText != s)
{
Text = "内容已被修改";
richTextBox2.Text = webBrowser1.DocumentText;
}
else
{
Text = " Form1 ";
richTextBox1.Text = webBrowser1.DocumentText;
}
//getmodified
__modified = (webBrowser1.DocumentText != s);
}
//set modify false
private void button1_Click(object sender, EventArgs e)
{
s = webBrowser1.DocumentText;
}
private void button2_Click(object sender, EventArgs e)
{
if (IsModified)
{
DialogResult d = MessageBox.Show("文档已被修改,是否保存", "编辑器", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (d == DialogResult.Yes)
{
MessageBox.Show("File have been saved!");
}
}
NewDocument();
}
private void button3_Click(object sender, EventArgs e)
{
webBrowser1.Document.ExecCommand("EditMode", false, true);
}
private void button4_Click(object sender, EventArgs e)
{
webBrowser1.Document.ExecCommand("BrowseMode", false, true);
s = webBrowser1.DocumentText;
}
}
}