全部,
在 Windows 上,游標由cur
檔案擴展名指示。OSX Cocoa 應用程式有類似的東西嗎?
XCode 中有沒有辦法編輯這些檔案,就像在 MSVC 中有一些基本的圖形編輯器一樣?
此外,是否有一個特殊的 API 允許在 Cocoa 上加載此類檔案?或者我可以將它們作為影像加載?
蒂亞!!
編輯:
與此同時,我認為我找到了一個可以滿足我需求的好解決方案
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() 的檔案,并且我需要修復最后兩行,因為陣列下標不能是字串。
有人可以幫忙嗎?
uj5u.com熱心網友回復:
以下源代碼將在 Xcode 中從 .cur 影像創建一個自定義 NSCursor,然后從位于 app bundle Resource 檔案夾中的檔案中檢索其“熱點”坐標。將 main.m 檔案的內容替換為下面的代碼,并洗掉預先提供的 AppDelegate 檔案以避免重復符號。您需要將 .cur 檔案復制/粘貼到 Xcode 專案檔案夾中,并將其拖放到專案導航器中。Xcode不會自動將此檔案放入應用程式包的 Resources 檔案夾中(與 .png 檔案不同),因此在編譯應用程式后也將其復制/粘貼到那里。參考:NSImage 加載的 Windows 游標 .cur 中的熱點?
#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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/473973.html