Solved

HTML element position

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Virtuailstring tree compare node issue 14 117
Reconfigure Delphi Install? 2 51
Multiple image collision 13 75
Delphi: sending SMS on android platform 1 21
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

772 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