Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# HTML element position

Posted on 2004-09-19
Medium Priority
489 Views
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
Question by:fidel83
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 2

LVL 4

Accepted Solution

MrJul earned 2000 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

ID: 12095348
PS: you have to put MSHTML in your uses clause.
0

LVL 1

Author Comment

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

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

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

## Featured Post

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
###### Suggested Courses
Course of the Month5 days, 13 hours left to enroll