OS X应用在状态栏添加图标

直接上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
@interface Tray : NSObject <NSApplicationDelegate> {
NSStatusItem *trayItem;
}
@end

@implementation Tray

- (void)testAction:(id)sender;
{
NSLog(@"Hello World");
}

- (void)quitAction:(id)sender;
{
[NSApp terminate:sender];
}

- (void)applicationDidFinishLaunching:(NSNotification *)note;
{
NSZone *zone = [NSMenu menuZone];
NSMenu *menu = [[NSMenu allocWithZone:zone] init];
NSMenuItem *item;

item = [menu addItemWithTitle:@"Testing" action:@selector(testAction:) keyEquivalent:@""];
[item setTarget:self];

item = [menu addItemWithTitle:@"Quit" action:@selector(quitAction:) keyEquivalent:@""];
[item setTarget:self];

trayItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
[trayItem setMenu:menu];
[trayItem setHighlightMode:YES];
[trayItem setTitle:@"HERE"];
}

@end