How to make TWebBrowser ignore accelerator chars of others controls?
-
25-09-2019 - |
Question
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.
Solution
Descend from TWebBrowser, override the CN_CHAR
message handler, and return 0
. 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 := 0;
end;
OTHER TIPS
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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow