我正在开发一个简单的 Mac 应用程序,该应用程序检测何时拔出外部屏幕,保存所有窗口的位置,一旦再次插入外部屏幕,将所有窗口恢复到其原始位置。(我知道那里已经有应用程序,我只是好奇这是如何完成的)
经过大量搜索,我终于设法通过使用将所有窗口都显示在屏幕上
NSArray *openWindows = [[NSWorkspace sharedWorkspace] runningApplications];
CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
这将返回类似
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 22;
Width = 279;
X = 1559;
Y = 0;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 25;
kCGWindowMemoryUsage = 13596;
kCGWindowName = "";
kCGWindowNumber = 18;
kCGWindowOwnerName = SystemUIServer;
kCGWindowOwnerPID = 260;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
},
{
kCGWindowAlpha = 0;
kCGWindowBounds = {
Height = 22;
Width = 1920;
X = 0;
Y = 0;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 25;
kCGWindowMemoryUsage = 5404;
kCGWindowNumber = 19;
kCGWindowOwnerName = SystemUIServer;
kCGWindowOwnerPID = 260;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
},
然后我会遍历数组并查看每个单独的窗口
for (int i = 0; i < CFArrayGetCount(windowList); i++) {
CFDictionaryRef ref = CFArrayGetValueAtIndex(windowList, i);
NSLog(@"%@", CFDictionaryGetValue(ref, kCGWindowBounds));
}
但这就是我卡住的地方,当使用多个屏幕时,我首先如何知道窗口在哪个屏幕上。其次,我以后如何调整窗口边界?每个应用程序都有自己的 ID 吗?或者我可以使用另一种方法吗?