根据给定的窗口 ID 确定窗口位于哪个屏幕上
假设我有窗口 ID,有没有办法获取另一个进程的窗口的 NSScreen
?
我非常接近以下代码,但信息字典没有说明窗口位于哪个屏幕上。
CFArrayRef windowArray = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
NSArray* windowList = (NSArray*)CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
NSUInteger count = [windowList count];
for (NSUInteger i = 0; i < count; i++)
{
NSDictionary* nswindowsdescription = [windowList objectAtIndex:i];
NSNumber* windowid = (NSNumber*)[nswindowsdescription objectForKey:@"kCGWindowNumber"];
if(windowid)
{
if ([windowid integerValue] == appWeAreLookingForWindowId)
{
Warning(@"Found it: %@", nswindowsdescription);
}
}
}
CFRelease(windowArray);
来自: CGWindowListCopyWindowInfo、kCGWindowLayer 和窗口级别
这里的输出:
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 1010;
Width = 1600;
X = 284;
Y = 43;
};
kCGWindowIsOnscreen = 1; // This is not the screen id, by the way
kCGWindowLayer = 0;
kCGWindowMemoryUsage = 7709736;
kCGWindowName = "osx - CGWindowListCopyWindowInfo, kCGWindowLayer and window level - Stack Overflow";
kCGWindowNumber = 4000;
kCGWindowOwnerName = Safari;
kCGWindowOwnerPID = 240;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
kCGWindowWorkspace = 1;
我可以尝试计算从窗口边界屏幕,但我想知道是否有更好的方法。
Is there a way to get the NSScreen
for a window of another process assuming that I have the window id?
I got pretty close with the following code, but the info dictionary doesn't say which screen the window is on.
CFArrayRef windowArray = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
NSArray* windowList = (NSArray*)CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
NSUInteger count = [windowList count];
for (NSUInteger i = 0; i < count; i++)
{
NSDictionary* nswindowsdescription = [windowList objectAtIndex:i];
NSNumber* windowid = (NSNumber*)[nswindowsdescription objectForKey:@"kCGWindowNumber"];
if(windowid)
{
if ([windowid integerValue] == appWeAreLookingForWindowId)
{
Warning(@"Found it: %@", nswindowsdescription);
}
}
}
CFRelease(windowArray);
From: CGWindowListCopyWindowInfo, kCGWindowLayer and window level
Here the output:
kCGWindowAlpha = 1;
kCGWindowBounds = {
Height = 1010;
Width = 1600;
X = 284;
Y = 43;
};
kCGWindowIsOnscreen = 1; // This is not the screen id, by the way
kCGWindowLayer = 0;
kCGWindowMemoryUsage = 7709736;
kCGWindowName = "osx - CGWindowListCopyWindowInfo, kCGWindowLayer and window level - Stack Overflow";
kCGWindowNumber = 4000;
kCGWindowOwnerName = Safari;
kCGWindowOwnerPID = 240;
kCGWindowSharingState = 1;
kCGWindowStoreType = 2;
kCGWindowWorkspace = 1;
I could try to calculate the screen from the window bounds, but I'm wondering if there's a better way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此信息不可用的原因可能是因为一个窗口可以同时出现在多个屏幕上。如果用户拖动窗口,使其一部分位于一个窗口中,其余部分位于另一个窗口中,则不存在当前屏幕这样的东西。
您的窗口边界估计方法可能是最好的选择。
The reason this information is not available is probably because a window can be on more than one screen at once. If the user has dragged the window so that a portion of it is in one window and the rest in another, there is no such thing as the current screen.
Your window bounds estimation method is probably the best option.