[Cocoa]_[初级]_[自定义的界面上绘制图片]

[Cocoa]_[初级]_[自定义的界面上绘制图片],第1张

概述创建一个NSView的子类. 然后根据需要创建自定义的view 来完成自定义画图和事件响应. 头文件:StretchView.h #import <Cocoa/Cocoa.h>@interface StretchView : NSView{ NSBezierPath *path; NSImage *image;}-(void) setImage:(NSImage *) n

创建一个NSVIEw的子类. 然后根据需要创建@R_301_6942@的vIEw 来完成@R_301_6942@画图和事件响应.

头文件:StretchVIEw.h

#import <Cocoa/Cocoa.h>@interface StretchVIEw : NSVIEw{    NSBezIErPath *path;    NSImage *image;}-(voID) setimage:(NSImage *) newImage;@end

源文件:StretchVIEw.m
#import "StretchVIEw.h"@implementation StretchVIEw-(voID) setimage:(NSImage *)newImage{    [newImage retain];    [image release];    image = newImage;    [self setNeedsdisplay:YES];}-(ID) initWithFrame:(NSRect)frameRect{    [path closePath];    return self;}-(voID) drawRect:(NSRect) rect{    NSRect bounds = [self bounds];    [[NScolor greencolor] set];    [NSBezIErPath fillRect:bounds];    [[NScolor whitecolor] set];    [path stroke];        NSBezIErPath *path = [NSBezIErPath bezIErPath];    [path setlinewidth:2];    NSPoint startPoint = {0,20};    NSPoint endPoint = { 100,20};        NSPoint secondPoint = {0,130};    NSPoint secondEndPoint ={100,130};    [path movetoPoint:startPoint];    [path linetoPoint:endPoint];    [path movetoPoint:secondPoint];    [path linetoPoint:secondEndPoint];    [[NScolor browncolor] set];      [path stroke];            NSRect r = NSMakeRect(0,1,100,150);    NSBezIErPath *bp = [NSBezIErPath bezIErPathWithRect:r];    NScolor *color = [NScolor bluecolor];    Nsstring *str = @" 11.png";    [str drawInRect:r withAttributes:NulL];        [color set];    [bp stroke];        NSRect downRect = NSMakeRect(0,20);    Nsstring *downString =@" # 1";    [downString drawInRect:downRect withAttributes:nil];       if(image)    {        NSRect imageRect;        imageRect.origin = NSZeroPoint;        imageRect.size = [image size];        NSRect drawingRect = [self currentRect];        [image drawInRect:drawingRect                 fromrect:imageRect                operation:NSCompositeSourceOver                 fraction:1.0];    }}-(voID) dealloc{    [path release];    [image release];    [super dealloc];}-(NSRect) currentRect{    return NSMakeRect(0,400,400);}@end
头文件:AppController.h

#import <Cocoa/Cocoa.h>@class StretchVIEw;@interface AppController : NSObject{    IBOutlet StretchVIEw *stretchVIEw;}-(voID)showImage;@end
源文件:AppController.m

#import "AppController.h"#import "StretchVIEw.h"@implementation AppController-(voID) showImage{           Nsstring *path = @"Users/picture/11.png";        NSImage *image = [[NSImage alloc] initWithContentsOffile:path];                [stretchVIEw setimage:image];        [image release];    }@end
总结

以上是内存溢出为你收集整理的[Cocoa]_[初级]_[自定义的界面绘制图片]全部内容,希望文章能够帮你解决[Cocoa]_[初级]_[自定义的界面上绘制图片]所遇到的程序开发问题。

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

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

原文地址:https://www.54852.com/web/1052362.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存