Thursday, May 22, 2008

How to add/modify contextmenu of URLPresenter/IWebBrowser2?

I just found a newgroup entry asking how to "add/modify contextmenu of URLPresenter". So it's time again to give some examples to a goodie I wrote.

URLPresenterWithContextMenu (in package "US Internet Explorer Extensions") allows you to control which context menu is displayed in a WebControl:
  • If the presenters view #allowContextMenu aspect is false no context menu will be shown. Neither a custom one nor the default one.
  • If the presenters view #allowContextMenu aspect is true it depends on #contextMenu. If #contextMenu is not nil it will be displayed. If it is nil the default one will be used.

Behind the scenes this presenter implements the IDocHostUIHandlerDispatch interface to get queries for some UI events. The interesting one here is ShowContextMenu() which allows you to allow/prevent the display of the default context menu. I'm using this method for the logic using #allowContextMenu and #contextMenu.

However for using this presenter no digging into COM is needed. Simply use #allowContextMenu and #contextMenu as written above.

No comments: