Code Repo    |     RSS
MD's Technical Sharing



Saturday, September 13, 2008

Using System.Windows.Forms.WebBrowser.ActiveXInstance

Expanding the WebBrowser control functionalities by using its ActiveXInstance property.


The WebBrowser control has a property called ActiveXInstance which is its run-time ActiveX instance and exposes many other methods/properties not available in the main control.

However, since ActiveXInstance is Object and requires late-binding to access all its methods and properties, Option Strict needs to be turned off in order to use it.

To use the WebBrowser extra functionalities: call WebBrowser.ActiveXInstance.ExecBB()

Define enums for use with ExecBB:

Private Enum Exec
OLECMDID_OPTICAL_ZOOM = 63
OLECMDID_SELECTALL = 17
OLECMDID_COPY = 12
OLECMDID_CLEARSELECTION = 18
End Enum

Private Enum ExecOpt
OLECMDEXECOPT_DODEFAULT = 0
OLECMDEXECOPT_PROMPTUSER = 1
OLECMDEXECOPT_DONTPROMPTUSER = 2
OLECMDEXECOPT_SHOWHELP = 3
End Enum

Copy selected text to clipboard:

Me.WebSMSView.ActiveXInstance.ExecWB(Exec.OLECMDID_COPY, ExecOpt.OLECMDEXECOPT_DODEFAULT)

Select all text:

Me.WebSMSView.ActiveXInstance.ExecWB(Exec.OLECMDID_SELECTALL, ExecOpt.OLECMDEXECOPT_DODEFAULT)

Zoom to 75%:

Me.WebSMSView.ActiveXInstance.ExecWB(Exec.OLECMDID_OPTICAL_ZOOM, ExecOpt.OLECMDEXECOPT_DODEFAULT, 75, 0)

To use a custom context menu in the WebBrowser control, the default context menu needs to be disabled by setting WebBrowser.IsWebBrowserContextMenuEnabled to false.


Reference:


  • Copy the contents of the WebBrowser control to the Clipboard
  • Using ExecWB with the native .NET 2.0 WebBrowser control
  • Allow only Copy/Paste Context Menu in System.Windows.Forms.WebBrowser Control
  • ExecWB Method
  • 4 comments:

    1. Hi, I know this is a long shot b/c this article is so old, but I'm having issues getting the SelectAll to work with my ActiveXInstance. I keep getting the error "Trying to revoke a drop target that has not been registered." Does anyone know a way to fix that error? Thanks!
      Tim

      ReplyDelete
    2. Hi,

      This error happens usually because you try to access the WebBrowser control ActiveX instance before it is ready. Depending on your use case, you can try to wait for a while before calling SelectAll, or use QueryStatusWB to check when it's ready, see this for more info - http://www.vbforums.com/showthread.php?625308-WebBrowser-Error-Trying-to-revoke-a-drop-target-that-has-not-been-registered

      Another issue could be incompatible argument type for the 3rd and 4th parameter. Depending on your language (VB or C#), you can try to pass either IntPtr.Zero or null and see if it makes a different. Refer to this https://social.msdn.microsoft.com/Forums/en-US/83fd2599-1afa-4435-8fff-95708ac18c1e/trying-to-revoke-a-drop-target-that-has-not-been-registered-exception-from-hresult-0x80040100?forum=vblanguage for more info

      Let me know if this helps

      ReplyDelete
    3. Wow, I apologize. I completely forgot to check back here for your answer. I was not able to get the SelectAll to work, even with the ideas you suggested. I was using VB.net. However, I was able to get the SaveAs command to work and just used a PDF parsing method to grab all the text. Thanks for your response though. I really appreciate it!
      Tim

      ReplyDelete
      Replies
      1. No problem, glad you got it to work and thanks for sharing your workaround too :)

        Delete

    Note: Only a member of this blog may post a comment.