简体   繁体   English

Delphi - TWebBrowser问题

[英]Delphi - TWebBrowser issues

Two quick questions 两个快速问题

  1. How do I set focus to a TWebBrowser? 如何将焦点设置为TWebBrowser? This is so the mouse wheel scrolls the display without having to click within the TWebBrwoser display area first. 这样,鼠标滚轮就可以滚动显示,而无需先在TWebBrwoser显示区域内单击。 It has a setfocus method that does nothing (or seems to do nothing). 它有一个setfocus方法,什么都不做(或似乎什么都不做)。

  2. Within a TWebBrowser, right click a displayed link and select properties. 在TWebBrowser中,右键单击显示的链接并选择属性。 The OK and Cancel buttons are disabled and you cannot close the dialog. “确定”和“取消”按钮被禁用,您无法关闭对话框。 You need to end task your app to kill it. 你需要结束任务你的应用程序来杀死它。

Any ideas? 有任何想法吗?

Thanks, Jason. 谢谢你,杰森。

Answer for Question 1 after much web hunting.... 经过多次网络搜索后回答问题1 ....

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

This is covered in the following article by Peter Johnson, How to make a TWebBrowser become the active control when clicked . 这将在Peter Johnson的以下文章中介绍, 如何使TWebBrowser成为单击时的主动控件

To summarise heavily, add this OnCommandStateChange event: 要进行大量总结,请添加此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;

There is a lot more detail in the article, please do make sure you read it all. 文章中有更多细节,请务必仔细阅读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM