可可第二屏幕上打开一个全屏窗口保持应用程序的第一个可见(Cocoa Open a fullscree

2019-07-21 15:06发布

我开发的OSX 10.7的应用程序,我想,目标是到第二个屏幕上打开一些图像,同时应用必须在第一正常运行。

因此,代码如下:

NSScreen *screen = [[NSScreen screens] objectAtIndex:1];

fullScreenWindow = [[NSWindow alloc] initWithContentRect:[screenFrame]
                                               styleMask:NSBorderlessWindowMask
                                                 backing:NSBackingStoreBuffered
                                                   defer:NO
                                                  screen:screen];
[fullScreenWindow setLevel: NSMainMenuWIndowLevel + 1];
[fullScreenWindow setOpaque: YES];
[fullScreenWindow setBackgroundColor:[NSColor yellowColor]];

fullScreenView = [[NSView alloc] initWithFrame:NSMakeRect(0.0f, 0.0f, fullScreenWindow.frame.size.width, fullScreenWindow.frame.size.height)];
// Adding a test button
NSButton *testButton = [[NSButton alloc] initWithFrame(50.0f, 50.0f, 100.0f, 50.0f)];
[testButton setTarget:self];
[testButton setAction:@selector(closeExternalWindow)];
[fullScreenView addSubview:testButton];

// Present the fullscreen window
[fullScreenWindow.contentView addSubview:fullScreenView];
[fullScreenWindow makeKeyAndOrderFront:self];

通过这种方式,第一屏幕上的应用程序显示正确,但第二个屏幕上我只看到一个全屏黑色的窗口。

有什么问题?

谢谢!

Answer 1:

阅读苹果的文档initWithContentRect:styleMask:backing:defer:screen:它指出参数..

指定其中如果窗口比主画面以外的画面绘制窗口的内容绘制矩形。 内容矩形相对于屏幕的左下角绘制。

因此,当使用[screen frame]你其实移动它关闭第二个屏幕的定位是相对于屏幕了。

为了让它出现在预期的位置,您可以更改代码如以

[[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, [screen frame].size.width, [screen frame].size.height)
                            styleMask:NSBorderlessWindowMask
                              backing:NSBackingStoreBuffered
                                defer:NO
                               screen:screen];


Answer 2:

几天以后,我不得不对这个代码重新工作的可能性,而现在它的工作原理:

NSScreen *screen = [[NSScreen screens] objectAtIndex:1];
NSRect mainDisplayRect = [screen frame];
fullScreenWindow = [[NSWindow alloc] initWithContentRect: mainDisplayRect styleMask:NSBorderlessWindowMask
                                                 backing:NSBackingStoreBuffered defer:YES];
[fullScreenWindow setLevel:NSMainMenuWindowLevel+1];
[fullScreenWindow setOpaque:YES];
[fullScreenWindow setHidesOnDeactivate:YES];
[fullScreenWindow setBackgroundColor:[NSColor redColor]];

NSRect viewRect = NSMakeRect(0.0, 0.0, mainDisplayRect.size.width, mainDisplayRect.size.height);
fullScreenView = [[PresenterView alloc] initWithFrame:viewRect];
[fullScreenWindow setContentView: fullScreenView];
[fullScreenWindow makeKeyAndOrderFront:self];

周杰伦感谢大家的支持;)



文章来源: Cocoa Open a fullscreen window on the second screen maintaining the app visible on the first