首页  编辑  

TWebBrowser控件无法响应回车键

Tags: /超级猛料/OS.操作系统/IE.扩展/TWebBrowser/输入/   Date Created:

TWebBrowser控件无法响应回车键怎么办?希望得到一个很妥当的方法

WnEunfn(斯人不语)

好不容易才解决这个问题,由于代码比较长,你要慢慢看吧……

首先:

 uses ActiveX;//引用!!

----------------------------------------------------------------  

 TfrmChildWeb = class(TForm)

   WebBrowser: TWebBrowser;//TwebBrowser!!!

   procedure FormCreate(Sender: TObject);//必用的一个事件…

 private

   FOleInPlaceActiveObject: IOleInPlaceActiveObject;

   procedure MsgHandler(var Msg: TMsg; var Handled: Boolean);//自定义的一个处理……

 public

 end;

----------------------------------------------------------------

过程:

procedure TfrmChildWeb.MsgHandler(var Msg: TMsg; var Handled: Boolean);

var

 iOIPAO: IOleInPlaceActiveObject;

 Dispatch: IDispatch;

begin

 if WebBrowser = nil then begin

   Handled := False;

   Exit;

 end;

 Handled := (IsDialogMessage(WebBrowser.Handle, Msg) = True);

 if (Handled) and (not WebBrowser.Busy) then

 begin

   if FOleInPlaceActiveObject = nil then

   begin

     Dispatch := WebBrowser.Application;

     if Dispatch <> nil then

     begin

       Dispatch.QueryInterface(IOleInPlaceActiveObject, iOIPAO);

       if iOIPAO <> nil then

         FOleInPlaceActiveObject := iOIPAO;

     end;

   end;

   if FOleInPlaceActiveObject <> nil then

     if ((Msg.message = WM_KEYDOWN) or (Msg.message = WM_KEYUP)) and

       ((Msg.wParam = VK_BACK) or (Msg.wParam = VK_LEFT) or (Msg.wParam = VK_RIGHT)) then

     else

       FOleInPlaceActiveObject.TranslateAccelerator(Msg);

 end;

end;

procedure TfrmChildWeb.FormCreate(Sender: TObject);

begin

 Application.OnMessage := MsgHandler;

end;

---------------------------------------

Problem:

 Web forms that have multiline text boxes and/or Submit buttons do not

 respond to the Enter key when displayed on a TWebbrowser.

 Also when browsing local folders, some keys don't respond.

 How to solve it:

unit Unit1;

interface

uses

 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

 Dialogs, StdCtrls, OleCtrls, SHDocVw, ActiveX;

type

 TForm1 = class(TForm)

   WebBrowser1: TWebBrowser;

   Button1: TButton;

   procedure FormCreate(Sender: TObject);

   procedure FormDestroy(Sender: TObject);

   procedure Button1Click(Sender: TObject);

 private

   { Private declarations }

   FOleInPlaceActiveObject: IOleInPlaceActiveObject;

   procedure MsgHandler(var Msg: TMsg; var Handled: Boolean);

 public

   { Public declarations }

 end;

var

 Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormDestroy(Sender: TObject);

begin

 FOleInPlaceActiveObject := nil;

end;

procedure TForm1.FormCreate(Sender: TObject);

begin

 Application.OnMessage := MsgHandler;

end;

procedure TForm1.MsgHandler(var Msg: TMsg; var Handled: Boolean);

const

 ExtKeys = [VK_DELETE, VK_BACK, VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN,

   $30..$39, $41..42, $44..$55, $57, $59..$5A]; { extended keys }

 fExtended = $01000000; { extended key flag }

var

 IOIPAO: IOleInPlaceActiveObject;

 Dispatch: IDispatch;

begin

 if WebBrowser1 = nil then

 begin

   Handled := False;

   Exit;

 end;

 Handled := (IsDialogMessage(WebBrowser1.Handle, Msg) = True);

 if (Handled) and (not WebBrowser1.Busy) then

 begin

   if FOleInPlaceActiveObject = nil then

   begin

     Dispatch := WebBrowser1.Application;

     if Dispatch <> nil then

     begin

       Dispatch.QueryInterface(IOleInPlaceActiveObject, IOIPAO);

       if IOIPAO <> nil then FOleInPlaceActiveObject := IOIPAO;

     end;

   end;

   if FOleInPlaceActiveObject <> nil then

     if ((Msg.message = WM_KEYDOWN) or (Msg.message = WM_KEYUP)) and

       ((Msg.wParam in ExtKeys) and ((Msg.lParam and fExtended) = fExtended)) then

       //nothing  -  do not pass on Backspace,  Left or Right arrows

     else

       FOleInPlaceActiveObject.TranslateAccelerator(Msg);

 end;

end;

procedure TForm1.Button1Click(Sender: TObject);

begin

 Webbrowser1.Navigate('www.SwissDelphiCenter.ch');

end;

initialization

 OleInitialize(nil);

finalization

 OleUninitialize

end.