Delphi - TWebBrowser问题

3

两个快速问题

  1. 如何将焦点设置到 TWebBrowser?这样,鼠标滚轮就可以在不必先单击 TWebBrwoser 显示区域的情况下滚动显示内容。它有一个 setfocus 方法,但似乎没有起作用。

  2. 在 TWebBrowser 中,右键单击一个已显示的链接并选择属性。确定和取消按钮都被禁用,无法关闭对话框。您需要结束任务来终止应用程序。

有什么想法吗?

谢谢, Jason。


在SO上最好分别提出问题。 SO的目的是创建一个具有高质量答案的问题存储库。 如果您对问题获得两个单独的答案,您会接受哪一个? - Argalatyr
没问题。我以后会创建单独的问题。杰森。 - Jason
2个回答

6

在经过大量的网络搜索后,回答问题1的答案是....

 with WebBrowser1 do
 if Document <> nil then
 with Application as IOleobject do
 DoVerb(OLEIVERB_UIACTIVATE, nil, WebBrowser1, 0, Handle, GetClientRect);

0
以下是Peter Johnson的文章如何使TWebBrowser在单击时成为活动控件中涵盖的内容。
简而言之,添加此OnCommandStateChange事件:
procedure TWebBrowserFrame.CommandStateChange(Sender: TObject;
  Command: Integer; Enable: WordBool);
var
  Doc: IHTMLDocument2;        // document object
  Sel: IHTMLSelectionObject;  // current selection
begin
  // Check we have a valid web browser triggering this event
  if not Assigned(Sender) or not (Sender is TWebBrowser) then
    Exit;
  // Check we have required command
  if TOleEnum(Command) <> CSC_UPDATECOMMANDS then
    Exit;
  // Get ref to document object and check not nil
  Doc := Browser.Document as IHTMLDocument2;
  if not Assigned(Doc) then
    Exit;
  // Get ref to current selection
  Sel := Doc.selection as IHTMLSelectionObject;
  // If selection is of correct type then we have a mouse click
  if Assigned(Sel) and (Sel.type_ = 'Text') then
  begin
    // Make the web browser the form's active control
    (Sender as TWebBrowser).SetFocus;
    Doc.parentWindow.focus;
  end;
end;

文章中还有很多细节,请确保你把它们全部阅读。


1
我加入了一篇文章的引用,说明你是从哪里获得这段代码的。请不要发布没有归属的代码。 - David Heffernan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接