Drawing a transparent NSView in NSWindow with black background - objective-c

I have a NSView inside a NSWindow and the background of NSWindow is set to black (with alpha of 0.7). Is there anyway to have the NSView showing what's underneath the NSWindow? In other words, how do I make the part where NSWindow and NSView overlap to have clear background?
Thanks!

You can try the setOpaque method

If I understand properly, it seems like you're trying to have a window with hole in it. You can take a look at apple's sample code Round Transparent Window. It shows how to add transparent areas to an NSWindow.

Related

NSTextField outer cell background inside custom draw view

I'm struggling with an NSTextField background issue. The textfield is within a custom view that has a semi-transparent black background. I've tried subclassing the field, the field's cell and a few other things to no avail. I'm getting this clearColor background outside of the text field cell. I'm looking for basically just a black NSSearchFeld, not having much luck though.
here is what i'm getting
I basically just want a textfield with a rounded stroke which blends into the black background.
thanks all, i'm fairly new to Xdev
figured it out, I needed to have the custom NSView subclass I'm using to draw the window use
[self bounds] when drawing vs the dirtyRect passed to the drawrect method.

Transparent NSWindow but with standard border and shadow [duplicate]

This question already has an answer here:
Holes in NSView or NSWindow
(1 answer)
Closed 6 years ago.
I want to have a more or less standard NSWindow with a toolbar and all that, but I want the content view to be transparent so that I can see through it. At the same time I want to keep the light gray outline of the window and also it's shadow. BUT I want to avoid the "inner" shadow I get from the toolbar inside the content view area.
What I have tried so far is just to set the window background color to a semi transparent color and also set opaque to NO. The problem is that the window border fades away with the alpha of the background itself, and the more transparency I have on the background, the more the shadow of the toolbar shows up within the content view.
Generally, the window shadow and border changes depending on the transparency of the content view, which I totally understand. But I want a behavior where it keeps the border and shadow just as if it was a completely opaque window, and then I want the content view area to be transparent.
I am not sure what I need to do conceptually to make it work. Maybe I have to draw the window border myself, maybe not. Maybe I need to draw the shadow myself, or maybe not.
Is there anyone that know how to build this? I don't need exact code details, but rather what parts I need to do custom..
I appreciate any input!
I dont't know if this is of any value for you after all this time but try:
[aWindow setOpaque:NO];
[aWindow setBackgroundColor:[NSColor clearColor]];
Subclass the NSView class, override the drawRect:(NSRect)dirtyRect method and set the color of the view as clearcolor, now set the class of your content view as the Subclass of NSView.

setBackgroundColor covers element

I have an NSWindow, and I'm using this code to add a bottom-metal-bar at the bottom.
[MyWindow setContentBorderThickness:40.0 forEdge:NSMinYEdge];
That works fine. But, once I use this:
[MyWindow setBackgroundColor: [NSColor redColor]];
The red covers the bar at the bottom. The bar shows correctly without the background color.
Yes, it would appear that changing the background-color of an NSWindow negates its bottom border. In order to achieve both effects, you can do one of two things:
In Interface Builder, move all your interface elements to a subclass of NSView that draws its background and add the view to your window.
Create an NSView that emulates the bottom border of your window and set the window's background color.
Personally, I would go for the first option, because it requires less work (trying to emulate a bottom border will be difficult, even with NSGradient) , but both are a possibility.

Howto draw a rect on screen. NSOpenGLContext vs transparent NSWindow + custom NSView

I'm reading pixels from an area of the main screen via NSOpenGLContext. Now I would like to draw a rect around that area to indicate where it actually is.
How would I do this?
My first thought was the "Cocoa way": create a transparent fullscreen NSWindow and a custom NSView to draw the rectangle path. But that feels a bit too complicated. Isn't it possible to draw directly on the NSOpenGLContext?
If you want to draw over elements not inside your application the floating window is the only correct way. There’s really no complication except mapping positions properly, which is easy to do with the coordinate-space conversions available on NSView and NSWindow.

Dimming NSWindow and layer NSView on top

Is there any way I could dim my NSWindow (basically putting a black transparent layer over top of it that you cannot click through) and then layer a custom NSView on top?
You could create an additional NSView which contains your custom NSView. This new NSView would simply draw the black transparent layer in its drawRect and capture all events to avoid them being passed through to the window.
A more compartmentalized way would be to make a borderless window containing the black view and the custom NSView, and make that a child window of the window you want to “dim”. Then use NSViewAnimation to fade the window in and out.
You will, of course, need to handle keeping their sizes matched if the “dimmed” window is resizable.
You could just set the background color of your NSWindow the black and change the opacity to your liking.