delphi 中 webbrowser1 浏览器中浏览网页,输入文本不能用ENTER键,无论你怎么按ENTER键,它都不会换行,还有,右键菜单的复制,和粘贴根本不是那么一回事,为什么呢?为什么??答案很简单,因为webbrowser不支持回车键响应,必须加上代码才行。

以下方法经本人测试通过,操作系统为XP sp2,开发环境Delphi7,浏览器是IE7.

1,在使用TwebBrowser控件的窗体(例如MainForm)中创建一个函数(代码里的wb对应你的webbrowser名称)
procedure TfrmMain.MsgHandler(var Msg: TMsg; var Handled: Boolean);
var
iOIPAO: IOleInPlaceActiveObject;
Dispatch: IDispatch;
begin

{ exit if we don't get back a webbrowser object }
if (wb = nil) then
begin
Handled := False;
Exit;
end;

Handled := (IsDialogMessage(wb.Handle, Msg) = True);
if (Handled) and (not wb.Busy) then
begin
if FOleInPlaceActiveObject = nil then
begin
Dispatch := wb.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;

procedure TForm1.FormDeactivate(Sender: TObject);
begin
Application.OnMessage := oldHandler;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Application.OnMessage := oldHandler;
FOleInPlaceActiveObject := nil;
end;

2,在该窗体的FormCreate事件中,添加以下代码
oldHandler := Application.OnMessage;
Application.OnMessage := MsgHandler;

3,在该Form的private中声明以下两个全局变量
FOleInPlaceActiveObject: IOleInPlaceActiveObject;
oldHandler: TMessageEvent

4,把activeX头文件包进去

最后修改:2009 年 08 月 16 日
卧槽,全是白嫖客,服务器不要钱吗?