根据给定的窗口 ID 确定窗口位于哪个屏幕上

发布于 2024-12-11 11:39:58 字数 1523 浏览 0 评论 0原文

假设我有窗口 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隔纱相望 2024-12-18 11:39:58

此信息不可用的原因可能是因为一个窗口可以同时出现在多个屏幕上。如果用户拖动窗口,使其一部分位于一个窗口中,其余部分位于另一个窗口中,则不存在当前屏幕这样的东西。

您的窗口边界估计方法可能是最好的选择。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文