disable the TWebBrowser's context menu

18:11
Mark Williams wrote:

> Apologies if this is the correct newsgroup for this question.

Uhm, yes. Apologies accepted. ;-)

> I use a TWebbrowser component to display various files (eg word,
> excel). However, I wish to disable edit capabilities. I've searched
> the newsgroups and googled, but couldn't find any thing which answered
> this problem.

Do you mean to disable the TWebBrowser's context menu? Probably the easiest
would be to install a keyboard/mouse hook. Below is an example for a very
simple dialog-style browser that lacks all advanced features: Basically the
code checks every keyboard/mouse event and if necessary suppresses them or
uses them to eg. close the application.

unit frmMiniBrowser;
 
interface
 
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, OleCtrls, SHDocVw_TLB, ExtCtrls;
 
type
TForm1 = class(TForm)
Browser: TWebBrowser_V1;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
 
var
Form1: TForm1;
MouHookID: THandle;
KbdHookID: THandle;
 
implementation
 
{$R *.DFM}
 
function KeyProc(nCode: Integer; wParam, lParam: Longint): Longint; stdcall;
const
ie_name = 'Internet Explorer_Server';
begin
if (nCode < 0) then
begin
Result := CallNextHookEx(KbdHookID, nCode, wParam, lParam);
end else
begin
case wParam of
27 {esc}:
begin
Result := HC_SKIP;
PostMessage(Form1.Handle, WM_CLOSE, 0, 0);
end;
else
Result := CallNextHookEx(KbdHookID, nCode, wParam, lParam);
end;
end;
end;
 
function MouseProc(nCode: Integer; wParam, lParam: Longint): Longint; stdcall;
var
szClassName: array[0..255] of Char;
const
ie_name = 'Internet Explorer_Server';
begin
if (nCode < 0) then
begin
Result := CallNextHookEx(MouHookID, nCode, wParam, lParam);
end else
begin
case wParam of
WM_RBUTTONDOWN, WM_RBUTTONUP:
begin
GetClassName(PMOUSEHOOKSTRUCT(lParam)^.HWND, szClassName, SizeOf(szClassName));
if lstrcmp(@szClassName[0], @ie_name[1]) = 0 then
Result := HC_SKIP
else
Result := CallNextHookEx(MouHookID, nCode, wParam, lParam);
end;
else
Result := CallNextHookEx(MouHookID, nCode, wParam, lParam);
end;
end;
end;
 
procedure TForm1.FormCreate(Sender: TObject);
var
vFlags, vTgt, vPost, vHdr: OleVariant;
Parm: string;
begin
Screen.Cursor := crHourGlass;
MouHookID := SetWindowsHookEx(WH_MOUSE, MouseProc, 0, GetCurrentThreadId());
KbdHookID := SetWindowsHookEx(WH_Keyboard, KeyProc, 0, GetCurrentThreadId());
if ParamCount = 0 then
Parm := Concat('res://', Application.ExeName, '/RT_HTML/ABOUT')
else
Parm := ParamStr(1);
Browser.Navigate(WideString(Parm), vFlags, vTgt, vPost, vHdr);
Screen.Cursor := crDefault;
end;
 
procedure TForm1.FormDestroy(Sender: TObject);
begin
if MouHookID <> 0 then
UnHookWindowsHookEx(MouHookID);
if KbdHookID <> 0 then
UnHookWindowsHookEx(KbdHookID);
end;
 
end.




--
Ben