尝试构建一个状态菜单程序,根据是左键单击还是右键单击/ ctrl单击,打开一个或另一个窗口.这是我所拥有的,它只适用于左键单击(显然):
-(voID) awakeFromNib{ NSBundle *bundle = [NSbundle mainBundle]; statusItem = [[Nsstatusbar systemStatusbar] statusItemWithLength:NSSquareStatusItemLength] retain]; [statusImage = [[NSImage alloc] initWithContentsOffile:[bundle pathForResource:@"icon" ofType:@"png"]]; [statusItem setimage:statusImage]; [statusItem settooltip:@"Program name"]; [statusItem setHighlightmode:YES]; [statusItem setAction:@selector(openWin:)]; [statusItem setTarget: self];}-(voID)openWin:(ID)sender{ [self openLeftwindow:sender];}-(IBAction)openLeftwindow:(ID)sender{ //Code to populate left Click Window [leftwindow makeKeyAndorderFront:nil];}-(IBAction)openRightwindow:(ID)sender{ //Code to populate Right Click Window [rightwindow makeKeyAndorderFront:nil];}
在我看来,解决方案将是openWin()函数中的if语句,以确定哪个按钮被点击(或者如果ctrl被按下),则运行相应的代码或使菜单意识到左右两个点击.但是,当我试图这样做时,这些都不会奏效.
提前致谢.
解决方法 如果您对检测到控制单击感到满意,而不是右键单击,那么第一个代码块将会执行您想要的 *** 作.如果您真的需要右键单击检测,则必须在NsstatusItem中使用自定义视图而不是图像,第二个代码块将会起作用.简单方法:
- (voID)openWin:(ID)sender { NSEvent *event = [NSApp currentEvent]; if([event modifIErFlags] & NSControlKeyMask) { [self openRightwindow:nil]; } else { [self openLeftwindow:nil]; }}
自定义视图方法:
- (voID)awakeFromNib { ... statusImage = ... MyVIEw *vIEw = [MyVIEw new]; vIEw.image = statusImage; [statusItem setVIEw:vIEw]; [statusItem settooltip:@"Program name"]; vIEw target = self; vIEw action = @selector(openLeftwindow:); vIEw rightAction = @selector(openRightwindow:); [vIEw release]; //[statusImage release]; //If you are not using it anymore,you should release it.}MyVIEw.h#import <Cocoa/Cocoa.h>@interface MyVIEw : NSControl { NSImage *image; ID target; SEL action,rightAction;}@property (retain) NSImage *image;@property (assign) ID target;@property (assign) SEL action,rightAction;@endMyVIEw.m#import "MyVIEw.h"@implementation MyVIEw@synthesize image,target,action,rightAction;- (voID)mouseUp:(NSEvent *)event { if([event modifIErFlags] & NSControlKeyMask) { [NSApp sendAction:self.rightAction to:self.target from:self]; } else { [NSApp sendAction:self.action to:self.target from:self]; }}- (voID)rightmouseUp:(NSEvent *)event { [NSApp sendAction:self.rightAction to:self.target from:self];}- (voID)dealloc { self.image = nil; [super dealloc];}- (voID)drawRect:(NSRect)rect { [self.image drawInRect:self.bounds fromrect:NSZeroRect operation:NSCompositeSourceOver fraction:1];}@end总结
以上是内存溢出为你收集整理的可可:右键单击NSStatusItem全部内容,希望文章能够帮你解决可可:右键单击NSStatusItem所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)