博客首页 注册 建议与交流 排行榜 加入友情链接
推荐 投诉 搜索: 帮助

剑心通明的资料库

文章均为转载,本人不负因参考它所导致的一切后果,请谨慎参考!如您的文章不愿被转载,请点击此处联系本人!
  jxtm.cublog.cn

关于作者
姓名:剑心通明
职业:高级工程师(专修灵魂^_^)
年龄:20出头30不到
位置:网络上一节点
个性介绍:努力学习每一天!
倾心打造:http://www.bsdlover.cn
http://bbs.bsdlover.cn
BSD爱好者的乐园!
|| << >> ||
我的分类


MAC OS X Cocoa NSToolbar 的使用

//向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

 原文地址 http://school.21tx.com/2005/04/10/18036.html
发表于: 2008-05-10,修改于: 2008-05-10 09:35,已浏览373次,有评论0条 推荐 投诉


网友评论
 发表评论