【问题标题】:TWebBrowser - How to detect which tag was clicked?TWebBrowser - 如何检测点击了哪个标签?
【发布时间】:2017-02-08 09:56:14
【问题描述】:

DesignMode 开启时如何检测 TWebBrowser 中点击了哪个 HTML 元素(标签)?

问题:
当 DesignMode 开启时:

  • Document_OnMouseOver 永远不会执行。
  • 文档从未完成加载。
  • 单击链接时未调用BeforeNavigate2。

更新:
我需要的是 IHTMLTxtRange (我认为)。当我双击链接/单词时,它会起作用。但是当没有选择文本/链接时,我不知道如何在插入符号下获取标签。

procedure TForm1.getRange;
var
  Sel: IHTMLSelectionObject;
  Range: IHTMLTxtRange;
  Doc: IHTMLDocument2;
begin
    Doc := EmbeddedWB.Doc2;
    if Assigned(Doc) then
    begin
      Sel := Doc.selection;
      if Assigned(Sel) then
      begin
        if (Sel.type_ = 'None') or (Sel.type_ = 'Text') then
        begin
          Range := Sel.createRange as IHTMLTxtRange;
          Range.expand('word');

          Memo.Text:=
              Range.htmlText + crlf +    // full tag
              Range.text;                // only text
        end;
      end;
end;

【问题讨论】:

  • 我不认为活动元素是你想要的。当然你需要获取坐标(客户端坐标),然后调用IHtmlDocument2.elementFromPoint
  • 可以在OnBeforeNavigate事件中获取链接
  • 嗨,大卫。我昨天已经试过了,但没有运气。我只得到“身体”。这是代码:stackoverflow.com/questions/42110608/…
  • 只是消费事件? here 是一个很好的例子,here 是官方文档。
  • @DarkPresidentOfAmerica:这对你有帮助吗?

标签: html delphi onclick delphi-xe7 twebbrowser


【解决方案1】:

这是如何使用鼠标坐标获取在浏览器中被点击的元素的示例!

 var
  X, Y: Integer;
  document,E: OleVariant;
begin
  if (Msg.message = WM_LBUTTONDOWN) and IsDialogMessage(WebBrowser1.Handle, Msg) then
  begin
    X := LOWORD(Msg.lParam);
    Y := HIWORD(Msg.lParam);
    document := WebBrowser1.Document;
    E := document.elementFromPoint(X, Y);
   Edit1.Text := 'You clicked on:' + E.outerHTML;
  end;
  Handled := False;
end;

【讨论】:

    猜你喜欢
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-18
    相关资源
    最近更新 更多