开发者

NSMenu's mouse over event

开发者 https://www.devze.com 2023-03-31 09:17 出处:网络
I used NSMenu and NSStatusItem to display custom menu on status bar for a long time in this way: statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:18] retain];

I used NSMenu and NSStatusItem to display custom menu on status bar for a long time in this way:

statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:18] retain]; [statusItem setImage:[NSImage imageNamed:@"myIcon"]]; [statusItem setMenu开发者_开发百科:myMenu]; [statusItem setHighlightMode:NO]; ...

It works fine by start clicking its image icon. But I noticed that apple's menu located on status bar can be triggered simple by mouse move over, like the airport menu, power menu, language menu and date/time menu. They are all auto pop up when you mouse walk over. How did they get it?

I have checked the "add tracking rect" for NSView with "mouseEntered" event, but things is not as difficult as that I think.

Any advice?


I think the statusItem is not a supported mouse event,but you can set a view to the statusItem and override the mouse even method to support the mouse event, just like this:

statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
  NSButton *nb = [[NSButton alloc] init]; 
  [nb setImage:image];
  [nb setAction:@selector(statusItemClick:)];  
  [_statusItem setView:nb];

here just an example to set a view to statusitem, if you want to support the mouse event , your view must extend same view and create NSTrackingArea and implement the moveMoved, mouseEntered and mouseExited methods (or whichever ones you want)

0

精彩评论

暂无评论...
验证码 换一张
取 消