Solved

HTML element position

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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 tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

15 Experts available now in Live!

Get 1:1 Help Now