objective-c – NSStatusItem右键菜单

objective-c – NSStatusItem右键菜单,第1张

概述我正在开发一个左右键单击的状态栏应用程序.我通过关注其他帖子的提示开始了这项工作,但我不确定如何在右键单击显示菜单. 我使用子类NSView作为我的NSStatusItem的自定义视图,并且右键和左键单击执行不同的函数: - (void)mouseDown:(NSEvent *)theEvent{ [super mouseDown:theEvent]; if ([theEvent 我正在开发一个左右键单击的状态栏应用程序.我通过关注其他帖子的提示开始了这项工作,但我不确定如何在右键单击显示菜单.

我使用子类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在左键单击时的 *** 作方式相同?

解决方法 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
总结

以上是内存溢出为你收集整理的objective-c – NSStatusItem右键菜单全部内容,希望文章能够帮你解决objective-c – NSStatusItem右键菜单所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1008358.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存