Web浏览器上的插入符位置 [英] Caret position on webbrowser

查看:134
本文介绍了Web浏览器上的插入符位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在插入符号闪烁的webbrowser中获取htmlinputtextelement的ID。因此,当我按TAB键时,它会发生变化。

I'm trying to get the id of a htmlinputtextelement in webbrowser where the caret is blinking. So when I press TAB it changes.

我该如何在delphi中做到这一点?

How can I do this in delphi?

您知道何时访问某个网站并且有多个输入文字元素。您可以通过按TAB滚动它们。在完成第1栏,TAB之后,请填写第2栏,TAB,第3栏,TAB,直到完成网站上的表格。我想做这个。通过知道当前插入符号所在的inputtextelement的ID是什么。

You know when you go onto a website and there are multiple inputtextelements. You can scroll through them by pressing TAB. When you are finished with box 1, TAB, fill in box 2, TAB, box 3, TAB till you have completed the form on the website. I want to do this. By knowing what the id is of the inputtextelement that the current caret is in.

您可以使用getcursorpos获得鼠标位置。你能以同样的方式获得插入符号的位置吗?它们不会为x和y提供相同的位置... ??

You can get the mouse position with getcursorpos. can you get the caret position the same way? They do not give the same location for x and y...??

procedure TForm1.Button2Click(Sender: TObject);
var
  MausPos: TPoint;
  HtmlElement: IHTMLElement;
    iHTMLDoc: IHtmlDocument2;
    tag1:string;
    id1:string;
begin
  if Supports(webbrowser1.Document, IHtmlDocument2, iHTMLDoc) then
  begin
    if GetcaretPos(MausPos) then
    begin
      MausPos := webbrowser1.screentoclient(MausPos);
      HtmlElement := iHTMLDoc.ElementFromPoint(MausPos.X, MausPos.Y);


推荐答案

插入符号并不像鼠标光标的位置那么简单:每个Window都可以自由创建和显示自己的插入符,无论它在哪里。这是在MSDN上使用Carets链接 。通常,您希望窗口仅在有键盘焦点的情况下显示插入符号,但我认为即使窗口没有键盘焦点,也没有什么可以阻止窗口显示插入符号。

The Caret is not as simple as the mouse cursor position: Each Window is free to create and display it's own caret, wherever it wants it. Here's a Using Carets link on MSDN. You'd normally expect a window to only show a Caret if it has keyboard focus, but a I don't think there's anything stopping a window from showing the Caret even if it doesn't have keyboard focus.

由于正常的行为是仅在出现键盘焦点时才显示插入符号,因此您可以使用以下方法进行检查: GetFocus 。但是您可能会发现TWebBrowser本身就是焦点,我怀疑每个HTML元素都有一个Window Handle。

Since the normal behavior is to only show the caret if there's keyboard focus, you may check for that using: GetFocus. But you'll likely find out the TWebBrowser itself is holding the focus, I doubt there's an Window Handle for each HTML element.

我认为您真正想要的是活动窗口元件。您可以使用以下命令获取该信息:

What I assume you actually want is the active element. You can get that using:

(TWebBrowser.Document as IHTMLDocument2).activeElement

以下是使用此属性的简短代码段:

Here's a short code snippet that uses this property:

procedure TForm25.Button2Click(Sender: TObject);
begin
  if (W.Document as IHTMLDocument2).activeElement <> nil then
    ShowMessage((W.Document as IHTMLDocument2).activeElement.tagName);
end;

这篇关于Web浏览器上的插入符位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆