Jump to content

How to disable right click in the WebBrowser Object


 Share

Recommended Posts

How to disable right click in the WebBrowser Object ?

Somtimes I don't want somebody to open the contextmenu in the WebBrowser...

There's some code in Visual Basic:

Rem

Dim WithEvents M_Dom As MSHTML.HTMLDocument

Private Function M_Dom_oncontextmenu() As Boolean

M_Dom_oncontextmenu = False

End Function

Private Sub Webbrowser1_DownloadComplete()

Set M_Dom = Webbrowser1.Document

End Sub

Rem

But I don't know whether it will work, or how to change it to au3 script...

I need some help...

Link to comment
Share on other sites

How to disable right click in the WebBrowser Object ?

I think there is a way to disable the context menu of a web page using Javascript, but you would have to know the author of the web page or be the author.

Das Häschen benutzt Radar

Link to comment
Share on other sites

I think there is a way to disable the context menu of a web page using Javascript, but you would have to know the author of the web page or be the author.

I have a way that to inject the java script into the HTML from _IEBodyReadHTML() by _IEDocInsertHTML.

But what I want now is the way to disable all the context menu in my WebBrowser...

Link to comment
Share on other sites

Thank you for all of you!

But I have to say that I had already know the way of Java script injecting.

The weakness of injecting is that you must inject every page in your WebBrowser, include some link you sometimes really can't anticipate it...

Is there any efficient way to disable right click?

Perhaps call the "WBCustomizer.dll", or use the "IDocHostUIHandler"? But how to use these difficult method?

Link to comment
Share on other sites

It's not so hard... make it event-driven:

; Set up an event sink so that we can trigger on page loads
$oEvent = ObjEvent($oIE, "Evt_")
    
Func Evt_onload()
    ; This function will be fired whenever a new page loads
    _IEHeadInsertEventScript ($oIE, "document", "oncontextmenu", "alert('You cannot rightclick');return false")
EndFunc   ;==>Evt_onload

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

I've got an idea, but at the sametime I found a problem which possibly the BUG of Function _IsPressed

Here is my code:

#include <GUIConstants.au3>
#include <IE.au3>
#Include <Misc.au3>

_IEErrorHandlerRegister()

$IEobject = _IECreateEmbedded()
GUICreate("Disable Right Click In WebBrowser - By D4llower", 640, 400, (@DesktopWidth - 640) / 2, (@DesktopHeight - 580) / 2, $WS_OVERLAPPEDWINDOW + $WS_VISIBLE + $WS_CLIPSIBLINGS)
$GUIActiveX = GUICtrlCreateObj($IEobject, 20, 20, 600, 360)

GUISetState()

_IENavigate($IEobject, "http://www.google.com")

_IEHeadInsertEventScript($IEobject, "document", "oncontextmenu", "window.event.cancelBubble = true;window.event.returnValue = false;")

While 1
    If _IsPressed('01') = 1 Or _  ;Detect the Left mouse button
            _IsPressed('02') = 1 Or _  ;Detect the Right mouse button
            _IsPressed('04') = 1 Or _  ;Detect the Middle mouse button (three-button mouse)
            _IsPressed('05') = 1 Or _  ;Detect the Windows 2000/XP: X1 mouse button
            _IsPressed('06') = 1 Or _  ;Detect the Windows 2000/XP: X2 mouse button
            _IsPressed('A4') = 1 Or _  ;Detect the Left MENU key, but Not work, Bug ???
            _IsPressed('A5') = 1 Then  ;Detect the Right MENU key, but Not work, Bug ???
        _IEHeadInsertEventScript($IEobject, "document", "oncontextmenu", "window.event.cancelBubble = true;window.event.returnValue = false;")  ;Inject Java Script...
    EndIf
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
    EndSelect
WEnd

Exit

I feel it not bad. But why _IsPressed can't detect the MENU key in keyboard, I mean when I click into some new page, or there is something wrong with my code ?

Edited by D4llower
Link to comment
Share on other sites

It's not so hard... make it event-driven:

; Set up an event sink so that we can trigger on page loads
$oEvent = ObjEvent($oIE, "Evt_")
    
Func Evt_onload()
    ; This function will be fired whenever a new page loads
    _IEHeadInsertEventScript ($oIE, "document", "oncontextmenu", "alert('You cannot rightclick');return false")
EndFunc   ;==>Evt_onload

Dale

Thanks, but actually it doesn't work...???

Can you try it and tell me the result?

Edited by D4llower
Link to comment
Share on other sites

No, you try it and show what you did and try to figure out why it didn't work.

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

  • 2 years later...

How to disable right click in the WebBrowser Object ?

Here's the best way that I found and it actually works very well:

If $msg = $GUI_EVENT_SECONDARYDOWN or  $msg = $GUI_EVENT_PRIMARYDOWN or $GUI_EVENT_PRIMARYUP Then
    _IEHeadInsertEventScript ($ieo, "document", "oncontextmenu", "return false")
Endif

This detects mouse clicks just as the page loads and disables the context menu on any new loaded page.

Let me know if it works at your end and sorry for replying on a subject that is quite old. It took me several hours to finally figure out how to get this completely working. There was a similar answer but it didn't always work because only $GUI_EVENT_PRIMARYDOWN was used. All 3 are required.

Cheers,

Ian

Edited by intime69

Developer and Co-OwnerInTime Applicaitons Inc.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...