NSWindow:将部分图像放在窗口外 [英] NSWindow: place part of image outside window

查看:182
本文介绍了NSWindow:将部分图像放在窗口外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 NSBorderlessWindowMask (没有标题栏)样式的 NSWindow 的Cocoa应用程序。我想在窗口中放置一个图像,但是图像的一部分应该是窗口之外的位置。



如何做到这一点?



>



可爱与爱的例子



parmanoir.com示例



要点是将NSWindow子类化为无边框的透明窗口,然后创建一个子视图,绘制您的自定义形状并将其作为窗口内容视图。



样本的样子阴影应该仍然适用。
来自Cocoa With Love:


窗口后面的阴影是自动绘制的。窗口的完全清除的任何部分都不会收到鼠标点击(它们将通过窗口落下)。


border在你的视图类显示方法中做这样的事情:

  NSBezierPath * border = [NSBezierPath bezierPathWithRect:self.frame]; 
[border setLineWidth:1.0];
[[NSColor windowFrameColor] set];
[border stroke];

如果您没有自定义视图类,那么 [view lockFocus ]; ,然后用您的视图实例替换 self 。在绘制后一定要做 [view unlockFocus];



文档中有关 lockFocus


隐藏或小型化单击窗口会导致该窗口的后备存储释放。如果你不使用标准显示机制来绘制,你应该使用lockFocusIfCanDraw而不是lockFocus,如果有一个机会绘制时窗口是小型化或隐藏。


另一种做到这一点的方法,肯定会保持阴影是使用两个窗口,并使一个孩子对另一个。



你不会因为各种原因而不能在窗口外绘制,至少你的进程需要拥有或者获得它的许可(很多其他原因)。


I've a Cocoa application with a NSWindow with the style NSBorderlessWindowMask (without titlebar). I would like to place a image in the window but a part of the image should be places outside of the window.

How can this be done?

解决方案

Here are two articles I found related to what you want to do.

Cocoa With Love Example

parmanoir.com example

The gist is to subclass NSWindow to make it a borderless transparent window, then make a sub view that draws your custom shape and make it the windows content view.

From the look of the sample the shadow should still apply. From Cocoa With Love:

The shadow behind the window is drawn automatically for whatever shape we draw. Any part of the window that is left completely clear will not receive mouse clicks (they will fall through the window).

to draw the border do something like this in your view class display method:

NSBezierPath* border = [NSBezierPath bezierPathWithRect:self.frame];
[border setLineWidth: 1.0];
[[NSColor windowFrameColor] set];
[border stroke];

If you don't have a custom view class then do [view lockFocus]; before doing that path and replace self with your view instance. after drawing be sure to do [view unlockFocus];

An important message from the docs concerning lockFocus:

Hiding or miniaturizing a one-shot window causes the backing store for that window to be released. If you don’t use the standard display mechanism to draw, you should use lockFocusIfCanDraw rather than lockFocus if there is a chance of drawing while the window is either miniaturized or hidden.

Another way to do this that for sure would keep the shadow would be to use two windows and make one a child to the other.

You will not ever be able to draw outside of a window for various reasons, not the least of which being your process needs to own or have permission for what it draws to (many other reasons too).

这篇关于NSWindow:将部分图像放在窗口外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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