Setting a window's origin to the current mouse coordinates

Discussion in 'Mac Programming' started by Spike099, Feb 12, 2008.

  1. macrumors regular

    Joined:
    Feb 18, 2007
    Location:
    Canada
    #1
    I am programming in Cocoa. When the window opens, I would like the window's origin to move to where the mouse is.

    This is basically the exact same implementation as connecting outlets in Interface Builder.

    I already have a IBAction function that is called to open a window, I just need to find the cursor's coordinates.

    Code:
    - (IBAction)open:(id)sender {
    	[self makeKeyAndOrderFront:self];
    	if (!isOpen) {
    		if (windowSetsOriginToMouseOnOpen) {
    			[self setFrameOrigin:*];
    		}
    		
    		if (windowFades)
    			[[self animator] setAlphaValue:1.0];
    		else
    			[self setAlphaValue:1.0];
    		isOpen = YES;
    	}
    }
    So, where the * is, I need to input the mouse coordinates.

    Is this how I should tackle this problem?

    Thanks for your help guys.
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    You should be able to use

    Code:
    [NSEvent mouseLocation]
    Although I haven't tested it.
     
  3. thread starter macrumors regular

    Joined:
    Feb 18, 2007
    Location:
    Canada
    #3
    Worked like a charm, thank you kainjow.
     

Share This Page