Cocoa Open a fullscreen window on the second screen maintaining the app visible on the first

元气小坏坏 提交于 2019-12-01 12:04:22

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];

After some days I have had the possibility to work again on this code and now it works:

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];

Jay thank you for the support ;)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!