WebBrowser支持ENTER CTRL+C

unit PennantWebBrowser;

interface

uses
    Windows, Messages, SysUtils, Classes, Menus, Graphics, Controls, Forms, Dialogs,
    OleCtrls, SHDocVw, ActiveX;

type
    TPennantWebBrowser = class(TWebBrowser)
    private
        { Private declarations }
        FOleInPlaceActiveObject: IOleInPlaceActiveObject;
        FSaveMessageHandler: TMessageEvent;
        procedure MyMessageHandler(var Msg: TMsg; var Handled: Boolean);
    protected
        { Protected declarations }
    public
        { Public declarations }
        constructor Create(AOwner: TComponent); override;
        destructor Destroy; override;
    published
        { Published declarations }
    end;

procedure Register;

implementation

procedure Register;
begin
    RegisterComponents('Pennant Components', [TPennantWebBrowser]);
end;

{ TPennantWebBrowser }

constructor TPennantWebBrowser.Create(AOwner: TComponent);
begin
    inherited;
    // save the application message handler and replace it with our own
    FSaveMessageHandler := Forms.Application.OnMessage;
    Forms.Application.OnMessage := MyMessageHandler;
end;

destructor TPennantWebBrowser.Destroy;
begin
    // restore the appplication message handler
    Forms.Application.OnMessage := FSaveMessageHandler;
    inherited;
end;

// this enables enter, CTRL+C and similar shortcut keys in the webbrowser
procedure TPennantWebBrowser.MyMessageHandler(var Msg: TMsg; var Handled: Boolean);
var
    iOIPAO : IOleInPlaceActiveObject;
    Dispatch : IDispatch;
begin
    Handled:=(IsDialogMessage(Handle, Msg) = True);

    if (Handled) and (not Busy) then
    begin
        if FOleInPlaceActiveObject = nil then
        begin
            Dispatch := 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
                //nothing - do not pass on Backspace, Left or Right arrows
            else
                FOleInPlaceActiveObject.TranslateAccelerator(Msg);
    end;
end;

end.

方法2:

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;

 

posted @ 2015-01-10 17:55  BeckSoft  阅读(194)  评论(0)    收藏  举报