将鼠标光标移动到无效的NSWindow [英] Change mouse cursor over inactive NSWindow

查看:555
本文介绍了将鼠标光标移动到无效的NSWindow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经子类化NSWindow,我有一个MYWindow类实现以下方法:

I have subclassed NSWindow and I have a MYWindow class implementing the following method:

-(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设置为关键窗口,这当然不是微不足道的,使它。

This will change the cursor for the whole window and I will see cursor.png instead of the default mouse pointer. The problem is that this only works if the MYWindow is set to the key window which is of course non trivial to make it.

在我的项目开始,我只有一个主窗口,但现在我需要有两个不同的MYWindow。两个窗口的问题是不可能设置为键窗口,因此自定义鼠标指针只显示在活动窗口。我需要点击其他窗口让光标出现。

In the beginning of my project I just have one main window but now I need to have two different MYWindow. The problem with two windows it is not possible to set both as the key window and hence the custom mouse pointer is only displayed on the active window. I need to click the other window to make the cursor appear.

有什么办法吗?那么我在两个窗口上都有自定义光标?

Is there any way around this? So I get a custom cursor on both windows?

编辑:尝试NSTrackingArea

我添加到我的内容视图的init方法:

I added this to my content view's init method:

self.trackingArea = [[NSTrackingArea alloc] initWithRect:[self frame] options: (NSTrackingCursorUpdate | NSTrackingActiveAlways | NSTrackingMouseMoved) owner:self userInfo:nil];
[self addTrackingArea:self.trackingArea];

然后我覆盖了cursorUpdate:像这样:

Then I overrided cursorUpdate: like this:

-(void)cursorUpdate:(NSEvent *)event {
    NSLog(@"event : %@", event);
    [[NSCursor crosshairCursor] set];
}

这使得当包含NSImageView派生类的NSWindow是key时,crosshairCursor显示窗口。但是如果我在应用程序中创建另一个NSWindow关键窗口,光标再次返回到标准光标。我做错了什么?

This makes the crosshairCursor show when the NSWindow that contains the NSImageView derived class is key window. But if I make another NSWindow within the app the key window, the cursor returns to the standard cursor again. Am I doing something wrong?

推荐答案

你应该能够添加一个改变游标的NSTrackingArea,

You should be able to add an NSTrackingArea that changes the cursor, as long as you don’t want it to also change when the app is inactive (that is essentially impossible).

编辑:

我可以使用下面的代码:

I was able to get this working with the following code:

- (vod)someSetup;
{
    NSTrackingArea *const trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect options: (NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways | NSTrackingInVisibleRect) owner:self userInfo:nil];
    [self.view addTrackingArea:trackingArea];
}

- (void)mouseEntered:(NSEvent *)theEvent;
{
    [[NSCursor IBeamCursor] push];
}

- (void)mouseExited:(NSEvent *)theEvent;
{
    [[NSCursor IBeamCursor] pop];
}

这篇关于将鼠标光标移动到无效的NSWindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆