By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,948 Members | 1,559 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,948 IT Pros & Developers. It's quick & easy.

Web Browser Disable Right Click

P: n/a
Hi All,

I'm sure this must have been asked a million times due to it's usefulness,
but are there any sensible solutions for disabling right click (and other
services such as Ctrl+P, Browser hotkeys etc) on a Web-Browser control in
VB.NET?

Despite Googling for ideas on and off for a week now, I only found one
solution and this was to implement IMessageFilter on the form and then trap
all the incoming messages. Unfortunately, this also prevents the user from
being able to move my application (something to do with trapping right
clicks), along with other useful things like being able to use the backspace
key!

Is there any other way of doing this? Or is there some way I can trap
messages that go into the Web-browser control BEFORE it processes them and
AFTER my Form has? Surely somebody must have come up with an efficient way
of hosting a customized browser in a .NET WinForm!

TIA
Alex Clark
Nov 21 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
Don't know if this would be of any help, but in the VB world, the
WbCustomizer control that Microsoft made available could be used to
suppress any the context menu and/or speed keys (all or individual). The
WbCustomizer was provided with soource code (it was a c++ project). Given
that it came with source, perhaps that could be modified to work under the
DotNet environment.

"Alex Clark" <al**@theclarkhome.SPAMTIN.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi All,

I'm sure this must have been asked a million times due to it's usefulness,
but are there any sensible solutions for disabling right click (and other
services such as Ctrl+P, Browser hotkeys etc) on a Web-Browser control in
VB.NET?

Despite Googling for ideas on and off for a week now, I only found one
solution and this was to implement IMessageFilter on the form and then trap all the incoming messages. Unfortunately, this also prevents the user from being able to move my application (something to do with trapping right
clicks), along with other useful things like being able to use the backspace key!

Is there any other way of doing this? Or is there some way I can trap
messages that go into the Web-browser control BEFORE it processes them and
AFTER my Form has? Surely somebody must have come up with an efficient way of hosting a customized browser in a .NET WinForm!

TIA
Alex Clark

Nov 21 '05 #2

P: n/a
Don't know if this would be of any help, but in the VB world, the
WbCustomizer control that Microsoft made available could be used to
suppress any the context menu and/or speed keys (all or individual). The
WbCustomizer was provided with soource code (it was a c++ project). Given
that it came with source, perhaps that could be modified to work under the
DotNet environment.

"Alex Clark" <al**@theclarkhome.SPAMTIN.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi All,

I'm sure this must have been asked a million times due to it's usefulness,
but are there any sensible solutions for disabling right click (and other
services such as Ctrl+P, Browser hotkeys etc) on a Web-Browser control in
VB.NET?

Despite Googling for ideas on and off for a week now, I only found one
solution and this was to implement IMessageFilter on the form and then trap all the incoming messages. Unfortunately, this also prevents the user from being able to move my application (something to do with trapping right
clicks), along with other useful things like being able to use the backspace key!

Is there any other way of doing this? Or is there some way I can trap
messages that go into the Web-browser control BEFORE it processes them and
AFTER my Form has? Surely somebody must have come up with an efficient way of hosting a customized browser in a .NET WinForm!

TIA
Alex Clark

Nov 21 '05 #3

P: n/a
one easy way to block the right click is to put this code into the html page

<body oncontextmenu="Return false;">

there are more complicated ways of doing it from .NET, but this hasa always
worked fine for me.

well, this helps part of it...

Jay Feldman

"Alex Clark" wrote:
Hi All,

I'm sure this must have been asked a million times due to it's usefulness,
but are there any sensible solutions for disabling right click (and other
services such as Ctrl+P, Browser hotkeys etc) on a Web-Browser control in
VB.NET?

Despite Googling for ideas on and off for a week now, I only found one
solution and this was to implement IMessageFilter on the form and then trap
all the incoming messages. Unfortunately, this also prevents the user from
being able to move my application (something to do with trapping right
clicks), along with other useful things like being able to use the backspace
key!

Is there any other way of doing this? Or is there some way I can trap
messages that go into the Web-browser control BEFORE it processes them and
AFTER my Form has? Surely somebody must have come up with an efficient way
of hosting a customized browser in a .NET WinForm!

TIA
Alex Clark

Nov 21 '05 #4

P: n/a
one easy way to block the right click is to put this code into the html page

<body oncontextmenu="Return false;">

there are more complicated ways of doing it from .NET, but this hasa always
worked fine for me.

well, this helps part of it...

Jay Feldman

"Alex Clark" wrote:
Hi All,

I'm sure this must have been asked a million times due to it's usefulness,
but are there any sensible solutions for disabling right click (and other
services such as Ctrl+P, Browser hotkeys etc) on a Web-Browser control in
VB.NET?

Despite Googling for ideas on and off for a week now, I only found one
solution and this was to implement IMessageFilter on the form and then trap
all the incoming messages. Unfortunately, this also prevents the user from
being able to move my application (something to do with trapping right
clicks), along with other useful things like being able to use the backspace
key!

Is there any other way of doing this? Or is there some way I can trap
messages that go into the Web-browser control BEFORE it processes them and
AFTER my Form has? Surely somebody must have come up with an efficient way
of hosting a customized browser in a .NET WinForm!

TIA
Alex Clark

Nov 21 '05 #5

P: n/a
make that R lowercase:

"return false;"

You can use this to block other events too, but only in the HTML.

Jay Feldman

"Jay Feldman" wrote:
one easy way to block the right click is to put this code into the html page

<body oncontextmenu="Return false;">

there are more complicated ways of doing it from .NET, but this hasa always
worked fine for me.

well, this helps part of it...

Jay Feldman

"Alex Clark" wrote:
Hi All,

I'm sure this must have been asked a million times due to it's usefulness,
but are there any sensible solutions for disabling right click (and other
services such as Ctrl+P, Browser hotkeys etc) on a Web-Browser control in
VB.NET?

Despite Googling for ideas on and off for a week now, I only found one
solution and this was to implement IMessageFilter on the form and then trap
all the incoming messages. Unfortunately, this also prevents the user from
being able to move my application (something to do with trapping right
clicks), along with other useful things like being able to use the backspace
key!

Is there any other way of doing this? Or is there some way I can trap
messages that go into the Web-browser control BEFORE it processes them and
AFTER my Form has? Surely somebody must have come up with an efficient way
of hosting a customized browser in a .NET WinForm!

TIA
Alex Clark

Nov 21 '05 #6

P: n/a
make that R lowercase:

"return false;"

You can use this to block other events too, but only in the HTML.

Jay Feldman

"Jay Feldman" wrote:
one easy way to block the right click is to put this code into the html page

<body oncontextmenu="Return false;">

there are more complicated ways of doing it from .NET, but this hasa always
worked fine for me.

well, this helps part of it...

Jay Feldman

"Alex Clark" wrote:
Hi All,

I'm sure this must have been asked a million times due to it's usefulness,
but are there any sensible solutions for disabling right click (and other
services such as Ctrl+P, Browser hotkeys etc) on a Web-Browser control in
VB.NET?

Despite Googling for ideas on and off for a week now, I only found one
solution and this was to implement IMessageFilter on the form and then trap
all the incoming messages. Unfortunately, this also prevents the user from
being able to move my application (something to do with trapping right
clicks), along with other useful things like being able to use the backspace
key!

Is there any other way of doing this? Or is there some way I can trap
messages that go into the Web-browser control BEFORE it processes them and
AFTER my Form has? Surely somebody must have come up with an efficient way
of hosting a customized browser in a .NET WinForm!

TIA
Alex Clark

Nov 21 '05 #7

P: n/a
Hi Alex

The interface you need to implement is IDocHostUIHandler. It has a method
ShowContextMenu. If you return an HRESULT of S_OK in your implementation of
this method this will cancel the context menu on right-click. It also allows
you to implement your own context menu.

In your form that host the WebBrowser control, implement IDocHostUIHandler.
Then, in the DocumentComplete event, get the ICustomDoc interface from the
document object, and call SetUIHandler(Me). Finally, wait for the readystate
to go to complete.

You could have a look at this complete thread:

http://groups.google.com/groups?q=id...phx.gbl&rnum=7

HTH

Charles
"Alex Clark" <al**@theclarkhome.SPAMTIN.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi All,

I'm sure this must have been asked a million times due to it's usefulness,
but are there any sensible solutions for disabling right click (and other
services such as Ctrl+P, Browser hotkeys etc) on a Web-Browser control in
VB.NET?

Despite Googling for ideas on and off for a week now, I only found one
solution and this was to implement IMessageFilter on the form and then
trap all the incoming messages. Unfortunately, this also prevents the
user from being able to move my application (something to do with trapping
right clicks), along with other useful things like being able to use the
backspace key!

Is there any other way of doing this? Or is there some way I can trap
messages that go into the Web-browser control BEFORE it processes them and
AFTER my Form has? Surely somebody must have come up with an efficient
way of hosting a customized browser in a .NET WinForm!

TIA
Alex Clark

Nov 21 '05 #8

P: n/a
Hi Alex

The interface you need to implement is IDocHostUIHandler. It has a method
ShowContextMenu. If you return an HRESULT of S_OK in your implementation of
this method this will cancel the context menu on right-click. It also allows
you to implement your own context menu.

In your form that host the WebBrowser control, implement IDocHostUIHandler.
Then, in the DocumentComplete event, get the ICustomDoc interface from the
document object, and call SetUIHandler(Me). Finally, wait for the readystate
to go to complete.

You could have a look at this complete thread:

http://groups.google.com/groups?q=id...phx.gbl&rnum=7

HTH

Charles
"Alex Clark" <al**@theclarkhome.SPAMTIN.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi All,

I'm sure this must have been asked a million times due to it's usefulness,
but are there any sensible solutions for disabling right click (and other
services such as Ctrl+P, Browser hotkeys etc) on a Web-Browser control in
VB.NET?

Despite Googling for ideas on and off for a week now, I only found one
solution and this was to implement IMessageFilter on the form and then
trap all the incoming messages. Unfortunately, this also prevents the
user from being able to move my application (something to do with trapping
right clicks), along with other useful things like being able to use the
backspace key!

Is there any other way of doing this? Or is there some way I can trap
messages that go into the Web-browser control BEFORE it processes them and
AFTER my Form has? Surely somebody must have come up with an efficient
way of hosting a customized browser in a .NET WinForm!

TIA
Alex Clark

Nov 21 '05 #9

P: n/a
Alex Clark wrote:
Hi All,

I'm sure this must have been asked a million times due to it's usefulness,
but are there any sensible solutions for disabling right click (and other
services such as Ctrl+P, Browser hotkeys etc) on a Web-Browser control in
VB.NET?

Despite Googling for ideas on and off for a week now, I only found one
solution and this was to implement IMessageFilter on the form and then trap
all the incoming messages. Unfortunately, this also prevents the user from
being able to move my application (something to do with trapping right
clicks), along with other useful things like being able to use the backspace
key!

Is there any other way of doing this? Or is there some way I can trap
messages that go into the Web-browser control BEFORE it processes them and
AFTER my Form has? Surely somebody must have come up with an efficient way
of hosting a customized browser in a .NET WinForm!

TIA
Alex Clark


What's the point of disabling the rightmousebutton. I hate websites that
do that since I navigate by using the rightmousebutton (rightclick and
choose back in mozilla). What is it that you want to prevent? Sourcecode
stealing? Image stealing? If so, just disabling the rightmousebutton
isn't the way to go since that is just a wax nose. The user can still do
all those things from the menu (i.e. save page to disk).

--
Rinze van Huizen
C-Services Holland b.v.
Nov 21 '05 #10

P: n/a
Hi,

This is not for a website, but rather a Windows Forms application which uses
an HTML based GUI. Whereas I agree that disabling right-click on a website
is bad UI practice (and not really a preventative measure), it is
innappropriate in this case for me to allow a user to right click on my
application and get an IE context menu, as it's not intended to behave like
a website, only to take advantage of the HTML processing capabilities of
IE6.

Kind Regards,
Alex Clark
"C-Services Holland b.v." <cs*@REMOVEcsh4u.nl> wrote in message
news:Mr********************@zeelandnet.nl...
Alex Clark wrote:
Hi All,

I'm sure this must have been asked a million times due to it's
usefulness, but are there any sensible solutions for disabling right
click (and other services such as Ctrl+P, Browser hotkeys etc) on a
Web-Browser control in VB.NET?

Despite Googling for ideas on and off for a week now, I only found one
solution and this was to implement IMessageFilter on the form and then
trap all the incoming messages. Unfortunately, this also prevents the
user from being able to move my application (something to do with
trapping right clicks), along with other useful things like being able to
use the backspace key!

Is there any other way of doing this? Or is there some way I can trap
messages that go into the Web-browser control BEFORE it processes them
and AFTER my Form has? Surely somebody must have come up with an
efficient way of hosting a customized browser in a .NET WinForm!

TIA
Alex Clark


What's the point of disabling the rightmousebutton. I hate websites that
do that since I navigate by using the rightmousebutton (rightclick and
choose back in mozilla). What is it that you want to prevent? Sourcecode
stealing? Image stealing? If so, just disabling the rightmousebutton isn't
the way to go since that is just a wax nose. The user can still do all
those things from the menu (i.e. save page to disk).

--
Rinze van Huizen
C-Services Holland b.v.

Nov 21 '05 #11

P: n/a
Many thanks Charles, I'm trying this out now but it looks as though it's
exactly what I was looking for. I'm surprised that it's been so hard to
come by on the web, and that MS make it so tricky to customize the browser
control. I guess things will be better with the new browser control in
VS2K5, just a shame that it's probably a year away!

Kind Regards,
Alex Clark


"Charles Law" <bl***@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi Alex

The interface you need to implement is IDocHostUIHandler. It has a method
ShowContextMenu. If you return an HRESULT of S_OK in your implementation
of this method this will cancel the context menu on right-click. It also
allows you to implement your own context menu.

In your form that host the WebBrowser control, implement
IDocHostUIHandler. Then, in the DocumentComplete event, get the ICustomDoc
interface from the document object, and call SetUIHandler(Me). Finally,
wait for the readystate to go to complete.

You could have a look at this complete thread:

http://groups.google.com/groups?q=id...phx.gbl&rnum=7

HTH

Charles
"Alex Clark" <al**@theclarkhome.SPAMTIN.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi All,

I'm sure this must have been asked a million times due to it's
usefulness, but are there any sensible solutions for disabling right
click (and other services such as Ctrl+P, Browser hotkeys etc) on a
Web-Browser control in VB.NET?

Despite Googling for ideas on and off for a week now, I only found one
solution and this was to implement IMessageFilter on the form and then
trap all the incoming messages. Unfortunately, this also prevents the
user from being able to move my application (something to do with
trapping right clicks), along with other useful things like being able to
use the backspace key!

Is there any other way of doing this? Or is there some way I can trap
messages that go into the Web-browser control BEFORE it processes them
and AFTER my Form has? Surely somebody must have come up with an
efficient way of hosting a customized browser in a .NET WinForm!

TIA
Alex Clark


Nov 21 '05 #12

P: n/a
Alex Clark wrote:
Hi,

This is not for a website, but rather a Windows Forms application which uses
an HTML based GUI. Whereas I agree that disabling right-click on a website
is bad UI practice (and not really a preventative measure), it is
innappropriate in this case for me to allow a user to right click on my
application and get an IE context menu, as it's not intended to behave like
a website, only to take advantage of the HTML processing capabilities of
IE6.

Kind Regards,
Alex Clark


Ah ok, that makes sense :) I don't know if the IE component has a
mouseclick event, but if it does, maybe you can catch it and compare the
button to the rightbutton and then set e.handled is true or something to
that effect. I don't know if the component takes precedence over the
code though, I 've never used the IE component in my apps.
--
Rinze van Huizen
C-Services Holland b.v.
Nov 21 '05 #13

P: n/a
Hi Charles,

The code seems to be missing a few bits and bobs, namely the implementation
of the IOleCommandTarget interface which is required for one of the methods
in the IDocHostUIHandler. Also, ShowContextMenu does not appear to be a
method within IDocHostUIHandler anywhere?

Thanks,
Alex Clark

"Charles Law" <bl***@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi Alex

The interface you need to implement is IDocHostUIHandler. It has a method
ShowContextMenu. If you return an HRESULT of S_OK in your implementation
of this method this will cancel the context menu on right-click. It also
allows you to implement your own context menu.

In your form that host the WebBrowser control, implement
IDocHostUIHandler. Then, in the DocumentComplete event, get the ICustomDoc
interface from the document object, and call SetUIHandler(Me). Finally,
wait for the readystate to go to complete.

You could have a look at this complete thread:

http://groups.google.com/groups?q=id...phx.gbl&rnum=7

HTH

Charles
"Alex Clark" <al**@theclarkhome.SPAMTIN.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi All,

I'm sure this must have been asked a million times due to it's
usefulness, but are there any sensible solutions for disabling right
click (and other services such as Ctrl+P, Browser hotkeys etc) on a
Web-Browser control in VB.NET?

Despite Googling for ideas on and off for a week now, I only found one
solution and this was to implement IMessageFilter on the form and then
trap all the incoming messages. Unfortunately, this also prevents the
user from being able to move my application (something to do with
trapping right clicks), along with other useful things like being able to
use the backspace key!

Is there any other way of doing this? Or is there some way I can trap
messages that go into the Web-browser control BEFORE it processes them
and AFTER my Form has? Surely somebody must have come up with an
efficient way of hosting a customized browser in a .NET WinForm!

TIA
Alex Clark


Nov 21 '05 #14

P: n/a
Hi Alex

Here are my implementations. ShowContextMenu is the first method in
IDocHostUIHandler.

<code>
' IDocHostUIHandler interface
<ComVisible(True), ComImport(),
Guid("bd3f23c0-d43e-11cf-893b-00aa00bdce1a"), _
InterfaceTypeAttribute(ComInterfaceType.InterfaceI sIUnknown)> _
Public Interface IDocHostUIHandler

<PreserveSig()> Function ShowContextMenu(ByVal dwID As Integer, ByRef
ppt As tagPOINT, _
<MarshalAs(UnmanagedType.IUnknown)> ByVal pcmdtReserved As Object, _
<MarshalAs(UnmanagedType.IDispatch)> ByVal pdispReserved As Object) As
HRESULT

<PreserveSig()> Function GetHostInfo(ByRef pInfo As DOCHOSTUIINFO) As
HRESULT

<PreserveSig()> Function ShowUI(ByVal dwID As Integer, ByVal
pActiveObject As IOleInPlaceActiveObject, _
ByVal pCommandTarget As IOleCommandTarget, ByVal pFrame As
IOleInPlaceFrame, ByVal pDoc As IOleInPlaceUIWindow) As HRESULT

<PreserveSig()> Function HideUI() As HRESULT
<PreserveSig()> Function UpdateUI() As HRESULT
<PreserveSig()> Function EnableModeless(ByVal fEnable As Boolean) As
HRESULT
<PreserveSig()> Function OnDocWindowActivate(ByVal fActivate As Boolean)
As HRESULT
<PreserveSig()> Function OnFrameWindowActivate(ByVal fActivate As
Boolean) As HRESULT
<PreserveSig()> Function ResizeBorder(ByRef prcBorder As tagRECT, ByVal
pUIWindow As IOleInPlaceUIWindow, ByVal fFrameWindow As Boolean) As HRESULT
<PreserveSig()> Function TranslateAccelerator(ByRef lpMsg As MSG, ByRef
pguidCmdGroup As Guid, ByVal nCmdID As Integer) As HRESULT
<PreserveSig()> Function GetOptionKeyPath(ByRef pbstrKey As IntPtr,
ByVal dw As Integer) As HRESULT
<PreserveSig()> Function GetDropTarget(ByVal pDropTarget As IDropTarget,
ByRef ppDropTarget As IDropTarget) As HRESULT
<PreserveSig()> Function
GetExternal(<MarshalAs(UnmanagedType.IDispatch)> ByRef ppDispatch As Object)
As HRESULT
<PreserveSig()> Function TranslateUrl(ByVal dwTranslate As Integer,
ByVal strURLIn As String, ByRef pstrURLOut As String) As HRESULT
<PreserveSig()> Function FilterDataObject(ByVal pDO As IDataObject,
ByRef ppDORet As IDataObject) As HRESULT

End Interface

Public Enum OLECMDTEXTF
OLECMDTEXTF_NONE = 0
OLECMDTEXTF_NAME = 1
OLECMDTEXTF_STATUS = 2
End Enum

' OLECMD
<StructLayout(LayoutKind.Sequential)> _
Public Structure OLECMD
Public cmdID As Int32
Public cmdf As Int32
End Structure

' OLECMDTEXT
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
Public Class OLECMDTEXT
Public cmdtextf As OLECMDTEXTF
Public cwActual As Int32
Private cwBuf As Int32 = 256 'Make sure this is the same as SizeConst
below
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=256)> _
Public text As String
End Class

<StructLayout(LayoutKind.Explicit)> _
Public Class OLEVARIANT
<FieldOffset(0)> Public vt As System.Int16
<FieldOffset(2)> Public wReserved1 As System.Int16
<FieldOffset(4)> Public wReserved2 As System.Int16
<FieldOffset(6)> Public wReserved3 As System.Int16
<FieldOffset(8)> Public lVal As Integer
<FieldOffset(8)> Public iVal As Short
<FieldOffset(8)> Public bstrVal As IntPtr
<FieldOffset(8)> Public pUnkVal As IntPtr
<FieldOffset(8)> Public pArray As IntPtr
<FieldOffset(8)> Public pvRecord As IntPtr
<FieldOffset(12)> Public pRecInfo As IntPtr

Public Sub Clear()
VariantClear(Me)
End Sub 'Clear

Public Sub LoadString(ByVal Value As String)

' Identify this as a BSTR
vt = CType(vt Or VarEnum.VT_BSTR, Short)

bstrVal = Marshal.StringToBSTR(Value)

End Sub

Public Function ToNativeObject() As Object
Dim p As IntPtr
Try
'Allocate a buffer to hold the data in this OLEVARIANT
p = Marshal.AllocCoTaskMem(Marshal.SizeOf(Me.GetType() ))
Marshal.StructureToPtr(Me, p, False)
Return Marshal.GetObjectForNativeVariant(p)
Finally
'Called no matter what
Marshal.FreeCoTaskMem(p)
End Try
End Function

<DllImport("Oleaut32.dll", PreserveSig:=False)> _
Private Shared Sub VariantClear(ByVal var As OLEVARIANT)
End Sub

End Class 'OLEVARIANT

' IOleCommandTarget interface
<ComVisible(True), ComImport(),
Guid("b722bccb-4e68-101b-a2bc-00aa00404770"), _
InterfaceType(ComInterfaceType.InterfaceIsIUnknown )> _
Public Interface IOleCommandTarget

Sub QueryStatus(ByRef pguidCmdGroup As Guid, ByVal cCmds As Int32, _
<InAttribute(), Out(), MarshalAs(UnmanagedType.LPArray,
SizeParamIndex:=1)> ByVal prgCmds() As OLECMD, <InAttribute(), Out()> ByVal
pCmdText As OLECMDTEXT)

Sub Exec(ByRef pguidCmdGroup As Guid, ByVal nCmdId As Int32, _
ByVal nCmdExecOpt As Int32, ByVal pvaIn As OLEVARIANT, ByVal pvaOut
As OLEVARIANT)

End Interface
</code>

Let me know if I have missed anything else.

HTH

Charles
"Alex Clark" <al**@theclarkhome.SPAMTIN.net> wrote in message
news:ei**************@TK2MSFTNGP15.phx.gbl...
Hi Charles,

The code seems to be missing a few bits and bobs, namely the
implementation of the IOleCommandTarget interface which is required for
one of the methods in the IDocHostUIHandler. Also, ShowContextMenu does
not appear to be a method within IDocHostUIHandler anywhere?

Thanks,
Alex Clark

"Charles Law" <bl***@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi Alex

The interface you need to implement is IDocHostUIHandler. It has a method
ShowContextMenu. If you return an HRESULT of S_OK in your implementation
of this method this will cancel the context menu on right-click. It also
allows you to implement your own context menu.

In your form that host the WebBrowser control, implement
IDocHostUIHandler. Then, in the DocumentComplete event, get the
ICustomDoc interface from the document object, and call SetUIHandler(Me).
Finally, wait for the readystate to go to complete.

You could have a look at this complete thread:

http://groups.google.com/groups?q=id...phx.gbl&rnum=7

HTH

Charles
"Alex Clark" <al**@theclarkhome.SPAMTIN.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi All,

I'm sure this must have been asked a million times due to it's
usefulness, but are there any sensible solutions for disabling right
click (and other services such as Ctrl+P, Browser hotkeys etc) on a
Web-Browser control in VB.NET?

Despite Googling for ideas on and off for a week now, I only found one
solution and this was to implement IMessageFilter on the form and then
trap all the incoming messages. Unfortunately, this also prevents the
user from being able to move my application (something to do with
trapping right clicks), along with other useful things like being able
to use the backspace key!

Is there any other way of doing this? Or is there some way I can trap
messages that go into the Web-browser control BEFORE it processes them
and AFTER my Form has? Surely somebody must have come up with an
efficient way of hosting a customized browser in a .NET WinForm!

TIA
Alex Clark



Nov 21 '05 #15

P: n/a
Hi Charles,

Can I assume all the HRESULT function returns should be replaced with an
Integer return?
eg <PreserveSig()> Function GetHostInfo(ByRef pInfo As DOCHOSTUIINFO) As
HRESULT

.... should be As Integer?

Also, there are many references to tagPOINT and tagRECT in the code, can I
safely assume these are identical to the POINT and RECT types declared in
the Win32 API, or is there something special about their layout?

Many thanks,
Alex Clark

"Charles Law" <bl***@nowhere.com> wrote in message
news:OP**************@TK2MSFTNGP14.phx.gbl...
Hi Alex

Here are my implementations. ShowContextMenu is the first method in
IDocHostUIHandler.

<code>
' IDocHostUIHandler interface
<ComVisible(True), ComImport(),
Guid("bd3f23c0-d43e-11cf-893b-00aa00bdce1a"), _
InterfaceTypeAttribute(ComInterfaceType.InterfaceI sIUnknown)> _
Public Interface IDocHostUIHandler

<PreserveSig()> Function ShowContextMenu(ByVal dwID As Integer, ByRef
ppt As tagPOINT, _
<MarshalAs(UnmanagedType.IUnknown)> ByVal pcmdtReserved As Object, _
<MarshalAs(UnmanagedType.IDispatch)> ByVal pdispReserved As Object) As
HRESULT

<PreserveSig()> Function GetHostInfo(ByRef pInfo As DOCHOSTUIINFO) As
HRESULT

<PreserveSig()> Function ShowUI(ByVal dwID As Integer, ByVal
pActiveObject As IOleInPlaceActiveObject, _
ByVal pCommandTarget As IOleCommandTarget, ByVal pFrame As
IOleInPlaceFrame, ByVal pDoc As IOleInPlaceUIWindow) As HRESULT

<PreserveSig()> Function HideUI() As HRESULT
<PreserveSig()> Function UpdateUI() As HRESULT
<PreserveSig()> Function EnableModeless(ByVal fEnable As Boolean) As
HRESULT
<PreserveSig()> Function OnDocWindowActivate(ByVal fActivate As
Boolean) As HRESULT
<PreserveSig()> Function OnFrameWindowActivate(ByVal fActivate As
Boolean) As HRESULT
<PreserveSig()> Function ResizeBorder(ByRef prcBorder As tagRECT, ByVal
pUIWindow As IOleInPlaceUIWindow, ByVal fFrameWindow As Boolean) As
HRESULT
<PreserveSig()> Function TranslateAccelerator(ByRef lpMsg As MSG, ByRef
pguidCmdGroup As Guid, ByVal nCmdID As Integer) As HRESULT
<PreserveSig()> Function GetOptionKeyPath(ByRef pbstrKey As IntPtr,
ByVal dw As Integer) As HRESULT
<PreserveSig()> Function GetDropTarget(ByVal pDropTarget As
IDropTarget, ByRef ppDropTarget As IDropTarget) As HRESULT
<PreserveSig()> Function
GetExternal(<MarshalAs(UnmanagedType.IDispatch)> ByRef ppDispatch As
Object) As HRESULT
<PreserveSig()> Function TranslateUrl(ByVal dwTranslate As Integer,
ByVal strURLIn As String, ByRef pstrURLOut As String) As HRESULT
<PreserveSig()> Function FilterDataObject(ByVal pDO As IDataObject,
ByRef ppDORet As IDataObject) As HRESULT

End Interface

Public Enum OLECMDTEXTF
OLECMDTEXTF_NONE = 0
OLECMDTEXTF_NAME = 1
OLECMDTEXTF_STATUS = 2
End Enum

' OLECMD
<StructLayout(LayoutKind.Sequential)> _
Public Structure OLECMD
Public cmdID As Int32
Public cmdf As Int32
End Structure

' OLECMDTEXT
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
Public Class OLECMDTEXT
Public cmdtextf As OLECMDTEXTF
Public cwActual As Int32
Private cwBuf As Int32 = 256 'Make sure this is the same as
SizeConst below
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=256)> _
Public text As String
End Class

<StructLayout(LayoutKind.Explicit)> _
Public Class OLEVARIANT
<FieldOffset(0)> Public vt As System.Int16
<FieldOffset(2)> Public wReserved1 As System.Int16
<FieldOffset(4)> Public wReserved2 As System.Int16
<FieldOffset(6)> Public wReserved3 As System.Int16
<FieldOffset(8)> Public lVal As Integer
<FieldOffset(8)> Public iVal As Short
<FieldOffset(8)> Public bstrVal As IntPtr
<FieldOffset(8)> Public pUnkVal As IntPtr
<FieldOffset(8)> Public pArray As IntPtr
<FieldOffset(8)> Public pvRecord As IntPtr
<FieldOffset(12)> Public pRecInfo As IntPtr

Public Sub Clear()
VariantClear(Me)
End Sub 'Clear

Public Sub LoadString(ByVal Value As String)

' Identify this as a BSTR
vt = CType(vt Or VarEnum.VT_BSTR, Short)

bstrVal = Marshal.StringToBSTR(Value)

End Sub

Public Function ToNativeObject() As Object
Dim p As IntPtr
Try
'Allocate a buffer to hold the data in this OLEVARIANT
p = Marshal.AllocCoTaskMem(Marshal.SizeOf(Me.GetType() ))
Marshal.StructureToPtr(Me, p, False)
Return Marshal.GetObjectForNativeVariant(p)
Finally
'Called no matter what
Marshal.FreeCoTaskMem(p)
End Try
End Function

<DllImport("Oleaut32.dll", PreserveSig:=False)> _
Private Shared Sub VariantClear(ByVal var As OLEVARIANT)
End Sub

End Class 'OLEVARIANT

' IOleCommandTarget interface
<ComVisible(True), ComImport(),
Guid("b722bccb-4e68-101b-a2bc-00aa00404770"), _
InterfaceType(ComInterfaceType.InterfaceIsIUnknown )> _
Public Interface IOleCommandTarget

Sub QueryStatus(ByRef pguidCmdGroup As Guid, ByVal cCmds As Int32, _
<InAttribute(), Out(), MarshalAs(UnmanagedType.LPArray,
SizeParamIndex:=1)> ByVal prgCmds() As OLECMD, <InAttribute(), Out()>
ByVal pCmdText As OLECMDTEXT)

Sub Exec(ByRef pguidCmdGroup As Guid, ByVal nCmdId As Int32, _
ByVal nCmdExecOpt As Int32, ByVal pvaIn As OLEVARIANT, ByVal pvaOut
As OLEVARIANT)

End Interface
</code>

Let me know if I have missed anything else.

HTH

Charles
"Alex Clark" <al**@theclarkhome.SPAMTIN.net> wrote in message
news:ei**************@TK2MSFTNGP15.phx.gbl...
Hi Charles,

The code seems to be missing a few bits and bobs, namely the
implementation of the IOleCommandTarget interface which is required for
one of the methods in the IDocHostUIHandler. Also, ShowContextMenu does
not appear to be a method within IDocHostUIHandler anywhere?

Thanks,
Alex Clark

"Charles Law" <bl***@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi Alex

The interface you need to implement is IDocHostUIHandler. It has a
method ShowContextMenu. If you return an HRESULT of S_OK in your
implementation of this method this will cancel the context menu on
right-click. It also allows you to implement your own context menu.

In your form that host the WebBrowser control, implement
IDocHostUIHandler. Then, in the DocumentComplete event, get the
ICustomDoc interface from the document object, and call
SetUIHandler(Me). Finally, wait for the readystate to go to complete.

You could have a look at this complete thread:

http://groups.google.com/groups?q=id...phx.gbl&rnum=7

HTH

Charles
"Alex Clark" <al**@theclarkhome.SPAMTIN.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi All,

I'm sure this must have been asked a million times due to it's
usefulness, but are there any sensible solutions for disabling right
click (and other services such as Ctrl+P, Browser hotkeys etc) on a
Web-Browser control in VB.NET?

Despite Googling for ideas on and off for a week now, I only found one
solution and this was to implement IMessageFilter on the form and then
trap all the incoming messages. Unfortunately, this also prevents the
user from being able to move my application (something to do with
trapping right clicks), along with other useful things like being able
to use the backspace key!

Is there any other way of doing this? Or is there some way I can trap
messages that go into the Web-browser control BEFORE it processes them
and AFTER my Form has? Surely somebody must have come up with an
efficient way of hosting a customized browser in a .NET WinForm!

TIA
Alex Clark



Nov 21 '05 #16

P: n/a
Wooohoo! I think that's got it, thanks for your help Charles :-D

Kind Regards,
Alex Clark
Nov 21 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.