Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming

How to Disable the Context Menu in a TWebBrowser

By Zarko Gajic, About.com

Here's the code to disable the context menu (the one a user gets when it right-clicks inside a TWebBrowser) for TWebBrowser in a Delphi application:

function MouseProc(nCode: Integer; wParam, lParam: Longint): LongInt; stdcall;
var
  classbuf: array[0..255] of Char;
const
  ie = 'Internet Explorer_Server';
begin
  case nCode < 0 of
    True:
      Result := CallNextHookEx(MouseHook, nCode, wParam, lParam) ;
    False:
      case wParam of
      WM_RBUTTONDOWN, WM_RBUTTONUP:
      begin
        GetClassName(PMOUSEHOOKSTRUCT(lParam)^.HWND, classbuf, SizeOf(classbuf)) ;
        if lstrcmp(@classbuf[0], @ie[1]) = 0 then
          Result := HC_SKIP
        else
          Result := CallNextHookEx(MouseHook, nCode, wParam, lParam) ;
      end
      else
      begin
        Result := CallNextHookEx(MouseHook, nCode, wParam, lParam) ;
      end;
    end; //case wParam
  end; //case nCode
end; (*MouseProc*)

//Form OnCreate
procedure TWebBrowserForm.FormCreate(Sender: TObject) ;
begin
  MouseHook := SetWindowsHookEx(WH_MOUSE, MouseProc, 0, GetCurrentThreadId()) ;
end;

//Form OnDestroy
procedure TWebBrowserForm.FormDestroy(Sender: TObject) ;
begin
  if MouseHook <> 0 then UnHookWindowsHookEx(MouseHook) ;
end;
If you want to find more about Windows hooks, read the :An introduction to hook procedures

Delphi tips navigator:
» How to open a web site or a .htm file with the default web broswer in a NEW window
« How to enable file download from an Asp.net page

Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 2004 Delphi Tips
  7. How to Disable the Context Menu in a TWebBrowser Instance

©2009 About.com, a part of The New York Times Company.

All rights reserved.