473,326 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Web Browser Disable Right Click

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
16 9399
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Wooohoo! I think that's got it, thanks for your help Charles :-D

Kind Regards,
Alex Clark
Nov 21 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: yaktipper | last post by:
This explains how to disable the view source / right-click menu in Netscape 4, Netscape 6 and Internet Explorer (IE). <script language="JavaScript"> //This code is the beginning of the right...
9
by: Dennis Allen | last post by:
Hi. What's the best way to display an image, disabling the user's ability to right-click on it? I believe you have to use <body oncontextmenu="return false">. Would the following work? var...
5
by: Sven | last post by:
I would like to disable the ability of the user hold control and click and get the dropdown menu. I am ultimately discouraging users from easily getting a copy of the image. I was able to do this...
0
by: Alex Clark | last post by:
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...
4
by: Seen | last post by:
This has probably been covered before but I can't seem to find it. I want to disable the user from using the right mouse button that brings up a windown menu of options. How's it done?????
1
by: Mclaren | last post by:
I have this code that disables all menus and toolbars including the right click functions. Dim i As Integer For i = 1 To CommandBars.Count CommandBars(i).Enabled = False Next i How can i...
14
by: Amar | last post by:
Hi All, I am a newbie to PHP and have the task to create a page using PHP where in that page I need to disable all key operation as well as mouse operation even also the menu operation that...
11
by: lightyagami | last post by:
how to disable right click in vb6? because i put a flash document in my form and i want to be it like a touch screeen....so how?
0
by: karthikselvam | last post by:
how to disable right click on panel in asp.net.please help me soon
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.