您现在的位置:首页 >> VCL >> VCL >> 内容

如何能在WebBrower里输入回车(包括多页面浏览器)

时间:2011/9/3 16:26:02 点击:

  核心提示:如何能在webbrower里输入回车含有多行文本输入框(textarea )或提交(submit)按钮的HTML表单在TWebBrowser中显示时,对回车键不响应。另外,Delphi表单上按钮的快捷...

如何能在webbrower里输入回车
含有多行文本输入框(textarea )或提交(submit)按钮的HTML表单在TWebBrowser中显示时,对回车键不响应。另外,Delphi表单上按钮的快捷字母键也无法在HTML表单上输入,因为一输入就触发相应按钮的单击事件。解决代码如下:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, SHDocVw_TLB, ActiveX, StdCtrls;

type
TForm1 = class(TForm)
WebBrowser1: TWebBrowser;
Button1: TButton;
Button2: TButton;
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(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
DialogKeys: set of Byte = [VK_LEFT, VK_RIGHT, VK_BACK, VK_UP, VK_DOWN,
$30..$39, $41..42, $44..$55, $57, $59..$5A];
var
iOIPAO: IOleInPlaceActiveObject;
Dispatch: IDispatch;
begin
{ exit if we don't get back a webbrowser object }
if (WebBrowser1 = nil) then
begin
Handled := System.False;
Exit;
end;

Handled := (IsDialogMessage(WebBrowser1.Handle, Msg) = System.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 DialogKeys) then
// nothing - do not pass on the DialogKeys
else
FOleInPlaceActiveObject.TranslateAccelerator(Msg);
end;
end;

initialization
OleInitialize(nil);

finalization
OleUninitialize;


本段代码出自SwissDelphiCenter.ch,作者未知。主要要引用ActiveX。Delphi 7中SHDocVw_TLB改为SHDocVw。

=============================================

方法2

在form放置一个ApplicationEvents控件,  
 在OnMessage事件中写下面的代码:  
   
   
 const  
     StdKeys   =   [VK_TAB,   VK_RETURN];   {   standard   keys   }  
     ExtKeys   =   [VK_Delete,   VK_BACK,   VK_LEFT,   VK_RIGHT];   {   extended   keys   }  
     fExtended   =   $01000000;   {   extended   key   flag   }  
 begin  
     Handled   :=   False;  
     with   Msg   do  
         if   ((Message   >=   WM_KEYFIRST)   and   (Message   <=   WM_KEYLAST))   and  
             ((wParam   in   StdKeys)   or   {$IFDEF   VER120}(GetKeyState(VK_CONTROL)   <   0)   or   {$ENDIF}  
             (wParam   in   ExtKeys)   and   ((lParam   and   fExtended)   =   fExtended))   then  
         try  
             if   IsChild(WebPageControl1.ActivePage.Browser.Handle,   hWnd)   then  
             {   handles   all   browser   related   messages   }  
             begin  
                 with   WebPageControl1.ActivePage.Browser.Application   as   IOleInPlaceActiveObject   do  
                     Handled   :=   TranslateAccelerator(Msg)   =   S_OK;  
                 if   not   Handled   then  
                 begin  
                     Handled   :=   True;  
                     TranslateMessage(Msg);  
                     DispatchMessage(Msg);  
                 end;  
             end;  
         except   end;

补充:  
 1、uses   ActiveX  
 2、WebPageControl1.ActivePage.Browser替换成你的WebBrowser

=============================================

如何处理多页面浏览器的回车问题(主要是判断当前使用的WebBrowser)

procedure TMainForm.ApplicationEvents1Message(var Msg: tagMSG;
  var Handled: Boolean);
const
  StdKeys = [VK_TAB, VK_RETURN]; { standard keys }
  ExtKeys = [VK_Delete, VK_BACK, VK_LEFT, VK_RIGHT]; { extended keys }
  fExtended = $01000000; { extended key flag }
var
  Web: TWebBrowser;
begin
  if PageControl1.PageCount = 0 then Exit;
  //Web := GetCurrentWeb;
    try
    if PageControl1.ActivePageIndex > 0 then
      Web:=Self.WebBrowser1
    else
      Web:=Self.Web;
  except
  end;

  Handled := False;
  with Msg do
    if ((Message >= WM_KEYFIRST) and (Message <= WM_KEYLAST)) and
    ((wParam in StdKeys) or {$IFDEF VER120}(GetKeyState(VK_CONTROL) < 0) or {$ENDIF}
      (wParam in ExtKeys) and ((lParam and fExtended) = fExtended)) then
    try
      if IsChild(Web.Handle, hWnd) then
{ handles all browser related messages }
      begin
        with Web.Application as IOleInPlaceActiveObject do
          Handled := TranslateAccelerator(Msg) = S_OK;
        if not Handled then
        begin
          Handled := True;
          TranslateMessage(Msg);
          DispatchMessage(Msg);
        end;
      end;
    except end;
end; // IEMessageHandler

作者:网络 来源:转载
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
本类推荐
  • 没有
本类固顶
  • 没有
  • 盒子文章(www.2ccc.com) © 2024 版权所有 All Rights Reserved.
  • 沪ICP备05001939号