使用WebBrowser与msHtml开发WinForms下的HtmlEditor控件
引用COM控件WebBrowser
引用msHtml。
BS,还在用vs2003。要是使用2005就不需要使用COM控件。
初始化:
With Me.AxWebBrowser1
.Navigate("about:blank")
doc = CType(.Document, mshtml.IHTMLDocument2)
doc.designMode = "on"
End With
bs:designMode居然是一个隐藏属性,找了半天都没有发现,还以为资料有误呢。
常用命令:
Case "加粗"
doc.execCommand("Bold", False, Nothing)
Case "倾斜"
doc.execCommand("Italic", False, Nothing)
Case "下划线"
doc.execCommand("Underline", False, Nothing)
Case "序列"
doc.execCommand("InsertOrderedList", False, Nothing)
Case "园点"
doc.execCommand("InsertUnorderedList", False, Nothing)
Case "减少缩进"
doc.execCommand("Outdent", False, Nothing)
Case "增加缩进"
doc.execCommand("Indent", False, Nothing)
Case "左对齐"
doc.execCommand("JustifyLeft", False, Nothing)
Case "居中"
doc.execCommand("JustifyCenter", False, Nothing)
Case "右对齐"
doc.execCommand("JustifyRight", False, Nothing)
' webBrowserBody.Document.ExecCommand("JustifyFull", false, null);
Case "超链接"
doc.execCommand("CreateLink", True, Nothing)
Case "图片"
doc.execCommand("InsertImage", True, Nothing)
Case "撤消"
doc.execCommand("Undo")
Case "重做"
doc.execCommand("Redo")
查询状态
Me.tb加粗.Pushed = doc.queryCommandState("Bold")
Me.tb倾斜.Pushed = doc.queryCommandState("Italic")
Me.tb下划线.Pushed = doc.queryCommandState("Underline")
Me.tb序列.Pushed = doc.queryCommandState("InsertOrderedList")
Me.tb园点.Pushed = doc.queryCommandState("InsertUnorderedList")
Me.tb左对齐.Pushed = doc.queryCommandState("JustifyLeft")
Me.tb居中.Pushed = doc.queryCommandState("JustifyCenter")
Me.tb右对齐.Pushed = doc.queryCommandState("JustifyRight")
返回设置控件内容:
Public Overrides Property Text() As String
Get
Debug.WriteLine(Me.AxWebBrowser1.ReadyState)
Return Me.doc.body.innerHTML
End Get
Set(ByVal Value As String)
Debug.WriteLine(Me.AxWebBrowser1.ReadyState)
Me.doc.body.innerHTML = Value
End Set
End Property
注意:如果是设置控件的内容,必须查询Me.AxWebBrowser1.ReadyState的状态为READYSTATE_COMPLETE才可以,不然body=Nothing.
如果判断是否加载完成,AxWebBrowser1_NavigateComplete2事件引发时说明已加载完成。