Can you convert 'obj.Left' value to 'CursorPos' value?

Hi,
i`m creating a rectangular component from TShape using Delphi6. when i add this cmponent to my form i want it the cursor icon to change.If the cursor is then moved in from the right or left edge of my component by 'x' pixels (for instance say 5px) i would like to change the cursor again. This means i would have a 5px border at the left and right sides of my component.I have created a private proc for the windows message WM_MOUSEMOVE and I have got what i want.The PROC is:

procedure TIBTTimeBandRect.WMMouseMove(var Message: TWMMouseMove);
var
  intTBLeft:Integer;  //TimeBands left cooordinate in reference to the screen as a whole
begin
  inherited;

    intTBLeft:=Parent.Left+Self.Left+4; //4 for Forms Border


    if ((mouse.CursorPos.X)>(intTBLeft+5))and ( ( (intTBLeft+self.Width)-5)>mouse.CursorPos.X)  then cursor:= crSizeAll
    else cursor:= crSizeWE;

end;

The problem is should i place this component under aother component i.e. a frame etc. it won't work as intTBLeft relies on the forms 'Left' property. How can i adapt it so that it will work no matter what form or component i place my component on i.e. perhaps changing the 'self.left' property to a 'cursorPos' equivalent value.

i`m relatively new to Delphi and would be grateful for any help on this matter.

Cheers,
Michael
mgroartyAsked:
Who is Participating?
 
geobulConnect With a Mentor Commented:
Or try this:

function GetScreenLeft(Control: TControl): integer;
var
  Point: TPoint;
begin
  Point.X := Control.Left;
  Point.Y := Control.Top;
  Point := Control.Parent.ClientToScreen(Point);
  result := Point.X;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(IntToStr(GetScreenLeft(Button1)));
end;

Regards, Geo
0
 
geobulCommented:
Hi,

Perhaps you have to use Owner instead of Parent:
intTBLeft:=Owner.Left+Self.Left+4;

Regards, Geo
0
 
Holger101497Commented:
I think the secret method you're looking for is called ClientToScreen() :-)

Ooops - I just see geobul has already mentioned this (kind of overlooked it)

It's easier to use than he thinks, though... you don't need to access the parent at all...

VAR upperLeft, lowerRight: TPoint;
BEGIN
  upperLeft.X := 4;
  upperLeft.Y := 4;
  upperLeft := clientToScreen(upperLeft);
  lowerRight.X := self.width-4;
  lowerRight.Y := self.height-4;
  lowerRight := clientToScreen(lowerRight);

  if ((mouse.CursorPos.X)>(upperLeft.X))and ( (mouse.CursorPos.X) < lowerRight.X))  then ...

END;

Looks more readable too :-)))
(it gives you the two corners that are important for your mouse changes --- do you not change the mouse for top/bottom?)

Hope that helps,
                    Holger

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.

 
geobulCommented:
Hi,
to Holger: your example uses ClientArea coordinates and NOT Left property of the control. There is a BIG difference between them. I know how to use that method but it seems that you don't.

Regards, Geo
0
 
Holger101497Commented:
Hey... let's stay friendly here, shall we?

Yes, I do use "client area coordinates", but there's one more difference between my code and yours:

I use THE CONTROL's OWN "clientToScreen"-method. It does this translation quite nicely!

You use the PARENT's clientToScreen and pass the control's coordinates. That's one time up the ladder and one time down the ladder...

Just try it...

Sincerely yours,
                    Holger

P.S.: Keep smiling :-))
0
 
mgroartyAuthor Commented:
Sorry Holger but the points goto Geobul,Thanks mate!! Your code works a treat although i`m not altogether sure of how the line
'Point := Control.Parent.ClientToScreen(Point);' actually works.(Perhaps cos i was at the champions league final last night and am still feeling the effects!!) Why do you reference the controls parent?

Anyway thanks alot for the help from both of you, its much appreciated.
Cheers,
Mick

0
 
mgroartyAuthor Commented:
Holger,

Just noticed your last comment, it appeared after i posted the accepted answer.your code works a treat also but i thought it was justthe forms coordinates and not the controls that you were calculaing.
So, if you tell me how I can transfer you the equivalent nbr of points i`ll do it!

sorry mate,
Mick
0
 
geobulCommented:
Thank you Mick. You were at the stadium in Glasgow last night? Wow great!!! I was watching it on TV.

Back to the issue: I used Control.Parent.ClientToScreen because Control.Left is a value in its Parent's Client Area relative to Parent.ClientOrigin. My function is applicable to all controls on a form. You could use:
Point.X := 0;
Point.Y := 0;
Point := Control.ClientToScreen(Point);
only with controls which ClientArea is the same as the control itself. Control.ClientWidth is sometimes smaller that the control.Width.

You may test it this way: drop a TButton and a TEdit on a form and run:

function GetScreenLeft(Control: TControl): integer;
var
 Point: TPoint;
begin
  if Control.Parent <> nil then begin // check if it has parent
    Point.X := Control.Left;
    Point.Y := Control.Top;
    Point := Control.Parent.ClientToScreen(Point);
    result := Point.X;
  end else raise Exception.CreateFmt('%s - has no parent',[Control.Name]); // raise exception
end;

procedure TForm1.Button1Click(Sender: TObject);
var
 Point: TPoint;
begin
  // my way
  ShowMessage(IntToStr(GetScreenLeft(Edit1)));

  // other way
  Point.X := 0;
  Point.Y := 0;
  Point := Edit1.ClientToScreen(Point);
  ShowMessage(IntToStr(Point.X));
end;



Hi Holger,
No problem with me. Just take a look at your first comment:
>It's easier to use than he thinks

Best regards to both of you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.