Round corners on a borderless NSWindow

2019-04-09 14:35发布

问题:

I'm creating an application and I don't want a title bar:

If the title remains the same all the time, does it make sense to show it? For example, if an app doesn’t show the names of documents, or any other assets that it opens, and there is plenty of space at the top around other controls to grab onto if you want to move the window around, does the title serve much purpose?

The problem is: how do I do this? I tried using [mainWindow setStyleMask:NSBorderlessWindowMask]; but I can't make it have round corners. I really don't know how to make round corners. Next to that, I can't make it have a resize control. If I use [mainWindow setStyleMask:NSBorderlessWindowMask | NSResizableWindowMask]; it's not borderless anymore. Can anyone help me? Thanks.

回答1:

Though this is a very old question...

Now it's easier on OS X 10.11.

        window1.backgroundColor             =   NSColor.whiteColor()
        window1.opaque                      =   false
        window1.styleMask                   =   NSResizableWindowMask
                                            |   NSTitledWindowMask
                                            |   NSFullSizeContentViewWindowMask
        window1.movableByWindowBackground   =   true
        window1.titlebarAppearsTransparent  =   true
        window1.titleVisibility             =   .Hidden
        window1.showsToolbarButton          =   false
        window1.standardWindowButton(NSWindowButton.FullScreenButton)?.hidden   =   true
        window1.standardWindowButton(NSWindowButton.MiniaturizeButton)?.hidden  =   true
        window1.standardWindowButton(NSWindowButton.CloseButton)?.hidden        =   true
        window1.standardWindowButton(NSWindowButton.ZoomButton)?.hidden         =   true

        window1.setFrame(CGRect(x: 400, y: 0, width: 400, height: 500), display: true)
        window1.makeKeyAndOrderFront(self)

Here's full working example.

This is a copied answer from another question. Seems a bit different, but can be answered equally.



回答2:

There is a similar SO question here: Hide NSWindow title bar.

Basically if you use NSBorderlessWindowMask you can't use any other style mask so you will have to implement your own window controls and round the corners yourself.

Or you could use a regular NSWindow and just not set a title. See Tweetie.app for an example.

Hopefully Apple will provide new window styles with 10.7.



回答3:

If you want to do something completely different (like drawing your own window) check out Matt Gemmell's code. Look at MAAttachedWindow code for ways of removing the title bar, drawing bezier corners, and making it look superb.