Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2002-05-15
8
Medium Priority
?
379 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 300 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Integration Management Part 2
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

916 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