mac 開發 透明窗口的實現與及完整貼圖

思路如下:

1.在主窗口中重寫awakeFromNib

- (void) awakeFromNib{

   [self setOpaque:NO];

    [selfsetBackgroundColor:[NSColorclearColor]];

}

只要實現了該步驟,那麼主窗口將會實現透明顯示

2.在主窗口的主view的drawRect中重繪所要貼圖的內容,這個就很自由了,隨便給上自己測試的:

- (void)drawRect:(NSRect)dirtyRect

{

    // Drawing code here.

    //NSRectFillUsingOperation(dirtyRect, NSCompositeClear);

    CGContextRef context = [[NSGraphicsContextcurrentContext]graphicsPort];

    //CGContextSetRGBFillColor(context, 0.5, 0.5, 0.5, 0.9);//0.0, 0, 0.5, 0.1);

    //CGContextFillPath(context);

   int x = 0, y =0;

CGImageRef ri = GetCGImageNamed([[[NSBundlemainBundle] resourcePath]stringByAppendingString:@"/frame.png"]);

    float w =CGImageGetWidth(ri), h = CGImageGetHeight(ri);

   NSRect rect = [selfbounds];

   while (YES) {

       CGContextDrawImage(context, CGRectMake(x, y, w, h), ri);

        x += w;

       if (x >= rect.size.width) {

            y += h;

            x =0;

        }

       if (y >= rect.size.height) {

           break;

        }

    }

    [NSGraphicsContextrestoreGraphicsState];

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章