webBrowser首次加载不能正确显示?

    /// <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());
        }

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值