Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
591 views
in Technique[技术] by (71.8m points)

objective c - Put a transparent NSWindow permanently on top of another 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?

EDIT:: 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Child window is what you need.

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.

Update with working code:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...