Monday, August 8, 2011

Draggable transparent NSWindow - Objective C

Subclass NSWindow as follows to get a draggable transparent NSWindow.

Draggable transparent NSWindow interface

@interface AppWindow : NSWindow {
NSPoint currentLocation;
NSPoint newOrigin;
int offsetX,offsetY;


Draggable transparent NSWindow implementation

#import "AppWindow.h"

@implementation AppWindow

- (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];
[self setMovableByWindowBackground:TRUE];
[self setMovable:TRUE];

//this will make the app not respond to F11 (show desktop)
// [self setCollectionBehavior:NSWindowCollectionBehaviorStationary];
return self;

- (BOOL) canBecomeKeyWindow{
return YES;

- (void)mouseMoved:(NSEvent *)theEvent{

- (void)mouseDown:(NSEvent *)theEvent{

currentLocation = [self convertBaseToScreen:[self mouseLocationOutsideOfEventStream]];

offsetX = currentLocation.x - [self frame].origin.x;
offsetY = currentLocation.y - [self frame].origin.y;

- (void)mouseDragged:(NSEvent *)theEvent{
currentLocation = [self convertBaseToScreen:[self mouseLocationOutsideOfEventStream]];

newOrigin.x = currentLocation.x - offsetX;
newOrigin.y = currentLocation.y - offsetY;

[self setFrameOrigin:newOrigin];



You may also like

No comments: