2013-07-12 25 views
5

Dưới đây là điều:Thay đổi màu nền trên di chuột trên một NSMenuItem với tùy chỉnh NSView

Tôi đã tạo ra một phong tục NSMenuItem với một tuỳ chỉnh NSView trong đó.

Mọi thứ hoạt động tốt trừ khi tôi không thể nhận được NSMenuItem để được đánh dấu (= thay đổi màu nền khi di chuột qua).

Tôi đang cố gắng làm điều đó bên trong phương thức drawRect, như được hiển thị trong các câu trả lời khác được đăng tại đây.

Tôi đang làm gì sai?


Các NSView lớp con:

@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 

Các NSMenuItem lớp con:
(tôi thêm subviews trên giao diện tùy chỉnh ở đây vì vậy tôi có thể có quyền truy cập vào các điều khiển thông qua các NSMenuItem dụ)

@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 

Và đây là App đại biểu:

@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 

Đây là những gì tôi nhận được:

NSMenuItem Issue Screenschot

Trả lời

0

OK tôi nghĩ rằng tôi đã nhận nó.
Tôi đã thêm biến công khai trong lớp con NSView.
Sau đó tôi được sử dụng

-(void)mouseEntered:(NSEvent *)theEvent 

-(void)mouseExited:(NSEvent *)theEvent 

để thiết lập các biến để YES hoặc NO. Sau khi thiết lập các biến i sử dụng

[self setNeedsDisplay:YES] 

gọi

-(void) drawRect: (NSRect) rect 

Đó là làm thế nào tôi nhận nó làm việc :)

3

Không cần phải thêm Booleans hoặc bất cứ điều gì khác, bạn có thể làm điều đó từ bên trong của bạn tùy chỉnh NSView được đính kèm vào 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);} 
của bạn