2013-07-12 13 views
5

Ecco la cosa:Cambiare colore di sfondo al passaggio del mouse su un NSMenuItem con l'abitudine NSView

ho creato una consuetudine NSMenuItem con un costume NSView in esso.

Tutto funziona bene, tranne che non riesco a ottenere il NSMenuItem per essere evidenziato (= cambiare il colore di sfondo al passaggio del mouse).

Sto provando a farlo all'interno del metodo drawRect, come mostrato in altre risposte pubblicate qui.

Cosa sto sbagliando?


Il NSView sottoclasse:

@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 

Il NSMenuItem sottoclasse:
(aggiungo subviews sulla visualizzazione personalizzata qui in modo che io possa avere accesso ai comandi attraverso l'istanza 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 

E questo è il Delegato 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 

Questo è ciò che ottengo:

NSMenuItem Issue Screenschot

risposta

0

OK Credo di aver capito.
Ho aggiunto una variabile bool pubblica nella sottoclasse NSView.
Poi i utilizzata

-(void)mouseEntered:(NSEvent *)theEvent 

e

-(void)mouseExited:(NSEvent *)theEvent 

per impostare la variabile di YES o NO. Dopo aver impostato la variabile ho usato

[self setNeedsDisplay:YES] 

chiamare

-(void) drawRect: (NSRect) rect 

è così che ho capito di lavoro :)

3

Non c'è bisogno di aggiungere booleani o qualsiasi altra cosa, è possibile farlo all'interno del tuo custom NSView che è collegato al tuo 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);} 
Problemi correlati