1

单击 NSStatusItem 时,我会弹出一个自定义窗口。该代码基于MAAtachedwindow。一切都很好,但是当用户单击其他状态栏项目或其他应用程序等其他内容时,我无法找到关闭窗口的方法。

这是我创建窗口的代码:

    statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:width] retain];

    //setup custom status menu view
    CGFloat height = [[NSStatusBar systemStatusBar] thickness];
    NSRect viewFrame = NSMakeRect(0.0f, 0.0f, width, height);
    statusMenuView = [[[_ISStatusMenuView alloc] initWithFrame:viewFrame] retain];
    statusMenuView.offset = aOffset;
    statusItem.view = statusMenuView;

    //setup the window to show when clicked
    NSRect contentRect = NSZeroRect;
    contentRect.size = aView.frame.size;
    statusMenuWindow = [[[NSWindow alloc] initWithContentRect:contentRect
                                   styleMask:NSBorderlessWindowMask
                                     backing:NSBackingStoreBuffered 
                                       defer:NO] retain];

    [statusMenuWindow setLevel:NSPopUpMenuWindowLevel];
    [statusMenuWindow setBackgroundColor:[NSColor clearColor]];
    [statusMenuWindow setMovableByWindowBackground:NO];
    [statusMenuWindow setExcludedFromWindowsMenu:YES];
    [statusMenuWindow setOpaque:NO];
    [statusMenuWindow setHasShadow:NO];
    [statusMenuWindow useOptimizedDrawing:YES];
    [[statusMenuWindow contentView] addSubview:aView];
    [statusMenuWindow setDelegate:self];

    statusMenuView.statusMenuWindow = statusMenuWindow;

这是我显示窗口的方式:

- (void)centerView{
    NSRect menuFrame = self.window.frame;
    NSRect windowFrame = self.statusMenuWindow.frame;
    NSPoint menuPoint = NSMakePoint(NSMidX(menuFrame), NSMinY(menuFrame));
    menuPoint.x -= windowFrame.size.width*0.5f;
    menuPoint.y -= windowFrame.size.height+self.offset;
    [self.statusMenuWindow setFrameOrigin:menuPoint];
    [self.statusMenuWindow makeKeyAndOrderFront:self];
}

我希望windowDidResignKey委托方法可以解决问题,但这种配置并不能解决问题。代表正在工作,因为windowDidMove确实在运行。

- (void)windowDidResignKey:(NSNotification *)notification{
    NSLog(@"windowDidResignKey");
    [statusMenuView hideView];
}

- (void)windowDidResignMain:(NSNotification *)notification{
    NSLog(@"windowDidResignMain");
}

- (void)windowDidMove:(NSNotification *)notification{
    NSLog(@"windowDidMove");
}

所以回顾一下,当用户点击其他任何东西时,我如何隐藏我的自定义窗口,标准状态栏菜​​单的工作方式?

编辑 查看Popup示例后,我唯一缺少的是我必须继承 NSPanel 并使其成为关键窗口。

@interface Panel : NSPanel
@end

@implementation Panel

- (BOOL)canBecomeKeyWindow{
    return YES;
}

@end
4

1 回答 1

4

您需要确保您的窗口可以成为键窗口,并在它退出键时调用您的窗口的 orderOut: 方法。您应该使用自定义 NSWindowController,如果您还没有,在这种情况下,您只需调用它的“关闭”方法来关闭您的窗口。

我建议您不要发布一堆代码,而是看看这个最近发布的将窗口附加到状态项的出色示例: Shpakovski Popup Window Example

于 2011-07-22T07:51:38.243 回答