可可:我想知道屏幕上状态栏图标的位置 [英] cocoa:I want to know the location of the status bar icon on the screen
问题描述
我想知道屏幕上状态栏图标的位置
- (void)awakeFromNib {
statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain];
NSBundle * bundle = [NSBundle mainBundle];
statusImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@rofType:@png]];
statusHighlightImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@rh \"ofType:@png]];
[statusItem setImage:statusImage];
[statusItem setAlternateImage:statusHighlightImage];
[statusItem setTitle:@APP];
[statusItem setToolTip:@你不需要这个...];
[statusItem setHighlightMode:YES];
NSRect rect = [[[statusItem view] window] frame];
NSLog(@%f,rect.origin.y);
}
这样做并没有到达
NSRect rect = [[[statusItem view] window] frame];
NSLog(@%f,rect.origin.y);
您尚未设置自定义视图项目,因此调用视图
将返回nil。
我要猜测你想要的原因
如果您要对状态项执行操作,则可能如下所示:
- (IBAction)someAction:(id)sender
{
NSWindow * window = [[[NSApplication sharedApplication] currentEvent]窗口];
NSRect rect = [window frame];
NSLog(@%f,rect.origin.y);
}
,然后设置状态项的操作,如下所示:
[statusItem setAction:@selector(someAction :)];
,然后每当你点击你的状态项,你会在你的日志中看到这样的: / p>
2012-04-17 20:40:24.344 test [337:403] 1578.000000
您可以使用这些信息来定位相对于状态项的窗口(例如Matt Gemmell的MAAttachedWindow)。
I want to know the location of the status bar icon on the screen
-(void)awakeFromNib{ statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength] retain]; NSBundle *bundle = [NSBundle mainBundle]; statusImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"r" ofType:@"png"]]; statusHighlightImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"rh" ofType:@"png"]]; [statusItem setImage:statusImage]; [statusItem setAlternateImage:statusHighlightImage]; [statusItem setTitle:@"APP"]; [statusItem setToolTip:@"You do not need this..."]; [statusItem setHighlightMode:YES]; NSRect rect = [[[statusItem view] window ] frame]; NSLog(@"%f",rect.origin.y); }
To do so and did not get to
NSRect rect = [[[statusItem view] window ] frame]; NSLog(@"%f",rect.origin.y);
解决方案You haven't set a custom view for your status item, so calling
view
is going to return nil.I'm going to guess that the reason you want to know the location is when you click the status item.
If you were to implement an action for your status item, it might look like this:
- (IBAction)someAction:(id)sender { NSWindow *window = [[[NSApplication sharedApplication] currentEvent] window]; NSRect rect = [window frame]; NSLog(@"%f",rect.origin.y); }
and then you'd set your status item's action like this:
[statusItem setAction:@selector(someAction:)];
and then whenever you clicked your status item, you'd see something ilke this in your log:
2012-04-17 20:40:24.344 test[337:403] 1578.000000
You could use that information to, for example, position a window (like Matt Gemmell's MAAttachedWindow) relative to the status item.
这篇关于可可:我想知道屏幕上状态栏图标的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!