本帖最后由 happy09li 于 2013-07-19 09:46:54 编辑

解决方案 »

  1.   

    奇怪哦,好像是转换成DesignMode没有完成。
    下面代码中htmlEditor_DocumentCompleted中的设置就不起作用,但是点击下按钮button1_Click,就ok了。
            private void button1_Click(object sender, EventArgs e)
            {
                bool res = doc.execCommand("FontName", true, "Arial");
                res = doc.execCommand("FontSize", true, 12);
            }        private void htmlEditor_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                htmlEditor.Focus();
                doc = htmlEditor.Document.DomDocument as IHTMLDocument2;
                doc.designMode = "On";
                bool res = doc.execCommand("FontName", true, "Arial");
                res = doc.execCommand("FontSize", true, 12);        }
      

  2.   


    htmlEditor_DocumentCompleted
    是不起作用,我在加载的是就设置了
      

  3.   

    又测试了一下,基本上是Webbrowser的状态还不是Complete,因此调用execCommand失败。
    下面的是可以的:private void htmlEditor_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                htmlEditor.Focus();
                doc = htmlEditor.Document.DomDocument as IHTMLDocument2;
                doc.designMode = "On";
                bToChangeFond = true;   //这里doc.readyState是loading.设置定时器等状态为complete再执行execCommand
                timer1.Start();
            }        private void timer1_Tick(object sender, EventArgs e)
            {
                if (bToChangeFond)
                {
                    if (doc.readyState == "complete")
                    {
                        bool res = doc.execCommand("FontName", true, "Arial");
                        res = doc.execCommand("FontSize", true, 12);
                        bToChangeFond = false;
                        timer1.Stop();
                    }
                }        }
    你可以看一下IHTMLDocument2的onreadystatechange属性(事件?)。我对这方面也不太熟。
      

  4.   

    找到方法了:        private void Form1_Load(object sender, EventArgs e)
            {
                htmlEditor.DocumentText = "<html><body></body></html>";
                doc = htmlEditor.Document.DomDocument as IHTMLDocument2;
                doc.designMode = "On";
                bToChangeFond = true;
                //timer1.Start();
                DHTMLEventHandler myHandler = new DHTMLEventHandler(doc);
                myHandler.Handler += new DHTMLEvent(this.SetFont);
                doc.onreadystatechange = myHandler;
            }        private void SetFont(IHTMLEventObj e)
            {
                if (bToChangeFond)
                {
                    if (doc.readyState == "complete")
                    {
                        bool res = doc.execCommand("FontName", true, "Arial");
                        res = doc.execCommand("FontSize", true, 12);
                        bToChangeFond = false;
                    }
                }
            }    public delegate void DHTMLEvent(IHTMLEventObj obj);    [ComVisible(true)]
        public class DHTMLEventHandler
        {
            public DHTMLEvent Handler;        private IHTMLDocument2 Document;        public DHTMLEventHandler(IHTMLDocument2 doc)
            {
                Document = doc;
            }        [DispId(0)]
            public void Call()
            {
                Handler(Document.parentWindow.@event);
            }
        }
    onreadystatechange部分代码参考了讨论:
    http://stackoverflow.com/questions/7222281/assigning-onmouseover-event-to-my-htmldocument-is-not-working
      

  5.   

    刚调试了下 
    if (doc.readyState == "complete") 
    跑完了状态没有complete 只有interactive,loading 这2个状态
      

  6.   

    奇怪,我这里按顺序是:
    loading, interactive, complete。难道版本不同,你价格button,等段时间,进去看看状态是什么?实在不行也来个timer吧。
      

  7.   

    换了中方法,直接在html代码中设置字体和大小,虽然有点缺陷,但是显示是正常的