自定义程序皮肤

EDIT  to completely change the window appearance, check out Matt Gallagher's  Drawing a custom window

After seeing Safari 4 drawing its tab bar in a custom way, I wondered how to do that. Each window has a frame view (the superview of contentView) that draws the window, replacing that view's drawRect: with our own will let us draw it ourselves !

Image:Custom NSThemeFrame drawing.png

First, replace the window's frame drawRect: with our own :

// Get window's frame view class
 id class = [[[window contentView] superview] class];  
// Add our drawRect: to the frame class 
Method m0 = class_getInstanceMethod([self class], @selector(drawRect:)); 
class_addMethod(class, @selector(drawRectOriginal:), method_getImplementation(m0), method_getTypeEncoding(m0));  
// Exchange methods 
Method m1 = class_getInstanceMethod(class, @selector(drawRect:));
Method m2 = class_getInstanceMethod(class, @selector(drawRectOriginal:));
method_exchangeImplementations(m1, m2);

Cocoa will then call our method each time the window frame needs to be drawn. We can draw an entirely custom frame or draw over the existing one(The attached sample does the latter)

A standard Cocoa window has rounded corners that need to be accounted for by building a clipping path to clip our custom drawing to the window shape :

  • Get the rounded corner radius with roundedCornerRadius
  • Using this radius, build a rounded corner path that describes the window
  • Intersect that path with the current rect
  • Draw our stuff
- (void)drawRect:(NSRect)rect { 
// Call original drawing method 
[self drawRectOriginal:rect]; 
// Build clipping path : intersection of frame clip (bezier path with rounded corners) and rect argument  
NSRect windowRect = [[self window] frame]; 
windowRect.origin = NSMakePoint(0, 0);  
float cornerRadius = [self roundedCornerRadius]; 
[[NSBezierPath bezierPathWithRoundedRect:windowRect xRadius:cornerRadius yRadius:cornerRadius] addClip]; 
[[NSBezierPath bezierPathWithRect:rect] addClip]; 
 // Any custom drawing goes here ... 
}
Application.h中添加:
- (void)redrawAllWindows;
- (void)drawRect:(NSRect)rect;
#import <objc/runtime.h>
@interface Application(ShutUpXcode)
- (float)roundedCornerRadius;
- (void)drawRectOriginal:(NSRect)rect;
@end


 - (void) applicationDidFinishLaunching: (NSNotification *) notification
{  //......
  if (atLeastMountainLion())
    {
        id windowClass = [[[[[[NSApplication sharedApplication] windows] objectAtIndex:0] contentView] superview] class];
        
        // Exchange draw rect
        Method m0 = class_getInstanceMethod([self class], @selector(drawRect:));
        class_addMethod(windowClass, @selector(drawRectOriginal:), method_getImplementation(m0), method_getTypeEncoding(m0));
        
        Method m1 = class_getInstanceMethod(windowClass, @selector(drawRect:));
        Method m2 = class_getInstanceMethod(windowClass, @selector(drawRectOriginal:));
        
        method_exchangeImplementations(m1, m2);
    }
    else
    {
        for (id window in [[NSApplication sharedApplication] windows])
        {
            // Get window's frame view class
            id windowClass = [[[window contentView] superview] class];
            
            // Exchange draw rect
            Method m0 = class_getInstanceMethod([self class], @selector(drawRect:));
            class_addMethod(windowClass, @selector(drawRectOriginal:), method_getImplementation(m0), method_getTypeEncoding(m0));
            
            Method m1 = class_getInstanceMethod(windowClass, @selector(drawRect:));
            Method m2 = class_getInstanceMethod(windowClass, @selector(drawRectOriginal:));
            
            method_exchangeImplementations(m1, m2);
        }
    }
    
    [self redrawAllWindows];
    [[NSUserDefaultsController sharedUserDefaultsController] addObserver:self forKeyPath:[Preferences pathForKey:kPrefKeyGUIColorScheme] options:0 context: NULL];
}
- (void)drawRect:(NSRect)rect
{
    // Call original drawing method
    [self drawRectOriginal:rect];
    
    if (currentColorScheme == kPrefKeyGUISilverColorScheme) return;
    //
    // Build clipping path : intersection of frame clip (bezier path with rounded corners) and rect argument
    //
    NSRect windowRect = [[self window] frame];
    windowRect.origin = NSMakePoint(0, 0);
    
    float cornerRadius = [self roundedCornerRadius];
    [[NSBezierPath bezierPathWithRoundedRect:windowRect xRadius:cornerRadius yRadius:cornerRadius] addClip];
    [[NSBezierPath bezierPathWithRect:rect] addClip];
    
    //
    // Draw a background color on top of everything
    //


    switch(currentColorScheme)
    {
        case kPrefKeyGUICharcoalColorScheme:
        {
            CGContextRef context = (CGContextRef)[NSGraphicsContext currentContext];
            CGContextSetBlendMode(context, kCGBlendModeNormal);
            [[NSColor backgroundColorForControl:@"NSWindow" colorScheme: kPrefKeyGUICharcoalColorScheme] set];
            [[NSBezierPath bezierPathWithRect:rect] fill];
            break;
        }
        default:
            assert(false);
    }
}
See more detail info: http://parmanoir.com/Custom_NSThemeFrame
 

                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值