NSMenu 不开始跟踪

发布于 2024-09-01 12:52:05 字数 1161 浏览 6 评论 0原文

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

因为我只针对雪豹,所以我尝试了以下操作:

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 {

}

知道我做错了什么吗?

I have a little cocoa app which usually operates in the background (as agent). Sometimes I'd like to be able to popup a contextmenu (no window or s.th. visible at this time).

As I'm only targetting Snow Leopard I tried this:

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];

 }

The menu popsup perfectly but if I hover the items with the mouse cursor they don't highlight and I can't click them.

The menuItemSelected: method looks just like this:

-(IBAction)menuItemSelected:(id)sender {

}

Any idea what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听风吹 2024-09-08 12:52:05

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

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

如果这有效,我会坚持这种方法并隐藏窗口。

I suspect that the windowing system doesn't consider your application to be active, and thus doesn't send mouse events to the menu you've created.

As an experiment, try creating a dummy window before popping up the menu. I'd create an NSPanel, possibly with style NSNonActivatingPanelMask. makeKeyAndOrderFront: your window/panel, then pop up the menu and see what happens.

If this works, I'd stick with the approach and hide the window.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文