Lefora Free Forum
Loading
794 views

Mouse position in a view ?!

Page 1
1–3
rookie - member
4 posts

Hi !

I have a little problem with the mouse position on a cocoa program.
I want to check the position of the mouse over my view (to highlight some parts of the view when the mouse is over it, like a mouseOver function)

Well, i tried, each frame, to use the [NSEvent mouseLocation] function, and to compare the return value with the relative position of my view and here is the problem : this function return the screen coordinates, and not the view coordinates, so how can i compare these two coordinates and also resize and move my view ?!

Thanks for your answers, and sorry for my poor english.

Nicolas

rookie - member
4 posts

I found the NSTrackingArea, but, can it return the position of the mouse into this area ?! it will be perfect if it would work !

Thanks

Nico

?
288 posts

I'm not too sure I've understood your question here.  However we'll get there soon.

I have a tutorial about drawing in Quartz.  I've taken that code and modified it a little to collect the mouse event.  I've also added an NSTextField on the right.  Every time you click on the clock, the string on the right moves down and reports its coordinates.

I added a mouseDown method to the QuartzClockView class and you can see, I've extracted the position of the mouse from the event.  It's relative to the bottom/left.  convertPointToBase is relative to the top left (and I think it's at Screen resolution).


- (void) mouseDown:(NSEvent*) event
{
NSLog(@"mouseDown event = %@",event) ;
//
NSPoint p = [event locationInWindow];
NSPoint q = [self convertPointToBase:p];
NSLog(@"mouseDown p = %d,%d q = %d,%d",(int)p.x,(int)p.y,(int)q.x,(int)q.y) ;
[clockController setLabelTitle:bla bla bla];
}

I added a QuartzClockController class and a method to set the label.

Take a look:  http://clanmills.com/files/QuartzClock2.zip


__________________
Page 1
1–3

Locked Topic


You must be a member to post in this forum

Join Now!