我正在尝试在所有屏幕上显示窗口副本。请参阅下面的代码。它只在主屏幕上正确显示无边框窗口。我没有在任何地方看到其他窗口。
该窗口应该从左侧显示200px,从顶侧显示200px。
我将origin.x设置为屏幕高度 - 300(= 200px间距+窗口高度100px)。
知道我做错了吗?
- (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];
}
答案 0 :(得分:2)
NSWindow有一个特殊的初始化程序来指定你想要它的屏幕(你实际上只关闭了一个参数!):
initWithContentRect:styleMask:backing:defer:screen:
初始化程序的另一种形式假设是主屏幕,这就是为什么它们堆积如此。