Connecting Tech Pros Worldwide Forums | Help | Site Map

Web Browser Disable Right Click

Alex Clark
Guest
 
Posts: n/a
#1: Nov 21 '05
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



Roy Soltoff
Guest
 
Posts: n/a
#2: Nov 21 '05

re: Web Browser Disable Right Click


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" <alex@theclarkhome.SPAMTIN.net> wrote in message
news:%23A7Q01NyEHA.2200@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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[/color]
trap[color=blue]
> all the incoming messages. Unfortunately, this also prevents the user[/color]
from[color=blue]
> being able to move my application (something to do with trapping right
> clicks), along with other useful things like being able to use the[/color]
backspace[color=blue]
> 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[/color]
way[color=blue]
> of hosting a customized browser in a .NET WinForm!
>
> TIA
> Alex Clark
>
>[/color]


Roy Soltoff
Guest
 
Posts: n/a
#3: Nov 21 '05

re: Web Browser Disable Right Click


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" <alex@theclarkhome.SPAMTIN.net> wrote in message
news:%23A7Q01NyEHA.2200@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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[/color]
trap[color=blue]
> all the incoming messages. Unfortunately, this also prevents the user[/color]
from[color=blue]
> being able to move my application (something to do with trapping right
> clicks), along with other useful things like being able to use the[/color]
backspace[color=blue]
> 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[/color]
way[color=blue]
> of hosting a customized browser in a .NET WinForm!
>
> TIA
> Alex Clark
>
>[/color]


Jay Feldman
Guest
 
Posts: n/a
#4: Nov 21 '05

re: Web Browser Disable Right Click


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:
[color=blue]
> 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
>
>
>[/color]
Jay Feldman
Guest
 
Posts: n/a
#5: Nov 21 '05

re: Web Browser Disable Right Click


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:
[color=blue]
> 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
>
>
>[/color]
Jay Feldman
Guest
 
Posts: n/a
#6: Nov 21 '05

re: Web Browser Disable Right Click


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:
[color=blue]
> 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:
>[color=green]
> > 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
> >
> >
> >[/color][/color]
Jay Feldman
Guest
 
Posts: n/a
#7: Nov 21 '05

re: Web Browser Disable Right Click


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:
[color=blue]
> 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:
>[color=green]
> > 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
> >
> >
> >[/color][/color]
Charles Law
Guest
 
Posts: n/a
#8: Nov 21 '05

re: Web Browser Disable Right Click


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" <alex@theclarkhome.SPAMTIN.net> wrote in message
news:%23A7Q01NyEHA.2200@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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
>
>[/color]


Charles Law
Guest
 
Posts: n/a
#9: Nov 21 '05

re: Web Browser Disable Right Click


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" <alex@theclarkhome.SPAMTIN.net> wrote in message
news:%23A7Q01NyEHA.2200@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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
>
>[/color]


C-Services Holland b.v.
Guest
 
Posts: n/a
#10: Nov 21 '05

re: Web Browser Disable Right Click


Alex Clark wrote:[color=blue]
> 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
>
>[/color]

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.
Alex Clark
Guest
 
Posts: n/a
#11: Nov 21 '05

re: Web Browser Disable Right Click


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." <csh@REMOVEcsh4u.nl> wrote in message
news:MrudnYp_QcZS-gXcRVnytQ@zeelandnet.nl...[color=blue]
> Alex Clark wrote:[color=green]
>> 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
>>
>>[/color]
>
> 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.[/color]


Alex Clark
Guest
 
Posts: n/a
#12: Nov 21 '05

re: Web Browser Disable Right Click


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" <blank@nowhere.com> wrote in message
news:%23ziRGKXyEHA.4028@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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" <alex@theclarkhome.SPAMTIN.net> wrote in message
> news:%23A7Q01NyEHA.2200@TK2MSFTNGP09.phx.gbl...[color=green]
>> 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
>>
>>[/color]
>
>[/color]


C-Services Holland b.v.
Guest
 
Posts: n/a
#13: Nov 21 '05

re: Web Browser Disable Right Click


Alex Clark wrote:
[color=blue]
> 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
>
>[/color]

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.
Alex Clark
Guest
 
Posts: n/a
#14: Nov 21 '05

re: Web Browser Disable Right Click


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" <blank@nowhere.com> wrote in message
news:%23ziRGKXyEHA.4028@TK2MSFTNGP09.phx.gbl...[color=blue]
> 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" <alex@theclarkhome.SPAMTIN.net> wrote in message
> news:%23A7Q01NyEHA.2200@TK2MSFTNGP09.phx.gbl...[color=green]
>> 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
>>
>>[/color]
>
>[/color]


Charles Law
Guest
 
Posts: n/a
#15: Nov 21 '05

re: Web Browser Disable Right Click


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" <alex@theclarkhome.SPAMTIN.net> wrote in message
news:eiOSThxyEHA.4028@TK2MSFTNGP15.phx.gbl...[color=blue]
> 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" <blank@nowhere.com> wrote in message
> news:%23ziRGKXyEHA.4028@TK2MSFTNGP09.phx.gbl...[color=green]
>> 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" <alex@theclarkhome.SPAMTIN.net> wrote in message
>> news:%23A7Q01NyEHA.2200@TK2MSFTNGP09.phx.gbl...[color=darkred]
>>> 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
>>>
>>>[/color]
>>
>>[/color]
>
>[/color]


Alex Clark
Guest
 
Posts: n/a
#16: Nov 21 '05

re: Web Browser Disable Right Click


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" <blank@nowhere.com> wrote in message
news:OPTGmSyyEHA.1392@TK2MSFTNGP14.phx.gbl...[color=blue]
> 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" <alex@theclarkhome.SPAMTIN.net> wrote in message
> news:eiOSThxyEHA.4028@TK2MSFTNGP15.phx.gbl...[color=green]
>> 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" <blank@nowhere.com> wrote in message
>> news:%23ziRGKXyEHA.4028@TK2MSFTNGP09.phx.gbl...[color=darkred]
>>> 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" <alex@theclarkhome.SPAMTIN.net> wrote in message
>>> news:%23A7Q01NyEHA.2200@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
>>>>
>>>>
>>>
>>>[/color]
>>
>>[/color]
>
>[/color]


Alex Clark
Guest
 
Posts: n/a
#17: Nov 21 '05

re: Web Browser Disable Right Click


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

Kind Regards,
Alex Clark


Closed Thread