为什么箭头键导航在 TWebBrowser 中不起作用?

Why does arrow key navigation not work in TWebBrowser?

这是一个在 VCL 应用程序中承载 TWebBrowser 控件的简单程序:

Unit1.pas

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, 
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.OleCtrls, SHDocVw;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  Browser: TWebBrowser;
begin
  Browser := TWebBrowser.Create(Self);
  TOleControl(Browser).Parent := Self;
  Browser.Align := alClient;
  Browser.Navigate('http://www.bbc.co.uk/');
end;

end.

Unit1.dfm

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 587
  ClientWidth = 928
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
end

当我 运行 程序时,我希望能够使用箭头键滚动页面,up/down/left/right。但是,这些键没有任何作用。我可以使用向上翻页和向下翻页,但不能 up/down/left/right。

我使用 WebBrowser 控件在 .net WinForms 应用程序中重新创建了等效的应用程序,并且行为是相同的。这似乎表明底层控制存在问题。

我可以做些什么来让这些键起作用吗?或者这只是一个失败的原因?

我认为表单将这些键解释为对话框导航键。所以我更改了控件对 WM_GETDLGCODE 消息的响应,以请求控件处理这些键:

type
  TWebBrowser = class(SHDocVw.TWebBrowser)
  protected
    procedure WMGetDlgCode(var Msg: TWMGetDlgCode); message WM_GETDLGCODE;
  end;

procedure TWebBrowser.WMGetDlgCode(var Msg: TWMGetDlgCode);
begin
  inherited;
  Msg.Result := Msg.Result or DLGC_WANTARROWS;
end;

这似乎可以解决问题。