如何以编程方式创建Cocoa窗口?

时间:2021-12-23 00:02:03

My Cocoa app needs some small dynamically generated windows. How can I programmatically create Cocoa windows at runtime?

我的Cocoa应用需要一些动态生成的小窗口。如何在运行时以编程方式创建Cocoa窗口?

This is my non-working attempt so far. I see no result whatsoever.

到目前为止,这是我的非工作尝试。我没有看到任何结果。

NSRect frame = NSMakeRect(0, 0, 200, 200);
NSUInteger styleMask =    NSBorderlessWindowMask;
NSRect rect = [NSWindow contentRectForFrameRect:frame styleMask:styleMask];

NSWindow * window =  [[NSWindow alloc] initWithContentRect:rect styleMask:styleMask backing: NSBackingStoreRetained    defer:false];
[window setBackgroundColor:[NSColor blueColor]];
[window display];

4 个解决方案

#1


127  

The problem is that you don't want to call display, you want to call either makeKeyAndOrderFront or orderFront depending on whether or not you want the window to become the key window. You should also probably use NSBackingStoreBuffered.

问题是,您不想调用display,您需要调用makeKeyAndOrderFront或orderFront,这取决于您是否希望窗口成为关键窗口。您还应该使用NSBackingStoreBuffered。

This code will create your borderless, blue window at the bottom left of the screen:

这段代码将在屏幕左下角创建一个无边框的蓝色窗口:

NSRect frame = NSMakeRect(0, 0, 200, 200);
NSWindow* window  = [[[NSWindow alloc] initWithContentRect:frame
                    styleMask:NSBorderlessWindowMask
                    backing:NSBackingStoreBuffered
                    defer:NO] autorelease];
[window setBackgroundColor:[NSColor blueColor]];
[window makeKeyAndOrderFront:NSApp];

//Don't forget to assign window to a strong/retaining property!
//Under ARC, not doing so will cause it to disappear immediately;
//  without ARC, the window will be leaked.

You can make the sender for makeKeyAndOrderFront or orderFront whatever is appropriate for your situation.

您可以为makeKeyAndOrderFront或orderFront发送发送方,以适合您的情况。

#2


35  

A side note, if you want to programatically instantiate the application without a main nib, in the main.m file / you can instantiate the AppDelegate as below. Then in your apps Supporting Files / YourApp.plist Main nib base file / MainWindow.xib delete this entry. Then use Jason Coco's approach to attach the window in your AppDelegates init method.

附带说明,如果您希望以编程方式实例化应用程序,而不需要主要的nib,则在main中。m文件/您可以实例化AppDelegate如下。然后在你的应用程序支持文件/你的应用。plist主nib基础文件/主窗口。xib删除该条目。然后使用Jason Coco的方法将窗口附加到appdelegate init方法中。

#import "AppDelegate.h":

int main(int argc, char *argv[])
{

  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  [NSApplication sharedApplication];

  AppDelegate *appDelegate = [[AppDelegate alloc] init];
  [NSApp setDelegate:appDelegate];
  [NSApp run];
  [pool release];
  return 0;
}

#3


6  

Try

试一试

[window makeKeyAndOrderFront:self]; 

instead of

而不是

[window display];

Is that what you're aiming for?

这就是你的目标吗?

#4


2  

This is what I've come up with myself:

这就是我自己想到的:

NSRect frame = NSMakeRect(100, 100, 200, 200);
NSUInteger styleMask =    NSBorderlessWindowMask;
NSRect rect = [NSWindow contentRectForFrameRect:frame styleMask:styleMask];
NSWindow * window =  [[NSWindow alloc] initWithContentRect:rect styleMask:styleMask backing: NSBackingStoreBuffered    defer:false];
[window setBackgroundColor:[NSColor blueColor]];
[window makeKeyAndOrderFront: window];

This displays a blue window. I hope this is the optimal approach.

这显示了一个蓝色的窗口。我希望这是最优的方法。

#1


127  

The problem is that you don't want to call display, you want to call either makeKeyAndOrderFront or orderFront depending on whether or not you want the window to become the key window. You should also probably use NSBackingStoreBuffered.

问题是,您不想调用display,您需要调用makeKeyAndOrderFront或orderFront,这取决于您是否希望窗口成为关键窗口。您还应该使用NSBackingStoreBuffered。

This code will create your borderless, blue window at the bottom left of the screen:

这段代码将在屏幕左下角创建一个无边框的蓝色窗口:

NSRect frame = NSMakeRect(0, 0, 200, 200);
NSWindow* window  = [[[NSWindow alloc] initWithContentRect:frame
                    styleMask:NSBorderlessWindowMask
                    backing:NSBackingStoreBuffered
                    defer:NO] autorelease];
[window setBackgroundColor:[NSColor blueColor]];
[window makeKeyAndOrderFront:NSApp];

//Don't forget to assign window to a strong/retaining property!
//Under ARC, not doing so will cause it to disappear immediately;
//  without ARC, the window will be leaked.

You can make the sender for makeKeyAndOrderFront or orderFront whatever is appropriate for your situation.

您可以为makeKeyAndOrderFront或orderFront发送发送方,以适合您的情况。

#2


35  

A side note, if you want to programatically instantiate the application without a main nib, in the main.m file / you can instantiate the AppDelegate as below. Then in your apps Supporting Files / YourApp.plist Main nib base file / MainWindow.xib delete this entry. Then use Jason Coco's approach to attach the window in your AppDelegates init method.

附带说明,如果您希望以编程方式实例化应用程序,而不需要主要的nib,则在main中。m文件/您可以实例化AppDelegate如下。然后在你的应用程序支持文件/你的应用。plist主nib基础文件/主窗口。xib删除该条目。然后使用Jason Coco的方法将窗口附加到appdelegate init方法中。

#import "AppDelegate.h":

int main(int argc, char *argv[])
{

  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  [NSApplication sharedApplication];

  AppDelegate *appDelegate = [[AppDelegate alloc] init];
  [NSApp setDelegate:appDelegate];
  [NSApp run];
  [pool release];
  return 0;
}

#3


6  

Try

试一试

[window makeKeyAndOrderFront:self]; 

instead of

而不是

[window display];

Is that what you're aiming for?

这就是你的目标吗?

#4


2  

This is what I've come up with myself:

这就是我自己想到的:

NSRect frame = NSMakeRect(100, 100, 200, 200);
NSUInteger styleMask =    NSBorderlessWindowMask;
NSRect rect = [NSWindow contentRectForFrameRect:frame styleMask:styleMask];
NSWindow * window =  [[NSWindow alloc] initWithContentRect:rect styleMask:styleMask backing: NSBackingStoreBuffered    defer:false];
[window setBackgroundColor:[NSColor blueColor]];
[window makeKeyAndOrderFront: window];

This displays a blue window. I hope this is the optimal approach.

这显示了一个蓝色的窗口。我希望这是最优的方法。