NSWindow的風(fēng)格一直只有兩種,很難看。Panel倒是有種HUD風(fēng)格的,但window沒(méi)有,畢竟window和panel的titile大小還
是不一樣的,網(wǎng)上有關(guān)于HUDWindow的代碼,不過(guò)那是把window的titlebar給隱藏掉,自己畫(huà)上三個(gè)button和右下角可以
resize的東東,如果用這個(gè)window來(lái)彈出sheet的話會(huì)很詭異的出現(xiàn)…… 就是sheet從window的最上面彈出來(lái)了,底層window看不到titlebar了~
NSWindow在設(shè)置成Texture模式之后可以設(shè)置背景色,不過(guò)這是單色,漸進(jìn)色設(shè)置不了。最簡(jiǎn)單的方法就是先給window設(shè)置背景色,再覆蓋上一層NSView來(lái)填充content的顏色,不過(guò)這樣不是渾然一體的,每個(gè)window都要這樣搞的話還是很麻煩的。
所以只能從被apple隱藏起來(lái)的方法里找辦法了~所以class-dump出來(lái)看看~
你會(huì)發(fā)現(xiàn)在NSWindow.h中繪制界面的函數(shù)幾乎沒(méi)有,不想那些繼承與NSView的控件一樣,那么多私有的繪制方法。那window是怎樣繪制的呢。
仔
細(xì)觀察,你會(huì)發(fā)現(xiàn)NSWindow有很多擴(kuò)展類,而且window功能性的東西是非常多的,所以它的重繪肯定是委托給別人來(lái)做了(這一點(diǎn)通過(guò)獲取
NSWindow的closebutton可以知道,那三個(gè)button都不是標(biāo)準(zhǔn)的NSButton,而是
NSThemeButton?記不太清楚了)。經(jīng)過(guò)查找可以發(fā)現(xiàn)函數(shù)
+(Class)frameViewClassForStyleMask:(unsigned int)styleMask
|
非常可疑,而且dump出來(lái)的類有一個(gè)叫做NSThemeFrame的,查看它的類方法,哈哈,發(fā)現(xiàn)了吧,全是跟window有關(guān)的繪制方法。
所以接下來(lái)的事情就很簡(jiǎn)單了,寫(xiě)一個(gè)類繼承 NSThemeFrame
#import <Cocoa/Cocoa.h>
#import "NSThemeFrame.h"
@interface KAThemeFrame : NSThemeFrame
{
}
@end
|
那么需要重寫(xiě)哪些函數(shù)呢~我們的目標(biāo)是重畫(huà)TitleBar,具體窗體的內(nèi)容不需要重畫(huà),因?yàn)榉凑幸粋€(gè)NSView要覆蓋在上面的,所以一個(gè)方法就足夠了,廢話少說(shuō),上代碼
#import "KAThemeFrame.h"
@implementation KAThemeFrame
- (id)contentFill
{
// This color is used only when dragging.
// Please don't try to modify the value.
return [NSColor colorWithCalibratedWhite:.13 alpha:1];
}
- (id)frameColor
{
return [NSColor redColor];
}
- (void)_drawTitleBar:(NSRect)rect
{
NSRect titleRect = [self titlebarRect];
// Panel style
if([self _isUtility])
{
KAGradient *titleGradient = [KAGradient gradientWithStartingColor: [NSColor colorWithCalibratedRed:0.423f green:0.423f blue:0.423f alpha:1.0]
endingColor: [NSColor colorWithCalibratedRed:0.365f green:0.365f blue:0.365f alpha:1.0]];
[titleGradient drawInRect:titleRect angle:-90];
}
// Window style
else
{
float radius = 4;
NSBezierPath *borderPath = [NSBezierPath bezierPathWithRoundedRect:titleRect cornerRadius:radius inCorners:(OSTopLeftCorner | OSTopRightCorner)];
NSBezierPath *titlebarPath = [NSBezierPath bezierPathWithRoundedRect:NSInsetRect(titleRect, 1, 1) cornerRadius:radius-1 inCorners:(OSTopLeftCorner | OSTopRightCorner)];
KAGradient *titleGradient = [KAGradient gradientWithStartingColor: [NSColor colorWithCalibratedRed:0.423f green:0.423f blue:0.423f alpha:1.0]
endingColor: [NSColor colorWithCalibratedRed:0.365f green:0.365f blue:0.365f alpha:1.0]];
KAGradient *borderGradient = [KAGradient gradientWithStartingColor: [NSColor colorWithCalibratedRed:0.423f green:0.423f blue:0.423f alpha:1.0]
endingColor: [NSColor colorWithCalibratedRed:0.365f green:0.365f blue:0.365f alpha:1.0]];
[[NSColor clearColor] set];
NSRectFill(titleRect);
[borderGradient drawInBezierPath:borderPath angle:-90];
[titleGradient drawInBezierPath:titlebarPath angle:-90];
}
[self _drawTitleStringIn:[self _titlebarTitleRect] withColor:[NSColor colorWithDeviceWhite:.75 alpha:1]];
具體請(qǐng)看:http://www.cocoachina.com/macdev/cocoa/2010/0122/352.html
|
@import url(http://www.shnenglu.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);