2013-02-06 348 views
3

我需要更改我的应用程序的NSWindow周围的边框颜色。更改NSWindow边框颜色

有谁知道窗口边框的绘制位置,以及如何更改颜色/覆盖绘制边框的方法?

我注意到,Tweetbot做到这一点:

Regular border vs Tweetbot border

+0

以一个刺在这里的黑暗:如果你叫什么'[self.window.contentView setBorderType:NSNoBorder]',和看看它是否完全消除了边界?从这一点,你可以绘制自己的。 –

+0

@ sudorm-rf这里没有运气:/ –

+0

是的,我认为这是行不通的。这里的主要问题是'NSWindow'实际上使用私人CGS函数在主要内容之外绘制了这个边界。除非您使用无边框窗口,否则我似乎无法记住任何方法来删除此内容。 –

回答

2

从内存中,我觉得Tweetbot已经使用了一个完整的无国界的窗口,并添加窗口控制自己,但是如果你想要让了AppKit仍处理这些细节,还有另一种方法。如果您将窗口设置为纹理窗口,则可以设置自定义背景NSColor。这NSColor可以是一个图像使用+[NSColor colorWithPatternImage:]

作为一个例子,我只是使用一个纯灰色作为填充,但你可以在这个图像中绘制任何你喜欢的东西。然后,您只需将NSWindow的类类型设置为纹理窗口类。

SLFTexturedWindow.h

@interface SLFTexturedWindow : NSWindow 
@end 

SLFTexturedWindow.m

#import "SLFTexturedWindow.h" 

@implementation SLFTexturedWindow 

- (id)initWithContentRect:(NSRect)contentRect 
       styleMask:(NSUInteger)styleMask 
        backing:(NSBackingStoreType)bufferingType 
        defer:(BOOL)flag; 
{ 
    NSUInteger newStyle; 
if (styleMask & NSTexturedBackgroundWindowMask) { 
    newStyle = styleMask; 
} else { 
    newStyle = (NSTexturedBackgroundWindowMask | styleMask); 
} 

if (self = [super initWithContentRect:contentRect styleMask:newStyle backing:bufferingType defer:flag]) { 

    [[NSNotificationCenter defaultCenter] addObserver:self 
               selector:@selector(windowDidResize:) 
                name:NSWindowDidResizeNotification 
                object:self]; 

     [self setBackgroundColor:[self addBorderToBackground]]; 

     return self; 
} 

return nil; 
} 

- (void)windowDidResize:(NSNotification *)aNotification 
{ 
    [self setBackgroundColor:[self addBorderToBackground]]; 
} 

- (NSColor *)addBorderToBackground 
{ 
    NSImage *bg = [[NSImage alloc] initWithSize:[self frame].size]; 
    // Begin drawing into our main image 
[bg lockFocus]; 

[[NSColor lightGrayColor] set]; 
NSRectFill(NSMakeRect(0, 0, [bg size].width, [bg size].height)); 

    [[NSColor blackColor] set]; 

    NSRect bounds = NSMakeRect(0, 0, [self frame].size.width, [self frame].size.height); 
    NSBezierPath *border = [NSBezierPath bezierPathWithRoundedRect:bounds xRadius:3 yRadius:3]; 
    [border stroke]; 

    [bg unlockFocus]; 

    return [NSColor colorWithPatternImage:bg]; 
} 

@end 
+0

看起来像应该做的伎俩谢谢! –

+0

事实上,它看起来像这样会导致窗口有2个边框,里面是黑色的,外面是灰色的。 http://cl.ly/image/201s0X0S1F3Q –

+0

是的,看起来你是对的。对不起,我无法看到灰色边框,甚至仔细观察(视力不良和视网膜显示的组合)。它看起来像你必须使用无边框窗口方法,并自己绘制标题栏 – iain