自定义程序皮肤

自定义程序皮肤,第1张

概述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 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 !

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,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,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
总结

以上是内存溢出为你收集整理的自定义程序皮肤全部内容,希望文章能够帮你解决自定义程序皮肤所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1054515.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存