/// <summary>
/// 开启对COM的访问
/// </summary>
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.Navigate("about:blank");
webBrowser1.Document.Write("<html><head><script language=\"javascript\" type=\"text/javascript\">function AddMsg(str) { divMsg.innerHTML = divMsg.innerHTML + str; }function DelMsg(id) {divMsg.removeChild(document.getElementById(id));window.external.DelMsg(id);}</script><style>body,div{margin-top:0;}</style></head><body style=\"background-color:AntiqueWhite;\"><div id=\"divMsg\"></div></body></html>");
//初始化页面
webBrowser1.ObjectForScripting = this;
}
int bj = 1;
/// <summary>
/// 添加记录
/// </summary>
/// <param name="strInfo"></param>
public void AddMsg(string strInfo)
{
object obj = new object();
lock (obj)
{
string[] strRow = new string[]{
"<div id=\"tb"+bj.ToString()+"\" style=\"font-family:宋体;font-size:9pt;\">" +
bj.ToString()+"-"+strInfo+" <a style=\"text-decoration:none;cursor:pointer;color:DeepSkyBlue;\" οnclick=\"DelMsg('tb"+bj.ToString()+"')\">查看</a>" +//查看后删掉
"</div>"};
bj++;
webBrowser1.Document.InvokeScript("AddMsg", strRow);
}
}
/// <summary>
/// 添加记录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
AddMsg("猜猜我是谁");
}
/// <summary>
/// JS调必须为public
/// </summary>
/// <param name="id"></param>
public void DelMsg(object id)
{
MessageBox.Show(id.ToString());
}
}