问题
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?
The environment I have is Visual Studio 2010, Windows 7 64-bit.
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).
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.
回答1:
The WebBrowser control has a built-in WYSIWYG mini-HTML editor. You can use it. Here's an example to how to turn that edit mode on:
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
回答2:
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:
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
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.
来源:https://stackoverflow.com/questions/13768310/how-to-edit-html-using-the-webbrowser-control-turn-on-wysiwyg-features