我對OS X Cocoa應用程式的編程非常陌生。所以請大家多多體諒。我想制作一個簡單的應用程式,但我把自己封鎖在應用程式中繪制影像。
這段代碼應該繪制多張圖片,并在調整視窗應用程式的大小或最大化時改變排列。
#include <Cocoa/Cocoa.h>
#ifndef NSWindowStyleMaskTitled
#define NSWindowStyleMaskTitled NSTitledWindowMask
#define NSWindowStyleMaskClosable NSClosableWindowMask
#define NSWindowStyleMaskMiniaturizable NSMiniaturizableWindowMask
#define NSWindowStyleMaskResizable NSResizableWindowMask
#define NSEventMaskAny NSAnyEventMask
#endif
@interface Window : NSWindow {
NSTimer* mTimer。
}
- (instancetype)init;
- (BOOL) acceptsFirstResponder。
- (BOOL) acceptsFirstMouse: (NSEvent*) pEvent;
- (void) viewDidMoveToWindow;
- (void) drawRect: (NSRect) rect;
- (void) onTimer: (NSTimer*) pTimer;
- (void) getMouseXY: (NSEvent*) pEvent x: (int*) pX y: (int*) pY;
- (void) mouseDown:(NSEvent*)event;
- (void) mouseUp: (NSEvent*)event。
- (void) mouseDragged: (NSEvent*) event。
- (void) rightMouseDown: (NSEvent*) event;
- (void) rightMouseUp: (NSEvent*) event;
- (NSDragOperation) draggingEntered:(id <NSDraggingInfo>)sender;
- (BOOL) performDragOperation:(id <NSDraggingInfo>)sender;
- (void) windowDidResize:(NSNotification*)notification。
- (void) windowDidMiniaturize:(NSNotification*)sender;
- (void) windowDidDeMiniaturize:(NSNotification*)sender;
- (BOOL) windowShouldClose:(id)sender;
@結束
@implementation Window
- (instancetype)init {
mTimer = 0;
double sec = 1.0 / 24;
mTimer = [NSTimer timerWithTimeInterval:sec target:self selector:@selector(onTimer:) userInfo:nil repeats:YES]。
[[NSRunLoop currentRunLoop] addTimer: mTimer forMode: (NSString*) kCFRunLoopCommonModes]。
[self registerForDraggedTypes:[NSArray arrayWithObjects: NSFilenamesPboardType, nil]]。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidResize:) name:NSWindowDidResizeNotification object:self]。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidMiniaturize:) name:NSWindowDidMiniaturizeNotification object:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidDeMiniaturize:) name:NSWindowDidDeminiaturizeNotification object:self];
[super initWithContentRect:NSMakeRect(0, 0, 500, 300) styleMask: NSWindowStyleMaskTitled | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable | NSWindowStyleMaskClosable backing:NSBackingStoreBuffered defer:NO】。]
[self setTitle:@"Hello world"] 。
[self center];
[self setOpaque:NO];
[self setHasShadow:YES]。
//[self setBackgroundColor:[NSColor clearColor]];
[self setBackgroundColor:[NSColor grayColor]]。
[self setIsVisible:YES]。
return self。
}
- (BOOL) acceptsFirstResponder {
return YES;
}
- (BOOL) acceptsFirstMouse: (NSEvent*) pEvent {
return YES;
}
- (void) viewDidMoveToWindow {
NSWindow* pWindow = [self window];
if (pWindow) {
[pWindow makeFirstResponder: self];
[pWindow setAcceptsMouseMovedEvents: YES];
}
}
- (void) drawRect: (NSRect) rect {
NSLog(@"drawRect %f, %f, %f", rect.origin.x, rect.origin.y, rect.size.width) 。
}
- (void) onTimer: (NSTimer*) pTimer {
NSRect r;
if (pTimer == mTimer) {
//NSLog(@"mTimer");
}
}
- (void) getMouseXY: (NSEvent*) pEvent x: (int*) pX y: (int*) pY {
NSPoint pt = [pEvent locationInWindow];
*pX = (int) pt.x;
//*pY = mGraphics->Height() - (int) pt.y;.
*pY = 555 - (int) pt.y;
NSLog(@"%f , %f", pt.x, pt.y) 。
}
- (void)mouseDown:(NSEvent*)event {
NSLog(@"mouseDown called")。
}
- (void) mouseUp: (NSEvent*) event {
int x, y;
[self getMouseXY:event x:&x y:&y]。
}
- (void) mouseDragged: (NSEvent*) event {
int x, y;
[self getMouseXY:event x:&x y:&y]。
}
- (void) rightMouseDown: (NSEvent*) event {
int x, y;
[self getMouseXY:event x:&x y:&y]。
}
- (void) rightMouseUp: (NSEvent*) event {
int x, y;
[self getMouseXY:event x:&x y:&y]。
}
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>) sender {
NSPasteboard *pboard = [sender draggingPasteboard];
return (([pboard.type containsObject:NSFilenamesPboardType]) ? NSDragOperationLink: NSDragOperationNone)。)
}
- (BOOL) performDragOperation:(id <NSDraggingInfo>) sender {
NSPasteboard *pboard = [sender draggingPasteboard];
if ( [[pboard types] containsObject:NSFilenamesPboardType] ) {
/NSPoint pt = [self convertPoint:[sender draggingLocation] fromView:nil];
for (NSString *path in [pboard propertyListForType:NSFilenamesPboardType] ) {
const char* tmp=[path UTF8String]。
NSLog(@"%@"/span>, path)。
//if (mGraphics) {.
//mGraphics->OnMouseDrop(pt.x, mGraphics-> Height() - pt.y, (char*)tmp);
//break;
//}
}
}
return YES;
}
- (void)windowDidResize:(NSNotification*)notification {
//NSLog(@"Resize");
}
- (void)windowDidMiniaturize:(NSNotification*)notification {
NSLog(@"Minimize"/span>)。
}
- (void)windowDidDeMiniaturize:(NSNotification*)notification {
NSLog(@"DeMinimize")。
}
- (BOOL)windowShouldClose:(id)sender {
[NSApp terminate:sender] 。
return YES;
}
@end
int main(int argc, char* argv[]) /span> {
[NSApplication sharedApplication];
[[[[Window alloc] init] autorelease] makeMainWindow]。
[NSApp運行]。
但是drawRect()根本就沒有被呼叫,問題出在哪里?
這個程序應該像Windows一樣,在有臟的區域需要重新繪制時被呼叫嗎?
uj5u.com熱心網友回復:
那么,我如何用NSView完成我的代碼呢?
為了代替你的例子,我推薦類似以下的方法供你考慮。 它是以編程方式創建 Cocoa 應用程式的另一種方式,并演示了在自定義 NSView 中使用 -drawRect() 的情況。 請注意,該視窗是可以調整大小的,與你發布的內容類似。 要在Xcode中運行,首先創建一個objc專案,然后洗掉為你提供的AppDelegate,并在洗掉原始代碼后將以下內容粘貼到main.m中:
#import <Cocoa/Cocoa.h> @interface CustomView : NSView //methods automatically called @interface CustomView : NSView @結束 @實作CustomView - (id)initWithFrame:(NSRect)frameRect { if ((self = [super initWithFrame:frameRect]) != nil) { //在這里添加初始化代碼。 } return self; } - (void)drawRect:(NSRect)rect { //**** Background **** // [[NSColor orangeColor] set] 。 [NSBezierPath fillRect:rect]; NSBezierPath *path = [NSBezierPath bezierPathWithOvalInRect:NSInsetRect([self bounds], 30,30)]。 [[NSColor redColor] set] 。 [路徑填充]。 [[NSColor whiteColor] set]。 [path setLineWidth:1.0]。 [path stroke]。 } //如果你想讓0,0(原點)成為頂部、左側,請使用此方法//。 //否則原點將在底部,左邊(未翻轉)//。 -(BOOL)isFlipped { return YES; } @end @interface AppDelegate : NSObject <NSApplicationDelegate> { NSWindow *window。 } - (void) myBtnAction: (id)sender; - (void) buildMenu; - (void) buildWnd; @結束 @implementation AppDelegate - (void) myBtnAction: (id)sender { NSBeep()。 } - (void) buildMenu { NSMenu *menubar = [NSMenu new]; NSMenuItem *appMenuItem = [NSMenuItem new]; [menubar addItem:appMenuItem]。 [NSApp setMainMenu:menubar]。 NSMenu *appMenu = [NSMenu new]; NSMenuItem *quitMenuItem = [[NSMenuItem alloc] initWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@"q"】。] [appMenu addItem:quitMenuItem]。 [appMenuItem setSubmenu:appMenu]。 } - (void) buildWnd { #define _wndW 500 #define _wndH 450 window = [[NSWindow alloc] initWithContentRect: NSMakeRect( 0, 0, _wndW, _wndH ) styleMask: NSWindowStyleMaskTitled | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable backing。NSBackingStoreBuffered defer: NO]。 [window center]; [window setTitle: @"Custom View"] 。 [window makeKeyAndOrderFront: nil]。 //**** Create an instance of CustomView (NSView) **** // CustomView *view = [[CustomView alloc]initWithFrame:NSMakeRect( 20, 80, _wndW - 40, _wndH - 100 ) ] 。 [view setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable]。 [[window contentView] addSubview:view]。 //**** Button **** // NSButton *myBtn =[[NSButton alloc]initWithFrame:NSMakeRect( 30, 30, 95, 30 )] 。 [myBtn setBezelStyle:NSBezelStyleRounded ] 。 [myBtn setTitle: @"Beep"]。 [myBtn setAction: @selector (myBtnAction:)]。 [[window contentView] addSubview: myBtn]。 //**** Quit btn **** // NSButton *quitBtn = [[NSButton alloc]initWithFrame:NSMakeRect( _wndW - 50, 10, 40, 40 )] 。 [quitBtn setBezelStyle:NSBezelStyleCircular ] 。 [quitBtn setTitle: @"Q" ] 。 [quitBtn setAutoresizingMask: NSViewMinXMargin]; [quitBtn setAction:@selector(terminate:)] 。 [[window contentView] addSubview: quitBtn]。 } - (void) applicationWillFinishLaunching: (NSNotification *)notification { [self buildMenu]; [self buildWnd]; } - (void) applicationDidFinishLaunching: (NSNotification *)notification { } @end int main() { NSApplication *application = [NSApplication sharedApplication]; AppDelegate *appDelegate = [[AppDelegate alloc] init]; [application setDelegate:appDelegate]; [應用運行]。 return 0。 }轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/320048.html
標籤:
