I'm trying to get the id of a htmlinputtextelement in webbrowser where the caret is blinking. So when I press TAB it changes.
How can I do this in delphi?
You know when you go onto a website and there are multiple inputtextelements. You can scroll through them开发者_StackOverflow 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.
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);
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.
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;
精彩评论