У меня есть Прозрачный NSWindow
с простым значком в нем, который можно перетаскивать по экрану.
Мой код:
.h:Как отличить событие mouseDown от mouseDragged в Transparent NSWindow
@interface CustomView : NSWindow{
}
@property (assign) NSPoint initialLocation;
.m
@synthesize initialLocation;
- (id) initWithContentRect: (NSRect) contentRect
styleMask: (NSUInteger) aStyle
backing: (NSBackingStoreType) bufferingType
defer: (BOOL) flag{
if (![super initWithContentRect: contentRect
styleMask: NSBorderlessWindowMask
backing: bufferingType
defer: flag]) return nil;
[self setBackgroundColor: [NSColor clearColor]];
[self setOpaque:NO];
[NSApp activateIgnoringOtherApps:YES];
return self;
}
- (void)mouseDragged:(NSEvent *)theEvent {
NSRect screenVisibleFrame = [[NSScreen mainScreen] visibleFrame];
NSRect windowFrame = [self frame];
NSPoint newOrigin = windowFrame.origin;
// Get the mouse location in window coordinates.
NSPoint currentLocation = [theEvent locationInWindow];
// Update the origin with the difference between the new mouse location and the old mouse location.
newOrigin.x += (currentLocation.x - initialLocation.x);
newOrigin.y += (currentLocation.y - initialLocation.y);
// Don't let window get dragged up under the menu bar
if ((newOrigin.y + windowFrame.size.height) > (screenVisibleFrame.origin.y + screenVisibleFrame.size.height)) {
newOrigin.y = screenVisibleFrame.origin.y + (screenVisibleFrame.size.height - windowFrame.size.height);
}
// Move the window to the new location
[self setFrameOrigin:newOrigin];
}
- (void)mouseDown:(NSEvent *)theEvent {
// Get the mouse location in window coordinates.
self.initialLocation = [theEvent locationInWindow];
}
Я хочу, чтобы отобразить NSPopover
когда пользователи нажимает на изображение прозрачного окна. Но, как вы видите в коде, событие mouseDown
используется для получения местоположения мыши (приведенный выше код был взят из примера).
Что я могу узнать, когда пользователь нажимает значок, чтобы перетащить его или просто щелкнул по нему, чтобы отобразить NSPopover
?
Спасибо
Очень жаль, что это так больно реализовать. Но спасибо за эту идею. – rocky