Profile image for Moriyosh Koizumi moriyoshi
TransformProcessType() did the trick! I referred to the code in GTK+.
Language
Objective-C
Tags
appkit cocoa corefoundation objective-c

Building a GUI without InterfaceBuilder in Objective-C

1 #import <Carbon/Carbon.h> 2 #import <AppKit/AppKit.h> 3 4 @interface MyApplicationDelegate: NSObject 5 { 6 NSWindow *window; 7 double e; 8 long aho; 9 } 10 -(void)applicationWillFinishLaunching: (NSNotification*) aNotification; 11 -(void)applicationDidFinishLaunching: (NSNotification*) aNotification; 12 -(BOOL)applicationShouldTerminateAfterLastWindowClosed: (NSApplication *)theApplication; 13 @end 14 15 @implementation MyApplicationDelegate 16 -(void)applicationWillFinishLaunching: (NSNotification*) aNotification 17 { 18 window = [[NSWindow alloc] initWithContentRect: 19 NSMakeRect(300, 800, 500, 500) 20 styleMask: (NSTitledWindowMask | NSClosableWindowMask) 21 backing: NSBackingStoreBuffered 22 defer: NO]; 23 [window setTitle: @"hoge"]; 24 [window makeKeyAndOrderFront: window]; 25 [window makeMainWindow]; 26 27 NSButton *btn = [[NSButton alloc] initWithFrame: NSMakeRect(10, 10, 120, 24)]; 28 // [btn setBezelStyle: NSRoundedBezelStyle]; 29 [btn setBezelStyle: NSRegularSquareBezelStyle]; 30 [[window contentView] addSubview: btn]; 31 [btn release]; 32 } 33 34 -(void)applicationDidFinishLaunching: (NSNotification*) aNotification 35 { 36 } 37 38 -(BOOL)applicationShouldTerminateAfterLastWindowClosed: (NSApplication *)theApplication 39 { 40 return YES; 41 } 42 43 -(void)dealloc 44 { 45 [window release]; 46 [super dealloc]; 47 } 48 @end 49 50 int main(int argc, char **argv) 51 { 52 static const ProcessSerialNumber thePSN = { 0, kCurrentProcess }; 53 TransformProcessType(&thePSN, kProcessTransformToForegroundApplication); 54 SetFrontProcess(&thePSN); 55 NSAutoreleasePool *aPool = [[NSAutoreleasePool alloc] init]; 56 [NSApplication sharedApplication]; 57 MyApplicationDelegate *aMyApplicationDelegate = [[MyApplicationDelegate alloc] init]; 58 [NSApp setDelegate: aMyApplicationDelegate]; 59 [aPool release]; 60 [NSApp run]; 61 return 0; 62 }

Discussion

gcc test.m -framework AppKit

Comments