【问题标题】:cocoa:I want to know the location of the status bar icon on the screencocoa:我想知道状态栏图标在屏幕上的位置
【发布时间】:2012-04-18 02:27:21
【问题描述】:

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

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

【问题讨论】:

    标签: objective-c xcode macos cocoa


    【解决方案1】:

    您尚未为状态项设置自定义视图,因此调用 view 将返回 nil。

    我猜你想知道位置的原因是你点击状态项的时候。

    如果你要为你的状态项实现一个动作,它可能看起来像这样:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 1970-01-01
      • 2014-07-15
      相关资源
      最近更新 更多