逛奔的蜗牛

我不聪明,但我会很努力

   ::  :: 新随笔 ::  ::  :: 管理 ::
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.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
posted on 2011-12-09 04:17 逛奔的蜗牛 阅读(2892) 评论(0)  编辑 收藏 引用 所属分类: Cocoa

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理