Solved

HTML element position

Posted on 2004-09-19
5
468 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

914 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now