NSMenu 不开始跟踪
我有一个小可可应用程序,通常在后台运行(作为代理)。有时我希望能够弹出一个上下文菜单(此时没有窗口或其他可见的东西)。
因为我只针对雪豹,所以我尝试了以下操作:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑窗口系统不认为您的应用程序处于活动状态,因此不会将鼠标事件发送到您创建的菜单。
作为实验,尝试在弹出菜单之前创建一个虚拟窗口。我将创建一个 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 styleNSNonActivatingPanelMask
.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.