Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa app Create transparent view on top of all mac running applications

Tags:

xcode

macos

cocoa

I am working on a mac osx application using Xcode. I would like to add a transparent full-screen view/window on top of all applications. So that I could 'draw' on the transparent view, and behind it will be whatever application, safari, word...etc.

I tried like the following

 NSRect rect = [[NSScreen mainScreen] frame];   //this is full screen size, but still with the status bar like time, battery, etc.

 NSWindow *overlayWindow = [[NSWindow alloc]initWithContentRect:rect
 styleMask:NSBorderlessWindowMask
 backing:NSBackingStoreBuffered
 defer:NO];
 overlayWindow.backgroundColor = [NSColor redColor];
 [self.window addChildWindow:overlayWindow ordered:NSWindowAbove];

It's a new full-screen child window of my mac-application. But it's not on top of all applications i am running on my mac.

So my question, How to add the view on top of my mac screen view(not only the top view of my application). Thanks so much!!!

like image 376
user2152814 Avatar asked May 27 '13 19:05

user2152814


People also ask

How do you make an app always on top on Mac?

Here, check that “Displays have separate Spaces” is active, then open some apps. With the toolbar of one app, hover over the green window button. You'll see a pop-up asking you to choose a tile format. Once you choose one, it will be replicated within your Spaces.

How do I change the opacity of a window on a Mac?

Change the appearance of the desktop on your Mac Make the desktop less transparent: Choose Apple menu > System Preferences, click Accessibility , click Display, click Display, then select “Reduce transparency.” The transparent areas of the desktop and app windows become gray.


2 Answers

See Apple's FunkyOverlayWindow sample code. In addition to setting the window's level, you will need to set its background color to clear and set it to non-opaque. If it's transparent but you still want it to receive mouse events for drawing (rather than letting them pass through to the windows behind it), you'll need to do [window setIgnoresMouseEvents:NO].

like image 128
Ken Thomases Avatar answered Sep 30 '22 07:09

Ken Thomases


This looks like what you want.

NSWindow has - (void)setLevel:(NSInteger)windowLevel

With this useful predefined levels. Pick one you like. Add or subtract 1 if you want it just above or just below one of these levels.

#define NSNormalWindowLevel          kCGNormalWindowLevel
#define NSFloatingWindowLevel        kCGFloatingWindowLevel
#define NSSubmenuWindowLevel         kCGTornOffMenuWindowLevel
#define NSTornOffMenuWindowLevel     kCGTornOffMenuWindowLevel
#define NSMainMenuWindowLevel        kCGMainMenuWindowLevel
#define NSStatusWindowLevel          kCGStatusWindowLevel
#define NSModalPanelWindowLevel      kCGModalPanelWindowLevel
#define NSPopUpMenuWindowLevel       kCGPopUpMenuWindowLevel
#define NSScreenSaverWindowLevel     kCGScreenSaverWindowLevel
#define NSDockWindowLevel            kCGDockWindowLevel
like image 38
Steve Waddicor Avatar answered Sep 30 '22 07:09

Steve Waddicor