如何使NSView透明和显示在NSWindow下的内容? [英] How to make an NSView transparent and show what's under the NSWindow?

查看:2440
本文介绍了如何使NSView透明和显示在NSWindow下的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在osx做一个放大镜样式的应用程序的乐趣,我遇到一个障碍,我似乎找不到答案。

I'm making a loupe style application in osx for fun and I ran into a snag I can't seem to find the answer to.

我在NSWindow里面有一个NSView我设置视图为clearColor和alpha到.25,但我不知道如何使NSWindow透明背后NSView

I have a NSView inside an NSWindow I set the view to clearColor and the alpha to .25, but I cannot figure out how to make the NSWindow transparent behind the NSView so you can see things below the application when it's selected.

在通过doc搜索时,似乎找不到我在寻找任何想法?

Been digging through doc's can't seem to find what I'm looking for any thoughts?

感谢

推荐答案

也许可以尝试 NSWindow的 setOpaque:需要使用透明颜色( setBackgroundColor:)设置窗口的背景颜色

Perhaps try NSWindow's setOpaque: and you also need to set the window's background color with a transparent color (setBackgroundColor:)

did:

自定义NSView命名为myView:

custom NSView named myView :

-(void)drawRect:(NSRect)dirtyRect
{
    [[NSColor windowBackgroundColor] set];  // Using the default window colour,
    dirtyRect.size.width /= 2;
    NSRectFill(dirtyRect);      // Only draw the part you need.
}

接下来我设置一个窗口的contentView一个myView实例。我在IB中做了,但你可以通过 setContentView 手动设置它。

Next I set a window's contentView an instance of myView. I did it in IB but you can manually set it via setContentView.

我将窗口配置为:

[myWin setOpaque:NO];  //Tells the window manager that the window might have transparent parts.
[myWin setBackgroundColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.0]];  //Tells the window to use a transparent colour.

这是我得到的。

就像我说的,你需要告诉窗口使用不可见的颜色作为背景,然后重绘您实际需要绘制的部分。

So like I said, you need to tell the window to use an invisible colour as background, then redraw the parts you actually do need drawn.

这篇关于如何使NSView透明和显示在NSWindow下的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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