【发布时间】:2014-03-06 07:20:14
【问题描述】:
我将 NSWindow 子类化,并且我有一个 MYWindow 类实现以下方法:
-(void)resetCursorRects {
NSImage *image = [NSImage imageNamed:@"cursor.png"];
[image setSize:NSMakeSize(32, 32)];
NSCursor *cursor = [[NSCursor alloc] initWithImage:image hotSpot:NSMakePoint(1, 1)];
[super resetCursorRects];
[self addCursorRect:[self bounds] cursor:cursor];
}
这将改变整个窗口的光标,我将看到 cursor.png 而不是默认的鼠标指针。问题是这仅在 MYWindow 设置为关键窗口时才有效,这当然不是微不足道的。
在我的项目开始时,我只有一个主窗口,但现在我需要有两个不同的 MYWindow。两个窗口的问题是无法将两者都设置为关键窗口,因此自定义鼠标指针仅显示在活动窗口上。我需要单击另一个窗口才能使光标出现。
有没有办法解决这个问题?所以我在两个窗口上都有一个自定义光标?
编辑:尝试过 NSTrackingArea
我将此添加到我的内容视图的 init 方法中:
self.trackingArea = [[NSTrackingArea alloc] initWithRect:[self frame] options: (NSTrackingCursorUpdate | NSTrackingActiveAlways | NSTrackingMouseMoved) owner:self userInfo:nil];
[self addTrackingArea:self.trackingArea];
然后我覆盖了 cursorUpdate: 像这样:
-(void)cursorUpdate:(NSEvent *)event {
NSLog(@"event : %@", event);
[[NSCursor crosshairCursor] set];
}
当包含 NSImageView 派生类的 NSWindow 是关键窗口时,这会使 crosshairCursor 显示。但是,如果我将应用程序中的另一个 NSWindow 设为关键窗口,则光标会再次返回标准光标。我做错了吗?
【问题讨论】:
标签: macos cocoa mouse mouseover nswindow