Solved

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

Posted on 2002-05-15
8
359 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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

12 Experts available now in Live!

Get 1:1 Help Now