【问题标题】:how can I detect if a button was pressed on a simple webpage using embeddedwb如何使用 Embeddedwb 检测是否在简单网页上按下了按钮
【发布时间】:2016-01-29 19:34:47
【问题描述】:

我在我的应用程序中使用 Embeddedwb,我有一个带有按钮的简单网页

<input name=mpi type=submit value=" Continue ">

我尝试过,但效果不佳

if E.outerHTML = '<input name=mpi type=submit value=" Continue ">' then
begin
  if rLoginGate.IsConnectedToLoginGate then
  begin
    ToggleChatBtn;
  end;
end;

现在我想做的是,当我按下按钮时,我需要我的应用程序将其拾起并运行一个简单的命令,例如消息框,有人有什么想法吗??

谢谢

【问题讨论】:

  • 如果您在 q 中添加您正在使用的 Delphi 和 TEmbeddedWB 的版本,它可能会帮助您获得比我更好的答案。

标签: delphi dom mshtml


【解决方案1】:

一种方法是使用 MSHTML.PAS 中的 HTML DOM 对象模型接口。

我之前的回答在这里:Detect when the active element in a TWebBrowser document changes 展示了如何通过 TWebBrowser 的 Document 对象访问它。 TEmbeddedWB 也通过其 Document 对象提供对它的访问。

该答案和它的 cmets 显示了如何捕获与文档中特定节点相关的事件,以及特定事件。

当然,如果 HTML 在您的控制之下,您可以通过为您感兴趣的 HTML 节点提供一个易于通过 DOM 模型找到的 ID 或属性,从而使事情变得更容易。

以下显示如何修改我的链接答案中的代码示例 将 OnClick 处理程序附加到特定元素节点:

procedure TForm1.btnLoadClick(Sender: TObject);
var
  V : OleVariant;
  Doc1 : IHtmlDocument;
  Doc2 : IHtmlDocument2;
  E : IHtmlElement;
begin
  //  First, navigate to About:Blank to ensure that the WebBrowser's document is not null
  WebBrowser1.Navigate('about:blank');

  //  Pick up the Document's IHTMLDocument2 interface, which we need for writing to the Document
  Doc2 := WebBrowser1.Document as IHTMLDocument2;

  //  Pick up the Document's IHTMLDocument3 interface, which we need for finding e DOM
  // Element by ID
  Doc2.QueryInterface(IHTMLDocument3, Doc);
  Assert(Doc <> Nil);

  //  Load the WebBrowser with the HTML contents of a TMemo
  V := VarArrayCreate([0, 0], varVariant);
  V[0] := Memo1.Lines.Text;
  try
    Doc2.Write(PSafeArray(TVarData(v).VArray));
  finally
    Doc2.Close;
  end;

  //  Find the ElementNode whose OnClick we want to handle
  V := Doc.getElementById('input1');
  E := IDispatch(V) as IHtmlElement;
  Assert(E <> Nil);

  //  Create an EventObject as per the linked answer
  DocEvent := TEventObject.Create(Self.AnEvent, False) as IDispatch;

  //  Finally, assign the input1 Node's OnClick handler
  E.onclick := DocEvent;
end;

PS:我使用 TEmbeddedWB 已经有一段时间了,可能会发现有一种更直接的方式来做这些事情,因为在我停止使用它之后(在 D5 时代)它发生了很多变化。即便如此,您也不会浪费时间研究这些东西,因为 COM 事件适用于各种事物,而不仅仅是 HTML DOM 模型。

【讨论】:

  • 我已经扩展了这个答案,使其更加独立,添加了一个代码示例,展示了如何将 OnClick 处理程序添加到特定的 HTML 元素节点。
猜你喜欢
  • 2015-10-30
  • 2013-06-11
  • 2012-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多