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;