gpt4 book ai didi

objective-c - 更改 NSWindow 边框颜色

转载 作者:搜寻专家 更新时间:2023-10-30 20:19:53 24 4
gpt4 key购买 nike

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

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

我注意到 Tweetbot 会这样做:

Regular border vs Tweetbot border

最佳答案

根据内存,我认为 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

关于objective-c - 更改 NSWindow 边框颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14720366/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com