Solved

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

Posted on 2002-05-15
8
362 Views
Last Modified: 2010-04-04
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
0
Comment
Question by:mgroarty
  • 4
  • 2
  • 2
8 Comments
 
LVL 17

Expert Comment

by:geobul
ID: 7011353
Hi,

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

Regards, Geo
0
 
LVL 17

Accepted Solution

by:
geobul earned 75 total points
ID: 7011407
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
 
LVL 6

Expert Comment

by:Holger101497
ID: 7011986
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
 
LVL 17

Expert Comment

by:geobul
ID: 7013029
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 6

Expert Comment

by:Holger101497
ID: 7013306
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
 

Author Comment

by:mgroarty
ID: 7013315
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
 

Author Comment

by:mgroarty
ID: 7013333
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
 
LVL 17

Expert Comment

by:geobul
ID: 7013392
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IExtractImage Delphi 14 205
Delphi XE10, MySQL Query 4 126
How to create virtual printer while installing my application 5 24
DBGrid or StringGrid ? 6 70
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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