【发布时间】:2011-11-15 21:16:12
【问题描述】:
我正在尝试制作一个允许在 ShieldingWindowLevel 绘制的覆盖窗口,但是当窗口出现时,光标仍然是默认指针。我想把它改成十字准线。在我感到困惑之前拥有控制器 NSCursors 为什么从未调用过 resetCursorRects。
我手动创建窗口如下(在我的 AppController 类中):
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Create the window
NSRect frame = [[NSScreen mainScreen] frame];
// Provide a small area on the right to move the cursor in-and-out of the window.
frame.size.width = frame.size.width - 20;
self.window = [[NSWindow alloc] initWithContentRect:frame
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO];
[self.window setAcceptsMouseMovedEvents:YES];
[self.window setOpaque:NO];
[self.window setLevel:CGShieldingWindowLevel()];
[self.window setBackgroundColor:[NSColor colorWithDeviceRed:0.0 green:0.0 blue:1.0 alpha:0.2]];
// Create the subview
ScreenOverlayView *subview = [[ScreenOverlayView alloc] initWithFrame:NSZeroRect];
[[self.window contentView] addSubview:subview];
// Add subview and show window
[self.window setContentView:subview];
[self.window makeFirstResponder:subview];
[self.window orderFrontRegardless];
}
使用以下 NSView 子类:
@implementation ScreenOverlayView
- (void) resetCursorRects {
[super resetCursorRects];
[self addCursorRect: [self bounds]
cursor: [NSCursor crosshairCursor]];
}
// ...
@end
我创建了一个示例项目来展示这个案例并将它发布到github,最有趣的文件是ScreenOverlayView.m和AppDelegate.m。
我应该指出,我也花了很多时间尝试使用 NSTrackingArea 来实现这一点,正如您在示例项目中看到的那样。如果鼠标在出现后进入视图,则跟踪区域有效,但如果它在内部开始,则无效。如果我有办法设置初始光标,使用 MouseEnter 和 MouseLeave 会很好,但它只会在变回之前改变一瞬间。
如何让 resetCursorRects 被调用 - 或者 - 当我将光标移动到超级视图时如何设置光标?
【问题讨论】:
标签: objective-c macos cocoa