问题描述
我想检测插入符是哪个HTML标记(更确切地说是超链接).
I want to detect on which HTML tag (more exactly hyperlink) is the caret.
procedure THTMLEdit.ShowTag;
var
CursorPos: TPoint;
HtmlElement: IHTMLElement;
iHTMLDoc: IHtmlDocument2;
begin
if Supports(wbBrowser.Document, IHtmlDocument2, iHTMLDoc) then
begin
if GetcaretPos(CursorPos) then
begin
CursorPos := wbBrowser.screentoclient(CursorPos);
HtmlElement := iHTMLDoc.ElementFromPoint(CursorPos.X, CursorPos.Y); // I NEED KEYBOARD CARET HERE, NOT MOUSE CURSOR
if HtmlElement <> NIL
then label1.Caption:= HtmlElement.tagName;
end;
end;
end;
注意:
TWebBrowser处于DesignMode(DesignMode:='On').
TWebBrowser在设计时采用其自己的形式,但在运行时将以另一种形式(在面板中)重新父级化.
Notes:
TWebBrowser is in DesignMode ( DesignMode := 'On' ).
TWebBrowser is in its own form at design time but at runtime is re-parented in another form (in a panel).
更新:
我需要的是IHTMLTxtRange(我认为).当我双击链接/单词时,它起作用.但是当不选择文本/链接时,我不知道如何将标签插入插入符号.
UPDATE:
The thing that I need is IHTMLTxtRange (I think). It works when I double click a link/word. But I don't know how to get the tag under caret when no text/link is selected.
推荐答案
GetcaretPos(CursorPos)返回客户端(相对)坐标(请参阅 GetCaretPos函数)
GetcaretPos(CursorPos) returns client (relative) coordinates (See GetCaretPos function)
删除wbBrowser.screentoclient(CursorPos),它应该可以正常工作.我已经在上面的代码示例中进行了测试
Remove wbBrowser.screentoclient(CursorPos) and it should work fine. I have tested with your code sample above
这篇关于TWebBrowser-检测插入符号下的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!