3

我正在尝试创建一个在状态栏中具有 NSStatusItem 图标的 Mac 应用程序。状态栏图标应支持文件拖放,并且在单击时还必须显示菜单。

问题是我已经设法分别实现这两个功能并且很难将它们合并在一起。

我能够使用此链接创建状态栏应用程序:

http://cocoatutorial.grapewave.com/2010/01/creating-a-status-bar-application/

然后我能够使用以下链接在状态栏图标上实现拖放功能

使用 NSStatusItem 拖放

我面临的问题如下,为了进行拖放,我必须创建另一个视图,然后将该视图分配给 NSStatusItem ,如下所示:

NSStatusItem *statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];

ViewWithDragFunctionality* viewWithDrag = [[ViewWithDragFunctionality alloc] initWithFrame:NSMakeRect(0, 0, 24, 24)];
[statusItem.view addSubview:viewWithDrag];

由于这只是一个视图,显然它不会像 NSStatusItem 的默认视图那样运行,也不支持鼠标交互或其他任何东西。我设法通过将以下函数添加到 ViewWithDragFunctionality.m 找到解决方法

- (void)mouseDown:(NSEvent *)theEvent {
    NSLog(@"Status Bar Icon Clicked");
}

每当单击状态栏图标并且还检测到文件拖放时,都会调用该函数。

但是现在我一直在弄清楚如何在单击状态栏图标时显示菜单。

任何帮助都感激不尽。我正在为此制定解决方案,如果我先找到一些东西,我会在这里发布:)

问候

舒迈斯

4

1 回答 1

5

经过多天的尝试和尝试,寻找合适的教程,又摸不着头脑,我终于偶然发现了一个在 github 上慷慨地为公众托管的 imgur 应用程序代码库。

The code was hosted at gihthub by a user called ZBUC.

The code that helped me out is at the following repository location on github : https://github.com/zbuc/imgurBar

This is exactly what was needed, after studying how he/they did things in there and combining what I learned/found with the links mentioned in the question, I was able to create a Custom status menu item for my application, was able to get a proper menu to drop down as is the case with a default status menu item and was also able to add drag and drop functionality to my applications status menu item.

So now I have a Custom Status Menu for my application which works like a normal status menu and also supports drag and drop functionality.

I hope that the links in the question, along with the repository link posted above, are of help to everyone who needs what I did.

Thank You

Shumais

于 2013-02-27T11:53:28.443 回答