我正在开发一个左右键单击的状态栏应用程序.我通过关注其他帖子的提示开始了这项工作,但我不确定如何在右键单击显示菜单.
我使用子类NSView作为我的NsstatusItem的自定义视图,并且右键和左键单击执行不同的函数:
- (void)mouseDown:(NSEvent *)theEvent{ [super mouseDown:theEvent]; if ([theEvent modifierFlags] & NSCommandKeyMask){ [self.target performSelectorOnMainThread:self.rightAction withObject:nil waitUntilDone:NO]; }else{ [self.target performSelectorOnMainThread:self.action withObject:nil waitUntilDone:NO]; } } - (void)rightMouseDown:(NSEvent *)theEvent{ [super rightMouseDown:theEvent]; [self.target performSelectorOnMainThread:self.rightAction withObject:nil waitUntilDone:NO]; }
解决方法
NsstatusItem popUpStatusItemmenu:诀窍.我从右键单击操作调用它并传入我要显示的菜单并显示它!这不是我期望的这个功能,但它正在发挥作用.
这是我的代码的重要部分:
- (void)showMenu{ // check if we are showing the highlighted state of the custom status item view if(self.statusItemView.clicked){ // show the right click menu [self.statusItem popUpStatusItemmenu:self.rightClickMenu]; } } // menu delegate method to unhighlight the custom status bar item view - (void)menuDidClose:(NSMenu *)menu{ [self.statusItemView setHighlightState:NO]; } - (void)applicationDidFinishLaunching:(NSNotification *)aNotification{ // setup custom view that implements mouseDown: and rightMouseDown: self.statusItemView = [[IsstatusItemView alloc] init]; self.statusItemView.image = [NSImage imageNamed:@"menu.png"]; self.statusItemView.alternateImage = [NSImage imageNamed:@"menu_alt.png"]; self.statusItemView.target = self; self.statusItemView.action = @selector(mainAction); self.statusItemView.rightAction = @selector(showMenu); // set menu delegate [self.rightClickMenu setDelegate:self]; // use the custom view in the status bar item self.statusItem = [[NsstatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength]; [self.statusItem setView:self.statusItemView]; }
以下是自定义视图的实现:
@implementation IsstatusItemView @synthesize image = _image; @synthesize alternateImage = _alternateImage; @synthesize clicked = _clicked; @synthesize action = _action; @synthesize rightAction = _rightAction; @synthesize target = _target; - (void)setHighlightState:(BOOL)state{ if(self.clicked != state){ self.clicked = state; [self setNeedsdisplay:YES]; } } - (void)drawImage:(NSImage *)aimage centeredInRect:(NSRect)aRect{ NSRect imageRect = NSMakeRect((CGFloat)round(aRect.size.width*0.5f-aimage.size.width*0.5f),(CGFloat)round(aRect.size.height*0.5f-aimage.size.height*0.5f),aimage.size.width,aimage.size.height); [aimage drawInRect:imageRect fromrect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0f]; } - (void)drawRect:(NSRect)rect{ if(self.clicked){ [[NSColor selectedMenuItemColor] set]; NSRectFill(rect); if(self.alternateImage){ [self drawImage:self.alternateImage centeredInRect:rect]; }else if(self.image){ [self drawImage:self.image centeredInRect:rect]; } }else if(self.image){ [self drawImage:self.image centeredInRect:rect]; } } - (void)mouseDown:(NSEvent *)theEvent{ [super mouseDown:theEvent]; [self setHighlightState:!self.clicked]; if ([theEvent modifierFlags] & NSCommandKeyMask){ [self.target performSelectorOnMainThread:self.rightAction withObject:nil waitUntilDone:NO]; }else{ [self.target performSelectorOnMainThread:self.action withObject:nil waitUntilDone:NO]; } } - (void)rightMouseDown:(NSEvent *)theEvent{ [super rightMouseDown:theEvent]; [self setHighlightState:!self.clicked]; [self.target performSelectorOnMainThread:self.rightAction withObject:nil waitUntilDone:NO]; } - (void)dealloc{ self.target = nil; self.action = nil; self.rightAction = nil; [super dealloc]; } @end
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。