How do I set the cursor for an application?
How do I set the cursor for an application?
- Subject: How do I set the cursor for an application?
- From: Dave Mihalik <email@hidden>
- Date: Thu, 12 Apr 2007 00:11:01 -0400
I have a simple application that displays a main window. For this
simple application, I would like to change the background color of the
main window to red and also set the mouse cursor for the application to
the openhandCursor cursor . I have successfully set the background
color of the main window. Now, I just need to be able to set the mouse
cursor.
Here is my sample program using the set and push methods:
#import <Cocoa/Cocoa.h>
int main(int argc, const char **argv)
{
NSAutoreleasePool *myPool;
NSWindow *myMainWindow;
NSRect myRect;
NSColor *myWindowBackColor;
NSCursor *myCursor;
myPool = [[NSAutoreleasePool alloc] init];
NSApp = [NSApplication sharedApplication];
myRect = NSMakeRect(100,350,800,600);
myMainWindow = [[NSWindow alloc]
initWithContentRect: myRect
styleMask: NSTitledWindowMask |
NSClosableWindowMask |
NSMiniaturizableWindowMask |
NSResizableWindowMask
backing: NSBackingStoreBuffered
defer: NO];
// Set main window background color to red
myWindowBackColor = [NSColor colorWithDeviceRed: 1
green: 0
blue: 0
alpha: 1.0];
[myMainWindow setBackgroundColor:myWindowBackColor];
// Set the application cursor
myCursor = [NSCursor openHandCursor];
// According to the documentation at:
http://developer.apple.com/documentation/Cocoa/Conceptual/CursorMgmt/
Tasks/ChangingCursors.html
// it says "you can send a set message to the cursor."
[myCursor set]; // This does not work. The cursor is still an arrow.
// According to the documentation at:
http://developer.apple.com/documentation/Cocoa/Conceptual/CursorMgmt/
Tasks/ChangingCursors.html
// it says that "You can manage cursors in a stack, using the push
and pop methods of NSCursor. The stack’s top cursor is the current
cursor."
[myCursor push]; // This does not work. The cursor is still an
arrow.
[myMainWindow performZoom:NSApp];
[myMainWindow makeKeyAndOrderFront: nil];
[NSApp run];
[myPool release];
return(EXIT_SUCCESS);
}
I have also tried setting the mouse cursor using resetCursorRects along
with addCursorRect in the following example.
The following example will set the mouse cursor only after the mouse
has exited the main window view area and then returned back to the main
window view area.
When the program starts, the arrow cursor is displayed. If the mouse
cursor is moved to the window menu area and then back to the main
window view area, then the mouse cursor will change to the
openhandCursor. I want the mouse to change to the hand when the main
window is initially displayed without having to move the mouse to a
specific region of the main window.
#import <Cocoa/Cocoa.h>
@interface WindowView : NSView{}
-(void)resetCursorRects;
@end
int main(int argc, const char **argv)
{
NSAutoreleasePool *myPool;
NSWindow *myMainWindow;
NSRect myRect;
NSColor *myWindowBackColor;
NSCursor *myCursor;
NSView *myWindowView;
myPool = [[NSAutoreleasePool alloc] init];
NSApp = [NSApplication sharedApplication];
myRect = NSMakeRect(100,350,800,600);
myMainWindow = [[NSWindow alloc]
initWithContentRect: myRect
styleMask: NSTitledWindowMask |
NSClosableWindowMask |
NSMiniaturizableWindowMask |
NSResizableWindowMask
backing: NSBackingStoreBuffered
defer: NO];
myWindowView = [[[WindowView alloc] initWithFrame:myRect]
autorelease];
[myMainWindow setContentView:myWindowView];
[myMainWindow setDelegate:myWindowView];
// Set main window background color to red
myWindowBackColor = [NSColor colorWithDeviceRed: 1
green: 0
blue: 0
alpha: 1.0];
[myMainWindow setBackgroundColor:myWindowBackColor];
[myMainWindow performZoom:NSApp];
[myMainWindow makeKeyAndOrderFront: nil];
[NSApp run];
[myPool release];
return(EXIT_SUCCESS);
}
@implementation WindowView
-(void)resetCursorRects
{
[self addCursorRect:[self visibleRect] cursor:[NSCursor
openHandCursor]];
}
@end
Any help is appreciated...
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden