Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Èxtracting cursor co-ord's from CPoint 3

Status
Not open for further replies.

richsb

Technical User
Aug 2, 2001
34
NO
Hi,

I am trying to extract the cursor co-ordinates from the OnMouseMove(), but am unable to get the x y data from the CPoint type into a more usable int type. I am doing this as I want to be able to use the position of the mouse in a SWITCH{CASE} statement, and SWITCH doesn't accept CPoint types.

Any suggestions much appreciated.

Rgds
Richard
 
Hi, maybe I didnt understand your problem correctly, but

CPoint point

x=point.x;y=point.y; //U'll get to longs,

I do not know, may be you must transform it depending on the mode the CDC is set.
if you need x and y in one place for your problem you coud do

long XandY=(((x & 0xFFFF)<<16) | (y && 0xFFFF));

but XandY uses only the lower bytes of x and y,
the sign of XandY is the same as the sign of x

Hope I understood you correct...



Greetings Andreas
 
Hi Andreas,

Thanks for replying.

I'll try to explain a bit more. I can get the x & y co-ordinates of the mouse cursor position as a CPoint variable. I then want to use these x & y values in a switch statement, as two seperate values. However, the switch statement doesn't take CPiont values/variables. So - I believe - I need to convert then to something acceptable for the switch statement such as an int.

From that then, are you saying that the variable type of the CPoint output point.x & point.y will be two 'longs' ? If this is the case then I might be able to use it as is, but I will try that out later.

Cheers
Richard
 
If you are using a Switch statement to test the mouse location, realize that you must provide a case for every possible combination of (x,y). I'm pretty sure that's not what you want.

Rather, If you are testing the point to see if it falls in some areas, use PtInRect member function of CRect or PtInRgn member function of CRgn.

I REALLY hope that helps.
Will
 
I agree that you probably do not want to use the switch statement. More than likely you want an if-then. Here is some code that will get you started. Hope this helps:

CPoint mouse; //This is passed into the OnMouseMove event
int xPos = (int)mouse.x;
int yPos = (int)mouse.y;

//You will need to convert the xPos, yPos values to the current drawing mode.
xPos = convert(xPos);
yPos = convert(yPos);

//You will also need to get the boundary of the area you are testing. The following code creates a centered square that is 10% wide and 10% long of the view.
CRect rectView;
GetClientRect(rectView);

CRect rectTest;
rectTest.left = (int)((double)rectView.Width()*0.45);
rectTest.right = (int)((double)rectView.Width()*0.55);
rectTest.top = (int)((double)rectView.Height()*0.45);
rectTest.bottom = (int)((double)rectView.Height()*0.55;

//Then you will need to test if the mouse is inside that area that you defined above
if(xPos>=rectTest.left && xPos<=rectTest.right && yPos>=rectTest.top && yPos<=rectTest.bottom)
{
/************IMPLEMENT YOUR SPECIAL CODE HERE*************
}

Hopefully, this will push you in a direction that leads to a solution for you!

 
Andreas, Will & BCRavens,

Fellas, thanks for the replies.

It was simple in the end when I realised that to get each co-ordinate out you simply write-

int x = point.x
int y = point.y

Why don't MSoft just explain that in their help files ?@#$%^&*$#@!$

Also, you were rightI didn't need a SWITCH statement and instead used an 'if then' condition.

Cheers
Richard
 
>Why don't MSoft just explain that in their help files ?@#$%^&*$#@!$

Using classes and class members is very basic C++ stuff and not MS-specific.

The properties of the CPoint class is fairly well documented in MSDN (for example at , so what would you say isn't explained there?




Nerdy signatures are as lame as the inconsistent stardates of STTNG.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top