NSWindow的風格一直只有兩種,很難看。Panel倒是有種HUD風格的,但window沒有,畢竟window和panel的titile大小還
是不一樣的,網上有關于HUDWindow的代碼,不過那是把window的titlebar給隱藏掉,自己畫上三個button和右下角可以
resize的東東,如果用這個window來彈出sheet的話會很詭異的出現…… 就是sheet從window的最上面彈出來了,底層window看不到titlebar了~
NSWindow在設置成Texture模式之后可以設置背景色,不過這是單色,漸進色設置不了。最簡單的方法就是先給window設置背景色,再覆蓋上一層NSView來填充content的顏色,不過這樣不是渾然一體的,每個window都要這樣搞的話還是很麻煩的。
所以只能從被apple隱藏起來的方法里找辦法了~所以class-dump出來看看~
你會發現在NSWindow.h中繪制界面的函數幾乎沒有,不想那些繼承與NSView的控件一樣,那么多私有的繪制方法。那window是怎樣繪制的呢。
仔
細觀察,你會發現NSWindow有很多擴展類,而且window功能性的東西是非常多的,所以它的重繪肯定是委托給別人來做了(這一點通過獲取
NSWindow的closebutton可以知道,那三個button都不是標準的NSButton,而是
NSThemeButton?記不太清楚了)。經過查找可以發現函數
+(Class)frameViewClassForStyleMask:(unsigned int)styleMask
|
非常可疑,而且dump出來的類有一個叫做NSThemeFrame的,查看它的類方法,哈哈,發現了吧,全是跟window有關的繪制方法。
所以接下來的事情就很簡單了,寫一個類繼承 NSThemeFrame
#import <Cocoa/Cocoa.h>
#import "NSThemeFrame.h"
@interface KAThemeFrame : NSThemeFrame
{
}
@end
|
那么需要重寫哪些函數呢~我們的目標是重畫TitleBar,具體窗體的內容不需要重畫,因為反正有一個NSView要覆蓋在上面的,所以一個方法就足夠了,廢話少說,上代碼
#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]];
具體請看: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);