NSWindow: place part of image outside window

☆樱花仙子☆ 提交于 2019-12-23 16:17:06

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/19684861/nswindow-place-part-of-image-outside-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!