Cocoa在第二个屏幕上打开一个全屏窗口,保持应用在第一个屏幕上可见 [英] Cocoa Open a fullscreen window on the second screen maintaining the app visible on the first
问题描述
我在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)];
//添加测试按钮
NSButton * testButton = [[NSButton alloc] initWithFrame(50.0f,50.0f,100.0f,50.0f)];
[testButton setTarget:self];
[testButton setAction:@selector(closeExternalWindow)];
[fullScreenView addSubview:testButton];
//显示全屏窗口
[fullScreenWindow.contentView addSubview:fullScreenView];
[fullScreenWindow makeKeyAndOrderFront:self];
这样,在第一个屏幕上,应用程序才会正确显示,
谢谢!
阅读Apple的文档 initWithContentRect:styleMask:backing:defer:screen:
> screen 参数..
指定绘制窗口的内容矩形除了主屏幕之外的屏幕。内容
矩形是相对于屏幕左下角绘制的。
code> [screen frame] 你实际上是把它移到第二个屏幕上,因为定位是相对于那个屏幕。
为了使它出现在期望的地方,你可以改变代码eg到
[[NSWindow alloc] initWithContentRect:NSMakeRect(0,0,[screen frame] .size.width,[screen frame ] .size.height)
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO
screen:screen];
I'm developing an app on OSX 10.7 and I'm trying and the goal is to open some images on a second screen while the app has to run normally on the first.
So the code is the following:
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];
In this way, on the first screen the app is correctly shown, but on the second screen I just see a fullscreen black window.
What's the issue?
Thanks!
Reading Apple's docs for initWithContentRect:styleMask:backing:defer:screen:
it states that the screen parameter..
Specifies where the window’s content rectangle is drawn if the window is to be drawn in a screen other than the main screen. The content rectangle is drawn relative to the bottom-left corner of screen.
So when using [screen frame]
you're actually moving it off the second screen as the positioning is relative to that screen already.
In order to make it appear where expected you can change the code e.g. to
[[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, [screen frame].size.width, [screen frame].size.height)
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO
screen:screen];
这篇关于Cocoa在第二个屏幕上打开一个全屏窗口,保持应用在第一个屏幕上可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!