xcode Cocoa app 在所有 mac 运行的应用程序之上创建透明视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16779469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:25:56  来源:igfitidea点击:

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

xcodemacoscocoa

提问by user2152814

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.

我正在使用 Xcode 开发 mac osx 应用程序。我想在所有应用程序之上添加一个透明的全屏视图/窗口。这样我就可以在透明视图上“绘图”,在它后面将是任何应用程序、safari、word 等。

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.

这是我的 mac 应用程序的新全屏子窗口。但这并不是我在 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!!!

所以我的问题是,如何在我的 mac 屏幕视图(不仅是我的应用程序的顶视图)之上添加视图。非常感谢!!!

回答by Ken Thomases

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].

请参阅 Apple 的FunkyOverlayWindow 示例代码。除了设置窗口的级别外,您还需要将其背景颜色设置为clear 并将其设置为non-opaque。如果它是透明的,但您仍然希望它接收用于绘图的鼠标事件(而不是让它们穿过它后面的窗口),则需要执行[window setIgnoresMouseEvents:NO].

回答by Steve Waddicor

This looks like what you want.

这看起来像你想要的。

NSWindowhas - (void)setLevel:(NSInteger)windowLevel

NSWindow- (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.

有了这个有用的预定义级别。选择一个你喜欢的。如果您希望它正好高于或低于这些级别之一,则加或减 1。

#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

回答by user2152814

My problem was just because I didn't set the self.window to the top layer. Then I add a childview(set to the top) to the self.window. It does nothing if I only set childview to top.

我的问题只是因为我没有将 self.window 设置为顶层。然后我向 self.window 添加一个子视图(设置为顶部)。如果我只将 childview 设置为顶部,它什么都不做。

Ken and Steve's answers have reasons. Thanks a lot.

肯和史蒂夫的回答是有原因的。非常感谢。