質問

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?

enter image description here

役に立ちましたか?

解決

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top