NSWindow

代码示例

NSRect frame = CGRectMake(0, 0, 200, 200);
NSUInteger style =  NSTitledWindowMask | NSClosableWindowMask |NSMiniaturizableWindowMask | NSResizableWindowMask;
NSWindow *window = [NSWindow alloc]initWithContentRect:frame styleMask:style backing:NSBackingStoreBuffered defer:YES];
window.title = @"New Create Window";
//窗口显示
[window makeKeyAndOrderFront:self];
//窗口居中
[window center];”

styleMask:窗口风格

enum {
   NSBorderlessWindowMask = 0,             //没有顶部titlebar边框
   NSTitledWindowMask = 1 << 0,            //有顶部titlebar边框
   NSClosableWindowMask = 1 << 1,          //带有关闭按钮
   NSMiniaturizableWindowMask = 1 << 2,    //带有最小化按钮
   NSResizableWindowMask = 1 << 3,         //恢复按钮
   NSTexturedBackgroundWindowMask = 1 << 8 //带纹理背景的window
};

backing:窗口绘制的缓存模式

enum {
   NSBackingStoreRetained     = 0, //兼容老系统参数,基本很少用到
   NSBackingStoreNonretained  = 1, //不缓存直接绘制
   NSBackingStoreBuffered     = 2  //缓存绘制
};

窗口通知

NSWindowDidBecomeKeyNotification: 窗口成为keyWindow
NSWindowDidBecomeMainNotification:窗口成为mainWindow
NSWindowDidMoveNotification:      窗口移动
NSWindowDidResignKeyNotification: 窗口不再是keyWindow
NSWindowDidResignMainNotification:窗口不再是mainWindow
NSWindowDidResizeNotification:    窗口大小改变
NSWindowWillCloseNotification:    关闭窗口

NSWindow 中的 contentView

关闭窗口时退出程序

方法一:
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)application {
    return YES;
}
方法二:利用 NSWindowDelegate
- (void)windowWillClose:(NSNotification *)notification
{
    NSWindow *window = notification.object;
    if(window == self.window) {
        [NSApp terminate:self];
       // [[NSApplication sharedApplication] terminate:nil];  //或这句也行
    }
}

设置Window的image和title

- (void)setWindowIcon {
    [self.window setRepresentedURL:[NSURL URLWithString:@"WindowTitle"]];
    [self.window setTitle:@"SQLiteApp"];
    NSImage *image = [NSImage imageNamed:@"windowIcon"];
    [[self.window standardWindowButton:NSWindowDocumentIconButton] setImage:image];
}

你可能感兴趣的:(NSWindow)