views:

192

answers:

2

I have a TWebBrowser placed on a form with the designMode enabled.
Bellow the browser I have a close button with the Caption set to 'Clos&e'.
When I am editing the contents of a document inside the WebBrowser and I press the key E the button close is called.
It appears that it is treating TWebBrowser like other controls that don't handle keys and/or don't accept chars (e.g. TButton).

How can I solve this?

Thanks in advance.

+2  A: 

Descend from TWebBrowser, override the CN_CHAR message handler, and return 1. Triggering the shortcut with Alt+E will still work.

type
  TWebBrowser = class(SHDocVw.TWebBrowser)
    procedure CNChar(var Message: TWMChar); message CN_CHAR;
  end;

...

procedure TWebBrowser.CNChar(var Message: TWMChar);
begin
  Message.Result := 1;
end;
Craig Peterson
It Worked, but I needed to change the MessageResult from 1 to 0. Tks
Douglas Lise
A: 

In your unit, declare

var
  WebBrowserHandle: HWND;

and

function EnumChildProc(h: HWND; p: LPARAM): boolean; stdcall;

In the implementation section, write

function EnumChildProc(h: HWND; p: LPARAM): boolean; stdcall;
var
  TestClassName: array[0..31] of AnsiChar;
begin
  GetClassNameA(h, @TestClassName, 32);
  result := not SameStr(TestClassName, 'Internet Explorer_Server');
  if not result then
    WebBrowserHandle := h;
end;

Now add a TApplicationEvents component, and use the OnShortCut event:

procedure TForm1.ApplicationEvents1ShortCut(var Msg: TWMKey;
  var Handled: Boolean);
var
  h: HWND;
begin
  WebBrowserHandle := 0;
  h := FindWindowEx(Handle, 0, 'Shell Embedding', nil);
  EnumChildWindows(h, @EnumChildProc, 0);
  if WebBrowserHandle = GetFocus then
    Handled := true;
end;
Andreas Rejbrand