You need to log in to create posts and topics.
ImageButton
1,678 Posts
#1 · September 9, 2011, 3:22 pm
Quote from Forum Archives on September 9, 2011, 3:22 pmPosted by: svanvoorst <svanvoorst@...>
Working with Ken's demo caused me to think about how to place an image on an NSButton. Place a .png image in the same folder as this demo and re-name it to match the code below (or keep the name and change the code). The demo should place a scaled down image on a generic NSButton (rectangular).'------ start-----compile as "Objective-C"if system(_sysVers) < 1060 then stop "Requires OS 10.6 or newer"include resources "myImage.png"BeginCCode{NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];[NSApplication sharedApplication];[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];// --- Menu bar ---- //id menubar = [[NSMenu new] autorelease];id appMenuItem = [[NSMenuItem new] autorelease];[menubar addItem:appMenuItem];[NSApp setMainMenu:menubar];// --- App menu ---- //id appMenu = [[NSMenu new] autorelease];id quitMenuItem = [[[NSMenuItem alloc] initWithTitle:@"Quit"action:@selector(terminate:) keyEquivalent:@"q"] autorelease];[appMenu addItem:quitMenuItem];[appMenuItem setSubmenu:appMenu];// --- Window ---- //#define _wndW 430#define _wndH 150id window = [ [ NSWindow alloc ]initWithContentRect:NSMakeRect( 0, 0, _wndW, _wndH )styleMask: NSClosableWindowMask | NSTitledWindowMaskbacking:NSBackingStoreBuffereddefer:NO];[window center];[window setTitle: @"Button Image_demo" ];[window makeKeyAndOrderFront:nil];//--- Image button ----- //NSButton *imageBtn = [[NSButton alloc]initWithFrame: NSMakeRect( 100, _wndH - 80, 150, 30 )];[imageBtn setTitle:@"BtnTitle"];NSImage *image = [NSImage imageNamed:@"myImage.png"];[image setSize:NSMakeSize( 24.0, 24.0 )];[imageBtn setImage:image];[imageBtn setImagePosition:NSImageRight];[[window contentView] addSubview: imageBtn];[imageBtn release];// ---- Quit ----- //NSButton *quitBtn = [[NSButton alloc]initWithFrame:NSMakeRect( _wndW - 130, 20, 95, 30 )];[quitBtn setBezelStyle:NSRoundedBezelStyle ];[quitBtn setTitle: @"Quit" ];[quitBtn setAction:@selector(terminate:)];[[window contentView] addSubview: quitBtn];[quitBtn release];[NSApp activateIgnoringOtherApps:YES];[NSApp run];[pool drain];}EndC
'----- end -----Steve Van Voorst
To unsubscribe, e-mail: fbcocoa-unsubscribe@welovegod.org
For additional commands, e-mail: fbcocoa-help@freegroups.net
Posted by: svanvoorst <svanvoorst@...>
Working with Ken's demo caused me to think about how to place an image on an NSButton. Place a .png image in the same folder as this demo and re-name it to match the code below (or keep the name and change the code). The demo should place a scaled down image on a generic NSButton (rectangular).
'------ start-----
compile as "Objective-C"
if system(_sysVers) < 1060 then stop "Requires OS 10.6 or newer"
include resources "myImage.png"
BeginCCode
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
[NSApplication sharedApplication];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
// --- Menu bar ---- //
id menubar = [[NSMenu new] autorelease];
id appMenuItem = [[NSMenuItem new] autorelease];
[menubar addItem:appMenuItem];
[NSApp setMainMenu:menubar];
// --- App menu ---- //
id appMenu = [[NSMenu new] autorelease];
id quitMenuItem = [[[NSMenuItem alloc] initWithTitle:@"Quit"action:
@selector(terminate:) keyEquivalent:@"q"] autorelease];
[appMenu addItem:quitMenuItem];
[appMenuItem setSubmenu:appMenu];
// --- Window ---- //
#define _wndW 430
#define _wndH 150
id window = [ [ NSWindow alloc ]
initWithContentRect:NSMakeRect( 0, 0, _wndW, _wndH )
styleMask: NSClosableWindowMask | NSTitledWindowMask
backing:NSBackingStoreBuffered
defer:NO];
[window center];
[window setTitle: @"Button Image_demo" ];
[window makeKeyAndOrderFront:nil];
//--- Image button ----- //
NSButton *imageBtn = [[NSButton alloc]initWithFrame: NSMakeRect( 100, _wndH - 80, 150, 30 )];
[imageBtn setTitle:@"BtnTitle"];
NSImage *image = [NSImage imageNamed:@"myImage.png"];
[image setSize:NSMakeSize( 24.0, 24.0 )];
[imageBtn setImage:image];
[imageBtn setImagePosition:NSImageRight];
[[window contentView] addSubview: imageBtn];
[imageBtn release];
// ---- Quit ----- //
NSButton *quitBtn = [[NSButton alloc]initWithFrame:
NSMakeRect( _wndW - 130, 20, 95, 30 )];
[quitBtn setBezelStyle:NSRoundedBezelStyle ];
[quitBtn setTitle: @"Quit" ];
[quitBtn setAction:@selector(terminate:)];
[[window contentView] addSubview: quitBtn];
[quitBtn release];
[NSApp activateIgnoringOtherApps:YES];
[NSApp run];
[pool drain];
}
EndC
'----- end -----
Steve Van Voorst
To unsubscribe, e-mail: fbcocoa-unsubscribe@welovegod.org
For additional commands, e-mail: fbcocoa-help@freegroups.net
Click for thumbs down.0Click for thumbs up.0