2

I am using TWebBrowser in my Delphi firemonkey application and would like to disable right click on the page. Is there any way for this.

Padam
  • 117
  • 1
  • 7

2 Answers2

1

The default TWebBrowser for Firemonkey doesn't do this natively as of 10.3 Rio. Outside of a different browser component, your best bet is to use Javascript. If you are controlling the content that is served, that's pretty easy. See How do I disable right click on my web page?

If you are dealing with another website whose content you don't have control over, you can try to inject the Javascript using TWebBrowser.EvaluateJavaScript()

procedure TForm1.DisableRC;
var
  strJS: string;
begin
  strJS := 'document.addEventListener("contextmenu", function(e){ e.preventDefault();}, false);';
  webbrowser1.EvaluateJavaScript(strJS);
end;

The code works if you call DisableRC; from say, a button click. But if the URL reloads or content changes, you will need to call it again.

I tried placing a call to DisableRC() in the TWebBrowser.OnDidFinishLoad event, to execute it after page navigation completes, but the event ended up firing thousands of times in an infinite loop. Using TThread.Queue made no difference. Possibly this is because the evaluation of Javascript makes the event fire again.

What ended up working was placing a TTimer on the form, disabled by default, with the following code in OnTimer:

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  DisableRC;
  Timer1.Enabled := false;
end;

And then enabling the Timer in the TWebBrowser.OnDidFinishLoad event.

It's somewhat of a hack, but hopefully it is helpful to get you started in your implementation.

A. Niese
  • 399
  • 3
  • 13
0

I believe you may be able to modify this code and trap the message WM_RBUTTONDOWN. Replace the WM_LBUTTONDOWN in the referenced code with WM_RBUTTONDOWN. I'd try it but i only have C++ Builder installed.

relayman357
  • 793
  • 1
  • 6
  • 30