Solved

HTML element position

Posted on 2004-09-19
5
473 Views
Last Modified: 2010-04-05
Hi,

I'm using 'elementFromPoint' to return me an html 'element' object. this works fine. I would like to know the element's left and top coordinates.
Any ideas?
0
Comment
Question by:fidel83
  • 3
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
MrJul earned 500 total points
ID: 12095347
I wrote this little function which do the trick :
You have to use offsetLeft & offsetTop, but since those properties returns parent-relative coordinates, the function loops through all parents.
At the end, you have absolute coordinates.

function GetElemCoordsFromPoint(WB: TWebBrowser; X, Y: Integer): TPoint;
var
    Doc: IHTMLDocument2;
    Elem: IHTMLElement;
begin
    Result.X := 0;
    Result.Y := 0;
    Doc := WB.Document as IHTMLDocument2;
    Elem := Doc.elementFromPoint(X, Y);
    while Elem <> nil do
    begin
        Inc(Result.X, Elem.offsetLeft);
        Inc(Result.Y, Elem.offsetTop);
        Elem := Elem.offsetParent;
    end;
end;
0
 
LVL 4

Expert Comment

by:MrJul
ID: 12095348
PS: you have to put MSHTML in your uses clause.
0
 
LVL 1

Author Comment

by:fidel83
ID: 12098039
hey MrJul,
I'm using 'variant' for the internet explorer object. is there a way to convert your code so that it works with this?
0
 
LVL 4

Expert Comment

by:MrJul
ID: 12099574
It depends of what is stored in your Variant.
If it is a IWebBrowser2 interface as I suspect, do the following :

Replace
function GetElemCoordsFromPoint(WB: TWebBrowser; X, Y: Integer): TPoint;

By
function GetElemCoordsFromPoint(WB: IWebBrowser2; X, Y: Integer): TPoint;

Then use the following code to call the function :
(V is assumed to be your Variant)

var
    Pt: TPoint;
    VarData: PVarData;
begin
    VarData := FindVarData(V);
    GetElemCoordsFromPoint(IWebBrowser2(VarData^.VDispatch), X, Y);
0
 
LVL 1

Author Comment

by:fidel83
ID: 12108414
thanks MrJul,
I ended up changing my code to use IHTMLDocument2
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question