我已经找到了我原来问题的答案。但是它有问题,我认为
_NSGetCarbonMenu()
-
问题:如何绘制菜单
使其看起来像本地菜单
物品?
-
自定义视图的行为与普通视图相同
向下按两次箭头键以获得
已选择下一项。
如何解决这些问题?
@interface MyMenuItem : NSView {
BOOL m_active;
}
@end
@implementation MyMenuItem
- (BOOL)acceptsFirstResponder { return YES; }
- (BOOL)becomeFirstResponder { m_active = YES; return YES; }
- (BOOL)resignFirstResponder { m_active = NO; return YES; }
- (void)viewDidMoveToWindow { [[self window] makeFirstResponder:self]; }
- (void)drawRect:(NSRect)rect {
if(m_active) {
[[NSColor blueColor] set];
} else {
[[NSColor blackColor] set];
}
NSRectFill(rect);
}
@end
// this makes sure the first item gets selected when the menu popups
MyMenuItem* view = [[[MyMenuItem alloc] initWithFrame:NSMakeRect(0, 0, 100, 20)] autorelease];
[view setAutoresizingMask:NSViewWidthSizable];
NSMenuItem* item = [menu itemAtIndex:0];
[item setView:view];
[NSMenu popUpContextMenu:menu withEvent:event forView:self];
// simulate a key press of the arrow-down key
CGKeyCode key_code = 125; // kVK_DownArrow = 125
CGEventRef event1, event2;
event1 = CGEventCreateKeyboardEvent(NULL, key_code, YES);
event2 = CGEventCreateKeyboardEvent(NULL, key_code, NO);
CGEventPost(kCGSessionEventTap, event1);
CGEventPost(kCGSessionEventTap, event2);
CFRelease(event1);
CFRelease(event2);
[NSMenu popUpContextMenu:menu withEvent:event forView:self];