参考链接

main.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "AppController.h"
#include <AppKit/AppKit.h>

int main(int argc, const char *argv[])
{
NSAutoreleasePool *pool;
AppController *delegate;

pool = [[NSAutoreleasePool alloc] init];
delegate = [[AppController alloc] init];

[NSApplication sharedApplication];
[NSApp setDelegate: delegate];

RELEASE(pool);
return NSApplicationMain (argc, argv);
}

AppController.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef _AppController_H_
#define _AppController_H_

#include <Foundation/NSObject.h>

@class NSWindow;
@class NSTextField;
@class NSNotification;

@interface AppController : NSObject
{
NSWindow *window;
NSTextField *label;
}

- (void)applicationWillFinishLaunching: (NSNotification *) not;
- (void)applicationDidFinishLaunching: (NSNotification *) not;

@end

#endif /* _AppController_H_ */

AppController.m

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "AppController.h"
#include <AppKit/AppKit.h>

@implementation AppController
- (void) applicationWillFinishLaunching: (NSNotification *) not
{
/* Create Menu */
NSMenu *menu;
NSMenu *info;

menu = [NSMenu new];
[menu addItemWithTitle: @"Info"
action: NULL
keyEquivalent: @""];
[menu addItemWithTitle: @"Hide"
action: @selector(hide:)
keyEquivalent: @"h"];
[menu addItemWithTitle: @"Quit"
action: @selector(terminate:)
keyEquivalent: @"q"];

info = [NSMenu new];
[info addItemWithTitle: @"Info Panel..."
action: @selector(orderFrontStandardInfoPanel:)
keyEquivalent: @""];
[info addItemWithTitle: @"Preferences"
action: NULL
keyEquivalent: @""];
[info addItemWithTitle: @"Help"
action: @selector (orderFrontHelpPanel:)
keyEquivalent: @"?"];

[menu setSubmenu: info
forItem: [menu itemWithTitle:@"Info"]];
RELEASE(info);

[NSApp setMainMenu:menu];
RELEASE(menu);

/* Create Window */
window = [[NSWindow alloc] initWithContentRect: NSMakeRect(300, 300, 200, 100)
styleMask: (NSTitledWindowMask |
NSMiniaturizableWindowMask |
NSResizableWindowMask)
backing: NSBackingStoreBuffered
defer: YES];
[window setTitle: @"Hello World"];

/* Create Label */
label = [[NSTextField alloc] initWithFrame: NSMakeRect(30, 30, 80, 30)];
[label setSelectable: NO];
[label setBezeled: NO];
[label setDrawsBackground: NO];
[label setStringValue: @"Hello World"];

[[window contentView] addSubview: label];
RELEASE(label);
}

- (void) applicationDidFinishLaunching: (NSNotification *) not
{
[window makeKeyAndOrderFront: self];
}

- (void) dealloc
{
RELEASE(window);
[super dealloc];
}

@end

HelloWorldInfo.plist

1
{
   ApplicationDescription = "Hello World Tutorial";
   ApplicationIcon = "";
   ApplicationName = HelloWorld;
   ApplicationRelease = 0.1;
   Authors = "";
   Copyright = "Copyright (C) 200x by ...";
   CopyrightDescription = "Released under...";
   FullVersionID = 0.1;
   URL = "";
}

GNUmakefile

1
2
3
4
5
6
7
8
9
10
#GNUSTEP_MAKEFILES=/usr/share/GNUstep/Makefiles
GNUSTEP_MAKEFILES=$(shell gnustep-config --variable=GNUSTEP_MAKEFILES)
include $(GNUSTEP_MAKEFILES)/common.make

APP_NAME = HelloWorld
HelloWorld_HEADERS = AppController.h
HelloWorld_OBJC_FILES = main.m AppController.m
HelloWorld_RESOURCE_FILES = HelloWorldInfo.plist

include $(GNUSTEP_MAKEFILES)/application.make

这里,需要使用gnustep-config –variable=GNUSTEP_MAKEFILES获取makefiles的路径

编译运行:

1
2
make
openapp ./HelloWorld.app

截图:

源代码下载: HelloWorld.zip

知识共享授权条款
本著作Chen, Zai-Chun制作,以知识共享 姓名标示-相同方式分享 4.0 国际 授权条款释出

留言

Dec 12 2011