cocoa:我想知道状态栏图标在屏幕上的位置

2024-01-09

我想知道状态栏图标在屏幕上的位置

-(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);

}

这样做并没有得到

NSRect rect = [[[statusItem view] window ] frame];

NSLog(@"%f",rect.origin.y);

您还没有为状态项设置自定义视图,因此调用view将返回零。

我猜测您想知道位置的原因是当您单击状态项时。

如果您要为状态项实施一项操作,它可能如下所示:

- (IBAction)someAction:(id)sender
{
    NSWindow *window = [[[NSApplication sharedApplication] currentEvent] window];
    NSRect rect = [window frame];

    NSLog(@"%f",rect.origin.y);
}

然后你可以像这样设置状态项的操作:

[statusItem setAction:@selector(someAction:)];

然后每当您单击状态项时,您都会在日志中看到类似以下内容:

2012-04-17 20:40:24.344 test[337:403] 1578.000000

例如,您可以使用该信息相对于状态项定位窗口(如 Matt Gemmell 的 MAAttachedWindow)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

cocoa:我想知道状态栏图标在屏幕上的位置 的相关文章

随机推荐