【问题标题】:NSMenu doesn't start trackingNSMenu 不开始跟踪
【发布时间】:2010-05-16 11:04:48
【问题描述】:

我有一个小可可应用程序,通常在后台运行(作为代理)。有时我希望能够弹出一个上下文菜单(此时没有可见的窗口或其他东西)。

因为我只针对雪豹,所以我尝试了这个:

if (windows) {
       NSMenu *theMenu = [[[NSMenu alloc] initWithTitle:@"test"] autorelease];
       [theMenu setShowsStateColumn:NO];
       [theMenu setAutoenablesItems:NO];

           for (id item in windows) {

               NSString *labelText = @"some text";

               NSMenuItem *theMenuItem = [[[NSMenuItem alloc] initWithTitle:labelText
                                             action:@selector(menuItemSelected:)
                                               keyEquivalent:@""] autorelease]; 

               [theMenuItem setTarget:self];
               [theMenuItem setRepresentedObject:item];
               [theMenuItem setEnabled:YES];
               [theMenuItem setImage:icon];
               [theMenu addItem:theMenuItem];
           }

       [theMenu popUpMenuPositioningItem:nil atLocation:[NSEvent mouseLocation] inView:nil];

 }

菜单完美弹出,但如果我将鼠标光标悬停在项目上,它们不会突出显示并且我无法单击它们。

menuItemSelected: 方法如下所示:

-(IBAction)menuItemSelected:(id)sender {

}

知道我做错了什么吗?

【问题讨论】:

    标签: cocoa macos contextmenu tracking nsmenu


    【解决方案1】:

    我怀疑窗口系统不认为您的应用程序处于活动状态,因此不会将鼠标事件发送到您创建的菜单。

    作为一个实验,在弹出菜单之前尝试创建一个虚拟窗口。我会创建一个NSPanel,可能具有NSNonActivatingPanelMask 的样式。 makeKeyAndOrderFront:你的窗口/面板,然后弹出菜单看看会发生什么。

    如果可行,我会坚持使用该方法并隐藏窗口。

    【讨论】:

    • 谢谢!我会试试的。现在我创建了一个外观和行为类似于 nsmenu 的窗口......我已经尝试创建一个非激活的 NSPanel,但我想我可能错过了 makeKeyAndOrderFront.. 如果我没记错的话,我只使用了 orderFront。如果您的解决方案很快奏效,我会报告。
    • 不幸的是,这似乎也不起作用:-(如果我将 NSButton 添加到窗口,将我的菜单设置为按钮的菜单,然后右键单击该按钮(然后调用 menuForEvent )但我认为我也不能以编程方式做到这一点(没有用户右键单击按钮......可以在按钮上发送鼠标事件,但这真的很难看)
    猜你喜欢
    • 1970-01-01
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    • 2021-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多