Finding a components ACTUAL LEFT&TOP..

Howdy,

I have a program in which I have a control bar, on which is a drop down tool button. When someone clicks the little down arrow it pops up my popup menu for people to select from.  The first time someone clicks the button itself, it also needs to drop that popup button down.  Which is easy enough;
Menu.Popup(x,y);

The problem I am having is getting my popup menu to appear right below the control.  I need to be able to find the correct x,y of the drop down tool button. Because it is on a control bar, which can be anywhere on the application, the drop down tool buttons top and left are 0.  (it is the left top most control on the control bar).

How do I find the Top and Left of the drop down tool button relevant to the screen?  Being that the 0,0 is the upper left-hand corner of the screen...

Hope I explained that well enough..

Thanks..
-Jason
LVL 4
PalamedesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ptmcompCommented:
var
  TopLeft: Point;
begin
  TopLeft:=ClientToScreen(Point(MyControl.Left, MyControl.Top));

I hope that'll help you.

Regards, ptm.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PalamedesAuthor Commented:
That doesn't seem to work. I am still playing with it to try to get it to though.

-Pal
0
kretzschmarCommented:
hi pal,

what ptmcomp has suggested should work

try my simple sample
(drop a popupmenu and a button on a form,
define at least onemenu-item for the popupmenu)

procedure TForm1.Button1Click(Sender: TObject);
var P : TPoint;
begin
  If (Sender is TButton) then
  Begin
    P := ClientToScreen(PoinT(TButton(Sender).Left,
                              TButton(Sender).Top+
                              TButton(Sender).Height));
    PopupMenu1.PopUp(P.X,P.Y);
  end;
end;

meikl
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

PalamedesAuthor Commented:
I tried it on a blank form and it worked just dandy.. but when I place it in my program using a TToolButton it doenst work.. However, a blank form with a TToolButton DOES  work... *sigh*  what do you think is the reasoning.. ?


procedure TForm1.RacesBoxClick(Sender: TObject);
var P : TPoint;
begin
  If (Sender is TToolButton) then
  Begin
    P := ClientToScreen(PoinT(TToolButton(Sender).Left, TToolButton(Sender).Top+TToolButton(Sender).Height));
    RacesMenu.PopUp(P.X,P.Y);
  end;
end;
0
PalamedesAuthor Commented:
The error message I am getting is ;

[Error]Unit1.pas(542); ')' expected bu ',' found

And its refering to this line,

P := ClientToScreen(PoinT(TToolButton(Sender).Left,

with the curser there after the Left,

This is a cut and paste of the EXACT same code I used in a blank form and it worked like a charm..

any ideas?
0
philipleighsCommented:
This is what you want I think.

pt := MyControl.ClientToScreen(Point(0, 0));
Menu.Popup(pt.x, pt.y);

Cheers,
Phil.
0
PalamedesAuthor Commented:
The error message I am getting is ;

[Error]Unit1.pas(542); ')' expected bu ',' found

And its refering to this line,

P := ClientToScreen(PoinT(TToolButton(Sender).Left,

with the curser there after the Left,

This is a cut and paste of the EXACT same code I used in a blank form and it worked like a charm..

any ideas?
0
PalamedesAuthor Commented:
Phil,

I am getting the same error.

P := RacesBox.ClientToScreen(Point(0, 0));

is the line, and after the first 0, I get a ')' expected but ',' found.

Its as if it thinks POINT only has one argument..


Grrrr.. this may be a bug of the D5 demo.  Good thing the real version will be here tomorrow..

-pal
0
philipleighsCommented:
The reasoning is that a controls top and left is relative to it's parent, not the form.

Try my suggestion.

Cheers,
Phil.
0
philipleighsCommented:
Make sure Classes comes after Windows in the uses clause.

Cheers,
Phil.
0
PalamedesAuthor Commented:
I think this must be some sort of bug.  I am cutting and pasting these examples from a blank form to my form and it works in the blank form.  

the difference between "Myform" and the blank one are nil..

Again, I am using the demo version of D5 until my bought copy arrives.. I think its just buggy.

-Pal
0
PalamedesAuthor Commented:
"Make sure Classes comes after Windows in the uses clause. " 

It does.

-Pal
0
ptmcompCommented:
Have you delcared some variables or procedures named Point?
0
philipleighsCommented:

0
ptmcompCommented:
You could also write
var
  r: TRect;
  pt: TPoint; // Was my bug has to be TPoint instead of POint
begin
  Windows.GetWindowRect(MyControl, r);
  pt:=r.TopLeft;
  Menu.Popup(pt.x, pt.y);
end;
 
Regards, ptm
0
PalamedesAuthor Commented:
Okay.. I moved Classes to the very end of my Uses clause and it works like a charm now..

But I don't understand why...

Here is my Uses line

uses
  Windows, Messages, SysUtils, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, Buttons, ComCtrls, Menus, ToolWin, ImgList,  StdCtrls,
  SHDocVw, Classes;


Classes WAS after Controls, and it didnt work.. its not at the end and works..

can someone explain that? (and who should I give these points to?)

-Pal

(thanks everyone.. )
0
philipleighsCommented:
Point is a function declared in Classes.pas.

function Point(AX, AY: Integer): TPoint;

You can do this instead (same result).

>>>>>>>>
var
  P: TPoint;
begin
  P.x := 0;
  P.y := 0;
  P := RacesBox.ClientToScreen(P);
  Menu.Popup(P.x, P.y);
end;
<<<<<<<<<

Cheers,
Phil.




0
philipleighsCommented:
The explanation.

Point is a windows type, as well as a function.

The compiler uses the last version of the type it sees in the uses clause.

Cheers,
Phil.

PS: There has been some serious overlap here! Check the times!  :-)
0
PalamedesAuthor Commented:
heh, yeah I noticed the overlap.. I like a quick response though.. its handy..

Not sure who to give the points to though.. everyone was helpful.  Especially you Phil.. but I am going to give the points to PTMComp as he was the first to answer correctly.. It was just a matter of figuring out the Classes bit afterwards..

Thanks Everyone

-Pal
0
ptmcompCommented:
Thanx. #;o)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.