Search code examples
macossdkoperating-systemosx-snow-leopardnscursor

Simplest use of NSCursor set command


I am trying to get a custom cursor for my OSX application. I've read about doing this without success for the last few hours. So i've moved to the simplest aspect, simply setting the current cursor as the crosshair cursor, in a project that has absolutely nothing else.

Here is the most basic setup which does not change the cursor for me. Using XCode, I made a new Cocoa project. The only piece of code I wrote is one single line in the following function.

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
        [[NSCursor crosshairCursor] set];
}

This does not change the cursor, which probably means I am missing something extremely basic, but cannot figure out for the life of me.

I tried many variations, using the NSCursor -push method to change the current cursor, and it does not work. I also tried putting an NSButton and then doing as a different answer suggests here (where button is of the type NSButton *):

[button addCursorRect:[button bounds] cursor:[NSCursor crosshairCursor]];

I've written a couple apps for the iPhone before, but never for the Mac, but it seems pretty similar. Am I missing something hopelessly stupid? I also tried playing with the addCursorRect:cursor: method, but I'm not sure I did it right. It wasn't clear what function is supposed to call it, and from where.

Once I figure out how to show ANY different cursor, then I want to move on to making my own images for the cursor.

Thanks!


Solution

  • I'm not sure why, but setting the cursor in applicationDidFinishLaunching doesn't seem to work. (I just tried it.) But it will work if you do it later, like as the result of clicking a button or maybe use a timer if you want it to happen at launch.

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
        [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timer:) userInfo:nil repeats:NO];
    }
    
    -(void)timer:(id)foo
    {
        [[NSCursor pointingHandCursor] set];
    }