将一个透明的 NSWindow 永久地放在另一个 NSWindow 之上 [英] Put a transparent NSWindow permanently on top of another NSWindow

查看:39
本文介绍了将一个透明的 NSWindow 永久地放在另一个 NSWindow 之上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 NSWebView 之上有一些 UI 控件,并且由于这个问题" https://stackoverflow.com/questions/9120868/video-in-nswebview-hides-views-on-top-of-the-nswebview " 我现在想在我的 NSWebView 之上添加一个透明的"NSWindow,因此没有关闭按钮等,因此,在我当前的之上>NSWindow.

I want to have some UI controls on top of a NSWebView and because of this problem " https://stackoverflow.com/questions/9120868/video-in-nswebview-hides-views-on-top-of-the-nswebview " I now want to add a "transparent" NSWindow, so without the close buttons etc., on top of my NSWebView, hence, on top of my current NSWindow.

我怎样才能做到这一点并确保这个覆盖窗口"保持原位,即使我移动了底层窗口?

How can I achieve this and make sure that this "overlay window" stays in place, even if I move the underlying window?

:虽然@dzolanta 的方法工作正常,但我想知道是否可以通过使用 NSWindowController 来做到这一点,这将允许我正确使用 Outlets 等.

: While @dzolanta's approach works fine, I wonder if it is possible to do it by using an NSWindowController which would allow me to properly use Outlets etc.

推荐答案

子窗口就是你所需要的.

Child window is what you need.

使用 NSBorderlessWindowMask 创建 NSWindow 并使用 - setOpaque:- setBackgroundColor: 方法将其定义为透明.然后将新创建的窗口添加为包含 NSWebView 实例的窗口的子窗口(使用 NSWindow- addChildWindow:ordered: 方法).移动父窗口会自动导致子窗口移动.

Create NSWindow with NSBorderlessWindowMask and define it to be transparent using - setOpaque: and - setBackgroundColor: methods. Then add newly created window as a child of window containing an instance of NSWebView (using NSWindow's - addChildWindow:ordered: method). Moving parent window will automatically cause child window to move.

使用工作代码更新:

CGRect wRect = self.window.frame;
NSView *contentView  =self.window.contentView;
CGRect cRect = contentView.frame;

CGRect rect = CGRectMake(wRect.origin.x, wRect.origin.y, cRect.size.width, cRect.size.height);
NSWindow *overlayWindow = [[NSWindow alloc]initWithContentRect:rect 
                                                     styleMask:NSBorderlessWindowMask 
                                                       backing:NSBackingStoreBuffered 
                                                         defer:NO];
overlayWindow.backgroundColor = [NSColor redColor];
[overlayWindow setOpaque:NO];
overlayWindow.alphaValue = 0.5f;

[self.window addChildWindow:overlayWindow ordered:NSWindowAbove];

这篇关于将一个透明的 NSWindow 永久地放在另一个 NSWindow 之上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆