游戏梦网
当前位置:IT学院 >> 操作系统 >> 苹果OS >> 正文

MAC OS X Cocoa NSToolbar 的使用

中关村商情网  2007-8-24  责编:soldier  字体:
  

//向NSToolbar控件添加自定义的NSToolbarItem项
- (NSToolbarItem *) toolbar: (NSToolbar *)toolbar itemForItemIdentifIEr:
(NSString *) itemIdent willBeInsertedIntoToolbar:(BOOL) willBeInserted {
// Required delegate method Given an item identifier, self method
returns an item
// The toolbar will use self method to obtain toolbar items that can
be displayed in the customization sheet, or in the toolbar itself
NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc]
initWithItemIdentifier: itemIdent] autorelease];

if ([itemIdent isEqual: SquareDotViewItemIdentifier]) {
SquareDotView *dv = [[SquareDotView alloc] initWithFrame:
NSMakeRect(0,0,100,32)];

[toolbarItem setView: dv];

// set a reasonable minimum size
[toolbarItem setMinSize: NSMakeSize(100,32)];
// set a maximum size that allows us to stretch.
[toolbarItem setMaxSize: NSMakeSize(300,32)];

[toolbarItem setLabel: @"Dot View"];
[toolbarItem setPaletteLabel: @"A Dot View"];
[toolbarItem setToolTip: @"This is a dot view"];
}
.....
}

//返回单击后能被选中且为高亮显示的NSToolbarItem的集合。
- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
{// return an array of all the items that can be highlight display after selected
return [NSArray arrayWithObjects:
NSColorToolbarItemIdentifier,
NSFontToolbarItemIdentifier,nil];
}

//返回程序加载后的默认的NSToolbarItem的集合。
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
{ // return an array of the items found in the default toolbar
return [NSArray arrayWithObjects:
NSToolbarSeparatorItemIdentifier,
NSFontToolbarItemIdentifier,
NSToolbarCustomizeToolbarItemIdentifier,
nil];
}


//以下是作为NSToolbarItem出现的自定义类
The "SquareDotView" class:


@interface SquareDotView : NSView {
@private
float sdSide;
NSPoint sdLocation;
}
@end

@implementation SquareDotView

#define START_SIDE 10
#define START_LOCATION NSMakePoint(10,10)

- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
sdSide = START_SIDE;
sdLocation = START_LOCATION;
}
return self;
}

- (void)mouseDown:(NSEvent *)theEvent {
sdLocation = [self convertPoint: [theEvent locationInWindow]
fromView: nil];
[self setNeedsDisplay: YES];
}

- (void)drawRect:(NSRect)rect {
[[NSColor redColor] set];
[[NSBezierPath bezierPathWithRect: NSMakeRect(sdLocation.x,
sdLocation.y, sdSide, sdSide)] fill];

[[NSColor blackColor] set];
[[NSBezierPath bezierPathWithRect: [self bounds]] stroke];
}

- (id)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder: coder];
if (self) {
[coder decodeValuesOfObjCTypes: "fff", &sdSide, &sdLocation.x,
&sdLocation.y];
}
return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
[super encodeWithCoder: coder];
if (self) {
[coder encodeValuesOfObjCTypes: "fff", &sdSide, &sdLocation.x,
&sdLocation.y];
}
}

@end

【作者:soldier】 【来源:中关村商情网】
评论】【 】【去论坛看看】【收藏】【打印】【关闭】【回到顶部

我来说两句

优惠活动:买笔记本、台式机、服务器,拨打电话010-68400280/68400215,享受惊喜特价!

论坛·美图

相关链接
IT学院 最新文章

HD Tune硬盘软件

服务器负载均衡

Oracle数据库

ASP加密和解密
·Mac OS X下应用程序的安装与卸载
·成为系统专家的必修课
·X系统不能启动故障排除的DIY完全手册
·认识苹果电脑的必备常识
·苹果操作系统精选教程1
·苹果操作系统精选教程2
·苹果操作系统精选教程3
·苹果操作系统精选教程4
·苹果操作系统精选教程5
·苹果操作系统精选教程6
·苹果操作系统精选教程7
·MAC系统的一些常识
·APPLE Mac osx (提速 / 修改键盘设置 
·APPLE 全功略 (一)
·APPLE Mac OSX工具软件下载
·在PC上本地安装Macl OS X x86
·MAC X86 硬件支持列表
·xp 与osx 双启动
·在PC上安装Mac OSX x86
·Mac OSX建立分区和格式化分区
IT学院 热点文章

关于CZN | 用户注册 | 友情链接 | 内容合作 | 广告服务 | 免责条款 | 工作机会 | 我要投稿 | 产品评测 | 联系CZN | 网站地图 | 设为首页 | 加入收藏
服务热线:010-68400280 EMAIL: 您若发现页面中有任何错误或侵犯您的版权,请及时与我们联系
Copyright© 2005-2008 Czn.Com.Cn All Rights Reserved 京ICP证05009081号