Cocoa:右键单击NSStatusItem [英] Cocoa: Right Click NSStatusItem

查看:151
本文介绍了Cocoa:右键单击NSStatusItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个.Net开发人员,需要将一个小项目移植到Mac,所以我知道没有什么目标C.事实上,下面的代码只是一束抓住稻草和在黑暗中射击。 p>

尝试构建一个状态菜单程序,打开一个或另一个窗口,取决于它是左键单击还是右键单击/ ctrl +单击。这里是我有,它只适用于左键单击(显然):

   - (void)awakeFromNib {

NSBundle * bundle = [NSbundle mainBundle];

statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
[statusImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@iconofType:@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 {
//填充左键窗口的代码
[leftWindow makeKeyAndorderFront:nil];
}

- (IBAction)openRightWindow:(id)sender {
//填充右键窗口的代码
[rightWindow makeKeyAndorderFront:nil];
}



在我看来,解决方案将是一个if语句在openWin ()函数来确定点击哪个按钮(或者如果按住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;
[stateItem setView:view];
[statusItem setToolTip:@Program Name];
view target = self;
view action = @selector(openLeftWindow :);
view rightAction = @selector(openRightWindow :);
[查看版本];
// [statusImage release]; //如果你不再使用它,你应该释放它。
}

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;
@end

MyView.m

#importMyView.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


I am a .Net developer who need to port a small project into Mac, so I know next to nothing about Objective C. In fact the code below was just a bunch of grasping at straws and shooting in the dark.

Trying to build a Status Menu program that opens one or another window depending on if it is a left-click or a right-click/ctrl+click. Here is what I have, and it works for left-click only (obviously):

-(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];
}

It seems to me that the solution would be either an if statement in the openWin() function to determine which button is clicked (or if ctrl was held down) then run the appropriate code or to make the menu aware of both the left and right clicks. But neither of these have worked when I attempted to do so.

Thanks In Advance.

解决方案

If you will be satisfied with detecting control-click and not right click, then the first block of code will do what you want. If you really need the right click detection, you will have to use a custom view instead of an image in your NSStatusItem, and the second block of code will work.

Simple Method:

- (void)openWin:(id)sender {
    NSEvent *event = [NSApp currentEvent];
    if([event modifierFlags] & NSControlKeyMask) {
        [self openRightWindow:nil];
    } else {
        [self openLeftWindow:nil];
    }
}

Custom view method:

- (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;
@end

MyView.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

这篇关于Cocoa:右键单击NSStatusItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆