2013-03-05 144 views
1

我试图在所有屏幕上显示窗口副本。看到我的代码如下。它只在主屏幕上正确显示无边框窗口。我没有在任何地方看到其他窗口。所有屏幕上的显示窗口

该窗口应该显示从左边200px和从顶边200px。

我将origin.x设置为屏幕高度--300(= 200px间距+窗口本身的高度)。

任何想法我做错了什么?

- (void)displayOnAllScreens 
{   
    NSArray *screenArray = [NSScreen screens]; 

    _tempWindows = [[NSMutableArray alloc] init]; 

    if ([screenArray count] == 1) { 
     [self displayOnScreen:[NSScreen mainScreen]]; 
    } else { 
     for (int i=0; i<[screenArray count]; i++) { 
      [self displayOnScreen:[screenArray objectAtIndex:i]]; 
     } 
    } 
} 

- (void)displayOnScreen:(NSScreen *)screen 
{ 
    BOOL isMainScreen = NO; 

    if (screen == [NSScreen mainScreen]) { 
     isMainScreen = YES; 
    } 

    NSRect screenRect = [screen frame]; 

    NSRect frame = NSMakeRect(screenRect.origin.x + 200, screenRect.size.height - 300, 100, 100); 

    NSWindow *_tempWindow; 

    _tempWindow = [[NSWindow alloc] initWithContentRect:frame 
                styleMask:NSBorderlessWindowMask 
                 backing:NSBackingStoreBuffered 
                 defer:NO]; 

    if (isMainScreen) { 
     [_tempWindow setBackgroundColor:[NSColor lightGrayColor]]; 
    } else { 
     [_tempWindow setBackgroundColor:[NSColor redColor]]; 
    } 
     [_tempWindow makeKeyAndOrderFront:NSApp]; 
    [_tempWindow setAlphaValue:0.93]; 

    [_tempWindow setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces]; 

    [_tempWindows addObject:_tempWindow]; 
} 

回答

2

NSWindow有一个特殊的初始化,指定你想要哪个屏幕上(你实际上只有一个参数了!):

initWithContentRect:styleMask:backing:defer:screen: 

初始化的另一种形式呈现在主屏幕上,这就是为什么他们这样堆积起来。

+0

谢谢,这是诀窍。我还需要将screenRect.origin设置为NSZeropoint,因为我现在不再需要相对于主屏幕的原点。 – Wesley 2013-03-05 14:17:42

+0

零像每个屏幕的中心,或像窗口的原点零? – CodaFi 2013-03-05 14:24:19

+0

对不起,您可能认为这是个问题。这是一个答案。我将screenRect.origin设置为NSZeroPoint,并使其在每个屏幕上正确定位。 – Wesley 2013-03-05 14:37:42