2013-07-12 58 views
5

這裏是東西:更改背景顏色上NSMenuItem自定義的NSView

我創建了一個自定義NSMenuItem在一個自定義NSView

一切工作正常,但我不能讓NSMenuItem得到突出顯示(=鼠標懸停更改背景顏色)。

我試圖在drawRect方法內做到這一點,如在這裏發佈的其他答案所示。

我在做什麼錯?


NSView子類:

@interface customView : NSView 
@end 
@implementation customView 

- (id)initWithFrame:(NSRect)frame 
{ 

    NSRect theRect = NSMakeRect(0, 0, 200, 30); 
    self = [super initWithFrame:theRect]; 
    if (self) { 
    NSTrackingArea * trackingArea = [[NSTrackingArea alloc] initWithRect:theRect 
                options: (NSTrackingMouseEnteredAndExited | NSTrackingActiveInKeyWindow |NSTrackingActiveAlways) 
                 owner:self userInfo:nil]; 
     [self addTrackingArea:trackingArea]; 
    } 

    return self; 
} 

#define menuItem ([self enclosingMenuItem]) 

- (void) drawRect: (NSRect) rect { 


    BOOL isHighlighted = [menuItem isHighlighted]; 
    if (isHighlighted) { 
     //this nslog never happens 
     NSLog(@"it's highlighted"); 
} 



- (void)mouseUp:(NSEvent*) event { 
    NSMenuItem* mitem = [self enclosingMenuItem]; 
    NSMenu* m = [mitem menu]; 
    [m cancelTracking]; 


    NSLog(@"you clicked the %ld item",[m indexOfItem: mitem]); 
} 
@end 

NSMenuItem子類:
(我的自定義視圖中添加子視圖這裏,所以我可以有通過NSMenuItem實例訪問控制)

@interface customItem : NSMenuItem{ 

} 

-(void)setTheText:(NSString*)theString; 

@property NSTextField *theLabel; 
@end 
#import "customItem.h" 
#import "customView.h" 
@implementation customItem 
@synthesize theLabel; 
-(id)init{ 

    if (self){ 

     customView *cv = [[customView alloc] init]; 
     theLabel = [[NSTextField alloc] initWithFrame:NSMakeRect(10, 8, 130, 17)]; 
     [theLabel setEditable:NO]; 
     [theLabel setBordered:NO]; 
     NSButton *myButton = [[NSButton alloc] initWithFrame:NSMakeRect(170, 7, 20, 20)]; 
     NSButton *myButton1 = [[NSButton alloc] initWithFrame:NSMakeRect(150, 7, 20, 20)]; 

     [myButton setBezelStyle:NSCircularBezelStyle]; 
     [myButton1 setBezelStyle:NSCircularBezelStyle]; 

     [myButton setTitle:@""]; 
     [myButton1 setTitle:@""]; 
     [cv addSubview:myButton]; 
     [cv addSubview:myButton1]; 
     [cv addSubview:theLabel]; 
     [self setView:cv]; 
     [theLabel setStringValue:@"A Value "]; 

    } 

    return self; 
} 

-(void)setTheText:(NSString *)theString{ 

    [theLabel setStringValue:theString]; 
} 


@end 

這是在App代表:

@interface AppDelegate : NSObject <NSApplicationDelegate>{ 

    NSStatusItem *statusItem; 
    IBOutlet NSMenu *theMenu; 
} 

@property (assign) IBOutlet NSWindow *window; 

@end 
#import "customItem.h" 
@implementation AppDelegate 

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 

} 

- (void)awakeFromNib{ 

    statusItem = [[NSStatusBar systemStatusBar] 
        statusItemWithLength:NSSquareStatusItemLength]; 
    NSBundle *bundle = [NSBundle mainBundle]; 

    NSImage *statusImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"barIcon" ofType:@"png"]]; 
    NSImage *highlightImage = [[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"barIcon_H" ofType:@"png"]]; 

    [statusItem setImage:statusImage]; 
    [statusItem setAlternateImage:highlightImage]; 


    [statusItem setMenu:theMenu]; 

    [theMenu removeAllItems]; 
    customItem *mi = [[customItem alloc] init]; 

    [theMenu addItem:mi]; 
    customItem *mi2 = [[customItem alloc] init]; 
    [theMenu addItem:mi2]; 
} 
@end 

這就是我得到:

NSMenuItem Issue Screenschot

回答

0

OK我想我得到了它。
我在NSView子類中添加了一個公共布爾變量。
然後我用

-(void)mouseEntered:(NSEvent *)theEvent 

-(void)mouseExited:(NSEvent *)theEvent 

到變量設置爲YESNO。設置變量後,我用

[self setNeedsDisplay:YES] 

調用

-(void) drawRect: (NSRect) rect 

這就是我得到它的工作:)

3

無需添加布爾或其他任何東西,你可以從內部做你定製NSView這是附加到您的NSMenuItem

- (void)drawRect:(NSRect)rect { 

[super drawRect:rect]; 

//Handle the hightlight 
if ([[self enclosingMenuItem] isHighlighted]) 
{ 
    [self.lbl_title setTextColor:[NSColor whiteColor]]; 
    [self.lbl_amount setTextColor:[NSColor colorWithDeviceRed:151.0f/255.0f green:164.0f/255.0f blue:179.0f/255.0f alpha:1.0f]]; 
    [[NSColor selectedMenuItemColor] setFill]; 
} 
else 
{ 
    [self.lbl_title setTextColor:[NSColor blackColor]]; 
    [self.lbl_amount setTextColor:[NSColor whiteColor]]; 
    [[self backgroundColor] setFill]; 
} 
NSRectFill(rect);}