How to change the mouse cursor in Objective-C

为君一笑 提交于 2019-12-19 11:51:01

问题


I am making an image editing application, but it looks very incomplete without a cursor like they have in Photoshop for the paintbrush. How can I set the icon, and have it change back when I exit the application?

This is the code in my header file (just in case it's needed):

@interface test : NSWindow <NSWindowDelegate> {
    IBOutlet id myView;

}

@end

myView is an NSView (customView) that will display everything.


回答1:


There's a handy NSCursor class for handling cursor appearance. If one of the built-in cursors doesn't look how you need it to, you can initialize a new NSCursor with an NSImage and -set it to be the active cursor.




回答2:


If the drawing area is rectangular(NSTracking Areas are always rectangular) :- Use the mouseEntered,mouse Exited methods of NSTrackingArea to track and change the mouse cursor.The mouse cursor could be changed using NSCursor class.

If the tracking area is not rectangular then make a bigger rectangular tracking area enclosing the whole drawing region and then track the mouseMoved events inside the tracking area to set the cursor appropriately . Refer to apple documentation for more details.



来源:https://stackoverflow.com/questions/5544757/how-to-change-the-mouse-cursor-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!