【问题标题】:Why does arrow key navigation not work in TWebBrowser?为什么箭头键导航在 TWebBrowser 中不起作用?
【发布时间】:2016-11-17 17:24:02
【问题描述】:

这是一个在 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

当我运行程序时,我希望能够使用箭头键向上/向下/向左/向右滚动页面。但是,这些键不起作用。我可以使用向上翻页和向下翻页,但不能使用向上/向下/向左/向右。

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

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

【问题讨论】:

  • 我无法在 D7 上重现。我收到一些 javascript 错误(这是由于兼容模式导致的),但箭头键有效。在使用按键之前你会点击 TWebBrowser 吗?
  • 我必须在任何箭头/home/end/pgup/pgdn 键起作用之前单击页面。与@kobik 相同。 Delphi-10.1 更新 2.
  • 我正在使用 XE7。单击控件不会改变行为。我的 WinForms 程序是 .net。

标签: delphi delphi-xe7 twebbrowser


【解决方案1】:

我相信表单将这些键解释为对话框导航键。于是我将控件对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;

这似乎可以解决问题。

【讨论】:

    猜你喜欢
    • 2021-03-22
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-11
    相关资源
    最近更新 更多