OSX Cocoa 光标在资源中
OSX Cocoa cursor in resources
全部,
在 Windows 上,游标由 cur
文件扩展名表示。 OSX Cocoa 应用程序是否有类似的东西?
在 XCode 中有没有办法编辑这些文件,就像在 MSVC 中有一些基本的图形编辑器一样?
此外,是否有特殊的 API 允许在 Cocoa 上加载此类文件?或者我可以将它们作为图像加载?
TIA!!
编辑:
与此同时,我认为我找到了一个可以满足我需求的好解决方案
NSString *path;
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:cursor_file ofType:@"cur"];
if( path )
{
CGImageSourceRef image = CGImageSourceCreateWithURL( path, nil );
CFDictionaryRef properties =
CGImageSourceCopyPropertiesAtIndex(图像, 0, 零);
NSInteger x = 属性["hotspotX"];
NSInteger y = 属性["hotspotY"];
}
除了不能编译:
我需要导入声明CGImageSourceCreateWithURL()的文件,我需要修复最后两行,因为数组下标不能是字符串。
有人可以帮忙吗?
在从位于应用程序包资源文件夹中的文件中检索其“热点”坐标后,以下源代码将在 Xcode 中从 .cur 图像创建自定义 NSCursor。用下面的代码替换 main.m 文件的内容并删除 pre-supplied AppDelegate 文件以避免重复的符号。您需要将 .cur 文件 copy/paste 放入 Xcode 项目文件夹中,并将其拖放到项目导航器中。 Xcode 不会 自动将此文件放入应用程序包的 Resources 文件夹中(与 .png 文件不同),因此 copy/paste 它也在那里编译了应用程序。参考:
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
CGFloat x,y;
}
@end
@implementation AppDelegate
- (void) customCursor {
// **** Get HotSpot from .cur file **** //
NSBundle *bundle = [NSBundle mainBundle];
NSURL *url = [bundle URLForImageResource:@"myCursor.cur"];
if( url ) {
CGImageSourceRef image = CGImageSourceCreateWithURL( (__bridge CFURLRef)url, nil );
NSDictionary *properties = (__bridge NSDictionary *)CGImageSourceCopyPropertiesAtIndex( image, 0, nil );
x = [[properties objectForKey:@"hotspotX"]floatValue];
y = [[properties objectForKey:@"hotspotY"]floatValue];
CFBridgingRelease(image);
CFBridgingRelease((__bridge CFTypeRef _Nullable)(properties));
}
NSCursor *customCursor = [[NSCursor alloc] initWithImage:[NSImage imageNamed:@"myCursor.cur"] hotSpot: NSMakePoint( x, y) ];
[customCursor set];
NSLog(@"x = %0.02f",x);
NSLog(@"y = %0.02f",y);
}
- (void) arrowCursor {
[[NSCursor arrowCursor] set];
}
- (void) buildMenu {
NSMenu *menubar = [NSMenu new];
[NSApp setMainMenu:menubar];
NSMenuItem *menuBarItem = [NSMenuItem new];
[menubar addItem:menuBarItem];
NSMenu *appMenu = [NSMenu new];
[menuBarItem setSubmenu:appMenu];
[appMenu addItemWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@"q"];
}
- (void) buildWnd {
#define _wndW 500
#define _wndH 250
window = [[NSWindow alloc] initWithContentRect: NSMakeRect( 0, 0, _wndW, _wndH ) styleMask: NSWindowStyleMaskTitled | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskClosable backing: NSBackingStoreBuffered defer: NO];
[window center];
[window setTitle: @"Custom cursor"];
[window makeKeyAndOrderFront: nil];
// **** Button 1 **** //
NSButton *myBtn =[[NSButton alloc]initWithFrame:NSMakeRect( 30, 30, 135, 30 )];
[myBtn setBezelStyle:NSBezelStyleRounded ];
[myBtn setTitle: @"Custom Cursor"];
[myBtn setAction: @selector (customCursor)];
[[window contentView] addSubview: myBtn];
// **** Button 2 **** //
NSButton *myBtn2 =[[NSButton alloc]initWithFrame:NSMakeRect( 190, 30, 135, 30 )];
[myBtn2 setBezelStyle:NSBezelStyleRounded ];
[myBtn2 setTitle: @"Arrow Cursor"];
[myBtn2 setAction: @selector (arrowCursor)];
[[window contentView] addSubview: myBtn2];
// **** Quit btn **** //
NSButton *quitBtn = [[NSButton alloc]initWithFrame:NSMakeRect( _wndW - 50, 10, 40, 40 )];
[quitBtn setBezelStyle:NSBezelStyleCircular ];
[quitBtn setTitle: @"Q" ];
[quitBtn setAction:@selector(terminate:)];
[[window contentView] addSubview: quitBtn];
}
- (void) applicationWillFinishLaunching: (NSNotification *)notification {
[self buildMenu];
[self buildWnd];
}
@end
int main() {
NSApplication *application = [NSApplication sharedApplication];
AppDelegate *appDelegate = [[AppDelegate alloc] init];
[application setDelegate:appDelegate];
[application run];
return 0;
}
全部,
在 Windows 上,游标由 cur
文件扩展名表示。 OSX Cocoa 应用程序是否有类似的东西?
在 XCode 中有没有办法编辑这些文件,就像在 MSVC 中有一些基本的图形编辑器一样?
此外,是否有特殊的 API 允许在 Cocoa 上加载此类文件?或者我可以将它们作为图像加载?
TIA!!
编辑:
与此同时,我认为我找到了一个可以满足我需求的好解决方案
NSString *path;
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:cursor_file ofType:@"cur"];
if( path )
{
CGImageSourceRef image = CGImageSourceCreateWithURL( path, nil );
CFDictionaryRef properties =
CGImageSourceCopyPropertiesAtIndex(图像, 0, 零); NSInteger x = 属性["hotspotX"]; NSInteger y = 属性["hotspotY"]; }
除了不能编译:
我需要导入声明CGImageSourceCreateWithURL()的文件,我需要修复最后两行,因为数组下标不能是字符串。
有人可以帮忙吗?
在从位于应用程序包资源文件夹中的文件中检索其“热点”坐标后,以下源代码将在 Xcode 中从 .cur 图像创建自定义 NSCursor。用下面的代码替换 main.m 文件的内容并删除 pre-supplied AppDelegate 文件以避免重复的符号。您需要将 .cur 文件 copy/paste 放入 Xcode 项目文件夹中,并将其拖放到项目导航器中。 Xcode 不会 自动将此文件放入应用程序包的 Resources 文件夹中(与 .png 文件不同),因此 copy/paste 它也在那里编译了应用程序。参考:
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
CGFloat x,y;
}
@end
@implementation AppDelegate
- (void) customCursor {
// **** Get HotSpot from .cur file **** //
NSBundle *bundle = [NSBundle mainBundle];
NSURL *url = [bundle URLForImageResource:@"myCursor.cur"];
if( url ) {
CGImageSourceRef image = CGImageSourceCreateWithURL( (__bridge CFURLRef)url, nil );
NSDictionary *properties = (__bridge NSDictionary *)CGImageSourceCopyPropertiesAtIndex( image, 0, nil );
x = [[properties objectForKey:@"hotspotX"]floatValue];
y = [[properties objectForKey:@"hotspotY"]floatValue];
CFBridgingRelease(image);
CFBridgingRelease((__bridge CFTypeRef _Nullable)(properties));
}
NSCursor *customCursor = [[NSCursor alloc] initWithImage:[NSImage imageNamed:@"myCursor.cur"] hotSpot: NSMakePoint( x, y) ];
[customCursor set];
NSLog(@"x = %0.02f",x);
NSLog(@"y = %0.02f",y);
}
- (void) arrowCursor {
[[NSCursor arrowCursor] set];
}
- (void) buildMenu {
NSMenu *menubar = [NSMenu new];
[NSApp setMainMenu:menubar];
NSMenuItem *menuBarItem = [NSMenuItem new];
[menubar addItem:menuBarItem];
NSMenu *appMenu = [NSMenu new];
[menuBarItem setSubmenu:appMenu];
[appMenu addItemWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@"q"];
}
- (void) buildWnd {
#define _wndW 500
#define _wndH 250
window = [[NSWindow alloc] initWithContentRect: NSMakeRect( 0, 0, _wndW, _wndH ) styleMask: NSWindowStyleMaskTitled | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskClosable backing: NSBackingStoreBuffered defer: NO];
[window center];
[window setTitle: @"Custom cursor"];
[window makeKeyAndOrderFront: nil];
// **** Button 1 **** //
NSButton *myBtn =[[NSButton alloc]initWithFrame:NSMakeRect( 30, 30, 135, 30 )];
[myBtn setBezelStyle:NSBezelStyleRounded ];
[myBtn setTitle: @"Custom Cursor"];
[myBtn setAction: @selector (customCursor)];
[[window contentView] addSubview: myBtn];
// **** Button 2 **** //
NSButton *myBtn2 =[[NSButton alloc]initWithFrame:NSMakeRect( 190, 30, 135, 30 )];
[myBtn2 setBezelStyle:NSBezelStyleRounded ];
[myBtn2 setTitle: @"Arrow Cursor"];
[myBtn2 setAction: @selector (arrowCursor)];
[[window contentView] addSubview: myBtn2];
// **** Quit btn **** //
NSButton *quitBtn = [[NSButton alloc]initWithFrame:NSMakeRect( _wndW - 50, 10, 40, 40 )];
[quitBtn setBezelStyle:NSBezelStyleCircular ];
[quitBtn setTitle: @"Q" ];
[quitBtn setAction:@selector(terminate:)];
[[window contentView] addSubview: quitBtn];
}
- (void) applicationWillFinishLaunching: (NSNotification *)notification {
[self buildMenu];
[self buildWnd];
}
@end
int main() {
NSApplication *application = [NSApplication sharedApplication];
AppDelegate *appDelegate = [[AppDelegate alloc] init];
[application setDelegate:appDelegate];
[application run];
return 0;
}