vb.net 如何使用 webbrowser 控件编辑 html?(开启所见即所得功能)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13768310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to edit html using the webbrowser control? (turn on WYSIWYG features)
提问by jeffery
Similar to a WYSIWYG(What you see is what you get) editor I want the user to be able to visually edit the html document and move objects around. How can I turn on these html editing features for the web browser control?
与 WYSIWYG(所见即所得)编辑器类似,我希望用户能够直观地编辑 html 文档并四处移动对象。如何为 Web 浏览器控件打开这些 html 编辑功能?
The environment I have is Visual Studio 2010, Windows 7 64-bit.
我的环境是 Visual Studio 2010,Windows 7 64 位。
I want to enable the html editing features of the web browser control (http://msdn.microsoft.com/en-us/library/aa752040%28v=VS.85%29.aspx).
我想启用 Web 浏览器控件的 html 编辑功能(http://msdn.microsoft.com/en-us/library/aa752040%28v=VS.85%29.aspx)。
Note: the code for the web browser control being is being used in a specialized editor and is too large and numerous to post here but I can post a link to it later if anyone wants the full source. This is a large project please refer to here as I attempted to converse and search google before posting their or here: http://social.msdn.microsoft.com/Forums/vstudio/en-US/1e5acdb2-9366-4258-890a-86eaaa1086ee/html-expert-needed.
注意:Web 浏览器控件的代码正在一个专门的编辑器中使用,并且太大和太多无法在此处发布,但如果有人想要完整源代码,我可以稍后发布指向它的链接。这是一个大型项目,请参阅此处,因为我在发布它们或此处之前尝试与谷歌交谈和搜索:http: //social.msdn.microsoft.com/Forums/vstudio/en-US/1e5acdb2-9366-4258-890a -86eaaa1086ee/html-expert-needed。
回答by Pradeep Kumar
The WebBrowser control has a built-in WYSIWYGmini-HTMLeditor. You can use it. Here's an example to how to turn that edit mode on:
WebBrowser 控件具有内置的WYSIWYG迷你HTML编辑器。你可以使用它。以下是如何打开该编辑模式的示例:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
' I do this for this example, so that we have some elements loaded.
' For you, you will need to add the tags from your code for various HTML elements.
WebBrowser1.Navigate("http://google.com")
End Sub
Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
' To turn On the edit mode.
Dim axObj As New Object
axObj = WebBrowser1.ActiveXInstance
axObj.document.designmode = "On"
End Sub
回答by jeffery
For new people reading this here's the relevant code section that finally solved everything which I am quoting from the msdn forums post above:
对于阅读本文的新人,这里是相关代码部分,它最终解决了我从上面的 msdn 论坛帖子中引用的所有内容:
Private Sub wb_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles wb.DocumentCompleted ' Tabp.wb_DocumentCompleted - Internet Related Document completion routine
If doc IsNot Nothing Then If m_EditMode = True Then doc.designMode = "On" Else doc.designMode = "Off" End If End If If wb.Document IsNot Nothing Then HTMLDOC = wb.Document If wb.CanGoBack Then mbBack.ImageIndex = ImglstImages.cVLeftArrowQuiescent mbBack.Enabled = True Else mbBack.ImageIndex = ImglstImages.cVLeftArrowGreyed mbBack.Enabled = False End If If wb.CanGoForward Then mbforward.Enabled = True mbforward.ImageIndex = ImglstImages.cVRightArrowQuiescent Else mbforward.Enabled = False mbforward.ImageIndex = ImglstImages.cVRightArrowGreyed End If wb.AllowNavigation = False cmbxAddressbar.Text = wb.Url.ToString AddIfUnique(cmbxAddressbar.Text) If Form1.GetClassIdentifier = m_ClassIdentifier Then Form1.ProgBar.Visible = False wb.AllowNavigation = True PU.Clear() ''''' wb.Focus() End Sub
Private Sub wb_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) 处理 wb.DocumentCompleted ' Tabp.wb_DocumentCompleted - Internet 相关文档完成例程
If doc IsNot Nothing Then If m_EditMode = True Then doc.designMode = "On" Else doc.designMode = "Off" End If End If If wb.Document IsNot Nothing Then HTMLDOC = wb.Document If wb.CanGoBack Then mbBack.ImageIndex = ImglstImages.cVLeftArrowQuiescent mbBack.Enabled = True Else mbBack.ImageIndex = ImglstImages.cVLeftArrowGreyed mbBack.Enabled = False End If If wb.CanGoForward Then mbforward.Enabled = True mbforward.ImageIndex = ImglstImages.cVRightArrowQuiescent Else mbforward.Enabled = False mbforward.ImageIndex = ImglstImages.cVRightArrowGreyed End If wb.AllowNavigation = False cmbxAddressbar.Text = wb.Url.ToString AddIfUnique(cmbxAddressbar.Text) If Form1.GetClassIdentifier = m_ClassIdentifier Then Form1.ProgBar.Visible = False wb.AllowNavigation = True PU.Clear() ''''' wb.Focus() End Sub
This is all I have that address's the question. To conclude their were two main problems with the code this was used for:
这就是我有那个地址的问题。总而言之,他们使用的代码存在两个主要问题:
- The html editing feature was disabled on windows 7 sp1 prohibiting usage (before the below answer the original author I was helping failed to correctly code his/her HTML editor).
- The original author of the code did not include a document_completed event making the code before fail to work correctly in the new windows 7 sp1 environment. Note: this is a personal coding project and yes the author gave me permission to use the code and/or edit it as desired. Note: this is the orginal answer from above used. I wanted to show my code so everyone did not think I was doing this out of thin air.
- html 编辑功能在 windows 7 sp1 上被禁用,禁止使用(在下面的回答之前,我帮助原作者未能正确编码他/她的 HTML 编辑器)。
- 代码的原作者没有包含一个 document_completed 事件,使代码之前无法在新的 windows 7 sp1 环境中正常工作。注意:这是一个个人编码项目,是的,作者允许我使用代码和/或根据需要对其进行编辑。注意:这是上面使用的原始答案。我想展示我的代码,所以每个人都不会认为我是凭空做的。

