使用 CGWindowListCopyWindowInfo 最前面的窗口
在文档中,它说此方法返回的窗口列表:
CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
将按从前到后的顺序返回窗口。但事实并非如此……最前面的窗口应该是测试的,但这是这个数组中的一个:
2011-03-12 18:18:14.221 test[982:a0f] (
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 22;
Width = 212;
X = 1662;
Y = 0;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 25;
kCGWindowMemoryUsage = 30104;
kCGWindowName = "";
kCGWindowNumber = 14;
kCGWindowOwnerName = SystemUIServer;
kCGWindowOwnerPID = 99;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
},
{
kCGWindowAlpha = 0;
kCGWindowBounds = {
Height = 22;
Width = 1920;
X = 0;
Y = 0;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 25;
kCGWindowMemoryUsage = 5528;
kCGWindowNumber = 15;
kCGWindowOwnerName = SystemUIServer;
kCGWindowOwnerPID = 99;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 22;
Width = 46;
X = 1874;
Y = 0;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 25;
kCGWindowMemoryUsage = 5528;
kCGWindowName = "";
kCGWindowNumber = 12;
kCGWindowOwnerName = SystemUIServer;
kCGWindowOwnerPID = 99;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 22;
Width = 1920;
X = 0;
Y = 0;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 24;
kCGWindowMemoryUsage = 218520;
kCGWindowName = "Shared Menubar";
kCGWindowNumber = 6;
kCGWindowOwnerName = "Window Server";
kCGWindowOwnerPID = 73;
kCGWindowSharingState = 2;
kCGWindowStoreType = 2;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 1058;
Width = 1920;
X = 0;
Y = 22;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 20;
kCGWindowMemoryUsage = 1200;
kCGWindowNumber = 13;
kCGWindowOwnerName = Dock;
kCGWindowOwnerPID = 98;
kCGWindowSharingState = 1;
kCGWindowStoreType = 1;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 52;
Width = 676;
X = 622;
Y = 1028;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 19;
kCGWindowMemoryUsage = 157080;
kCGWindowName = "Magic Mirror";
kCGWindowNumber = 16;
kCGWindowOwnerName = Dock;
kCGWindowOwnerPID = 98;
kCGWindowSharingState = 2;
kCGWindowStoreType = 2;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 382;
Width = 480;
X = 335;
Y = 367;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 0;
kCGWindowMemoryUsage = 792496;
kCGWindowName = test;
kCGWindowNumber = 252;
kCGWindowOwnerName = test;
kCGWindowOwnerPID = 982;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
kCGWindowWorkspace = 1;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 807;
Width = 1206;
X = 321;
Y = 157;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 0;
kCGWindowMemoryUsage = 4055280;
kCGWindowName = "AppDelegate.m - test";
kCGWindowNumber = 24;
kCGWindowOwnerName = Xcode;
kCGWindowOwnerPID = 132;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
kCGWindowWorkspace = 1;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 967;
Width = 960;
X = 175;
Y = 22;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 0;
kCGWindowMemoryUsage = 3817968;
kCGWindowName = "test - Debugger Console";
kCGWindowNumber = 31;
kCGWindowOwnerName = Xcode;
kCGWindowOwnerPID = 132;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
kCGWindowWorkspace = 1;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 967;
Width = 1845;
X = 121;
Y = 22;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 0;
kCGWindowMemoryUsage = 882288;
kCGWindowName = "CocoaDev Forums - get focused window id";
kCGWindowNumber = 80;
kCGWindowOwnerName = "Google Chrome";
kCGWindowOwnerPID = 244;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
kCGWindowWorkspace = 1;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 784;
Width = 885;
X = 560;
Y = 157;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 0;
kCGWindowMemoryUsage = 318768;
kCGWindowName = "Console Messages";
kCGWindowNumber = 125;
kCGWindowOwnerName = Console;
kCGWindowOwnerPID = 482;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
kCGWindowWorkspace = 1;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 494;
Width = 1167;
X = 519;
Y = 417;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 0;
kCGWindowMemoryUsage = 2441264;
kCGWindowName = Debug;
kCGWindowNumber = 54;
kCGWindowOwnerName = Finder;
kCGWindowOwnerPID = 100;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
kCGWindowWorkspace = 1;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 382;
Width = 480;
X = 335;
Y = 367;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 0;
kCGWindowMemoryUsage = 55216;
kCGWindowName = test;
kCGWindowNumber = 223;
kCGWindowOwnerName = test;
kCGWindowOwnerPID = 849;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
kCGWindowWorkspace = 1;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 437;
Width = 770;
X = 396;
Y = 308;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 0;
kCGWindowMemoryUsage = 238512;
kCGWindowName = Applications;
kCGWindowNumber = 19;
kCGWindowOwnerName = Finder;
kCGWindowOwnerPID = 100;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
kCGWindowWorkspace = 1;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 437;
Width = 770;
X = 450;
Y = 429;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 0;
kCGWindowMemoryUsage = 332720;
kCGWindowName = Applications;
kCGWindowNumber = 17;
kCGWindowOwnerName = Finder;
kCGWindowOwnerPID = 100;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
kCGWindowWorkspace = 1;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 14;
Width = 1920;
X = 0;
Y = 22;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = "-20";
kCGWindowMemoryUsage = 5528;
kCGWindowName = "Backstop Menubar";
kCGWindowNumber = 11;
kCGWindowOwnerName = "Window Server";
kCGWindowOwnerPID = 73;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 1080;
Width = 1920;
X = 0;
Y = 0;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = "-2147483627";
kCGWindowMemoryUsage = 1200;
kCGWindowName = "";
kCGWindowNumber = 10;
kCGWindowOwnerName = Finder;
kCGWindowOwnerPID = 100;
kCGWindowSharingState = 1;
kCGWindowStoreType = 1;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 1080;
Width = 1920;
X = 0;
Y = 0;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = "-2147483628";
kCGWindowMemoryUsage = 8328600;
kCGWindowName = Desktop;
kCGWindowNumber = 2;
kCGWindowOwnerName = "Window Server";
kCGWindowOwnerPID = 73;
kCGWindowSharingState = 2;
kCGWindowStoreType = 2;
}
)
什么是 SystemUIServer、dock 和魔镜?这些不是我最喜欢的应用程序。谢谢!
In the documentation, it says that list of windows returned by this method:
CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
will return windows in order from front to back. But it doesn't...The front most window should be test, but here's the out of this array:
2011-03-12 18:18:14.221 test[982:a0f] (
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 22;
Width = 212;
X = 1662;
Y = 0;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 25;
kCGWindowMemoryUsage = 30104;
kCGWindowName = "";
kCGWindowNumber = 14;
kCGWindowOwnerName = SystemUIServer;
kCGWindowOwnerPID = 99;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
},
{
kCGWindowAlpha = 0;
kCGWindowBounds = {
Height = 22;
Width = 1920;
X = 0;
Y = 0;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 25;
kCGWindowMemoryUsage = 5528;
kCGWindowNumber = 15;
kCGWindowOwnerName = SystemUIServer;
kCGWindowOwnerPID = 99;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 22;
Width = 46;
X = 1874;
Y = 0;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 25;
kCGWindowMemoryUsage = 5528;
kCGWindowName = "";
kCGWindowNumber = 12;
kCGWindowOwnerName = SystemUIServer;
kCGWindowOwnerPID = 99;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 22;
Width = 1920;
X = 0;
Y = 0;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 24;
kCGWindowMemoryUsage = 218520;
kCGWindowName = "Shared Menubar";
kCGWindowNumber = 6;
kCGWindowOwnerName = "Window Server";
kCGWindowOwnerPID = 73;
kCGWindowSharingState = 2;
kCGWindowStoreType = 2;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 1058;
Width = 1920;
X = 0;
Y = 22;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 20;
kCGWindowMemoryUsage = 1200;
kCGWindowNumber = 13;
kCGWindowOwnerName = Dock;
kCGWindowOwnerPID = 98;
kCGWindowSharingState = 1;
kCGWindowStoreType = 1;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 52;
Width = 676;
X = 622;
Y = 1028;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 19;
kCGWindowMemoryUsage = 157080;
kCGWindowName = "Magic Mirror";
kCGWindowNumber = 16;
kCGWindowOwnerName = Dock;
kCGWindowOwnerPID = 98;
kCGWindowSharingState = 2;
kCGWindowStoreType = 2;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 382;
Width = 480;
X = 335;
Y = 367;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 0;
kCGWindowMemoryUsage = 792496;
kCGWindowName = test;
kCGWindowNumber = 252;
kCGWindowOwnerName = test;
kCGWindowOwnerPID = 982;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
kCGWindowWorkspace = 1;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 807;
Width = 1206;
X = 321;
Y = 157;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 0;
kCGWindowMemoryUsage = 4055280;
kCGWindowName = "AppDelegate.m - test";
kCGWindowNumber = 24;
kCGWindowOwnerName = Xcode;
kCGWindowOwnerPID = 132;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
kCGWindowWorkspace = 1;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 967;
Width = 960;
X = 175;
Y = 22;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 0;
kCGWindowMemoryUsage = 3817968;
kCGWindowName = "test - Debugger Console";
kCGWindowNumber = 31;
kCGWindowOwnerName = Xcode;
kCGWindowOwnerPID = 132;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
kCGWindowWorkspace = 1;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 967;
Width = 1845;
X = 121;
Y = 22;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 0;
kCGWindowMemoryUsage = 882288;
kCGWindowName = "CocoaDev Forums - get focused window id";
kCGWindowNumber = 80;
kCGWindowOwnerName = "Google Chrome";
kCGWindowOwnerPID = 244;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
kCGWindowWorkspace = 1;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 784;
Width = 885;
X = 560;
Y = 157;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 0;
kCGWindowMemoryUsage = 318768;
kCGWindowName = "Console Messages";
kCGWindowNumber = 125;
kCGWindowOwnerName = Console;
kCGWindowOwnerPID = 482;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
kCGWindowWorkspace = 1;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 494;
Width = 1167;
X = 519;
Y = 417;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 0;
kCGWindowMemoryUsage = 2441264;
kCGWindowName = Debug;
kCGWindowNumber = 54;
kCGWindowOwnerName = Finder;
kCGWindowOwnerPID = 100;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
kCGWindowWorkspace = 1;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 382;
Width = 480;
X = 335;
Y = 367;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 0;
kCGWindowMemoryUsage = 55216;
kCGWindowName = test;
kCGWindowNumber = 223;
kCGWindowOwnerName = test;
kCGWindowOwnerPID = 849;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
kCGWindowWorkspace = 1;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 437;
Width = 770;
X = 396;
Y = 308;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 0;
kCGWindowMemoryUsage = 238512;
kCGWindowName = Applications;
kCGWindowNumber = 19;
kCGWindowOwnerName = Finder;
kCGWindowOwnerPID = 100;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
kCGWindowWorkspace = 1;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 437;
Width = 770;
X = 450;
Y = 429;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = 0;
kCGWindowMemoryUsage = 332720;
kCGWindowName = Applications;
kCGWindowNumber = 17;
kCGWindowOwnerName = Finder;
kCGWindowOwnerPID = 100;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
kCGWindowWorkspace = 1;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 14;
Width = 1920;
X = 0;
Y = 22;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = "-20";
kCGWindowMemoryUsage = 5528;
kCGWindowName = "Backstop Menubar";
kCGWindowNumber = 11;
kCGWindowOwnerName = "Window Server";
kCGWindowOwnerPID = 73;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 1080;
Width = 1920;
X = 0;
Y = 0;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = "-2147483627";
kCGWindowMemoryUsage = 1200;
kCGWindowName = "";
kCGWindowNumber = 10;
kCGWindowOwnerName = Finder;
kCGWindowOwnerPID = 100;
kCGWindowSharingState = 1;
kCGWindowStoreType = 1;
},
{
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 1080;
Width = 1920;
X = 0;
Y = 0;
};
kCGWindowIsOnscreen = 1;
kCGWindowLayer = "-2147483628";
kCGWindowMemoryUsage = 8328600;
kCGWindowName = Desktop;
kCGWindowNumber = 2;
kCGWindowOwnerName = "Window Server";
kCGWindowOwnerPID = 73;
kCGWindowSharingState = 2;
kCGWindowStoreType = 2;
}
)
What's SystemUIServer, dock and magic mirror? These aren't my front most apps. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
获取 Apple 示例应用程序的副本:Son Of Grab。然后构建并运行它,您将拥有一个非常方便的交互式应用程序,用于调查窗口服务器已知的所有窗口。
您的问题的答案是窗口是从前到后排序的,但是定义为窗口的用户界面元素比您预期的要多得多。这包括菜单栏、停靠栏、服务菜单、桌面等。这些是在不同级别定义的,对应于输出中的
kCGWindowLayer
。您希望在标准窗口级别(零)定义第一个窗口。如果您查看输出,会发现第一个带有
kCGWindowLayer == 0
的窗口实际上是第七个窗口。Grab a copy of Apple's sample app: Son Of Grab. Then build and run it, and you'll have a very handy interactive app for investigating all the windows known to the window server.
The answer to your question is that windows are ordered front to back, but there are far more user interface elements defined as windows than you are expecting. This includes the menubar, the dock, the services menu, the Desktop, etc. These are defined at different levels, corresponding to
kCGWindowLayer
in your output.You want the first window defined at the standard window level (zero). And if you look at your output, the first window with
kCGWindowLayer == 0
is in fact the 7th.SystemUIServer是顶部菜单栏。 Dock就是Dock(通常在屏幕的底部)。我对“魔镜”不熟悉。您可以使用 lsregister 在您的系统上查找它:
在输出中搜索“Magic Mirror”。
SystemUIServer is the top menu bar. Dock is the Dock (usually at the bottom of the screen). I'm not familiar with "Magic Mirror." You can look for it on your system with
lsregister
:Search the output for "Magic Mirror".