确定在macOS上哪个窗口处于打开状态 [英] Determine which display a window is on in macOS

查看:79
本文介绍了确定在macOS上哪个窗口处于打开状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个窗口的CGWindowID和我的Mac的所有CGDirectDisplayID.然后,我想知道显示在哪个窗口上.我尝试获取Window的CGWindowInfo,但是找不到有用的信息.

I have the CGWindowID of one window and all CGDirectDisplayIDs of my Mac. Then I want to know which display the window on. I try to get the CGWindowInfo of the Window,but can`t find useful informations.

CFArrayRef windowList =  CGWindowListCopyWindowInfo(kCGWindowListOptionIncludingWindow, windowID);
CFArrayApplyFunction(windowList, CFRangeMake(0, CFArrayGetCount(windowList)), &WindowListApplierFunction, this);
CFRelease(windowList);

推荐答案

您可以为此使用NSScreen API.使用 [NSScreen屏幕] 检索计算机连接的屏幕,然后匹配 [myWindow屏幕] 返回的屏幕.

You can use the NSScreen API for that. Use [NSScreen screens] to retrieve the screens your computer is connected to and then match the screen returned by [myWindow screen].

如果您拥有要在哪个屏幕上知道的窗口,请执行以下操作:

If you own the window that you want to know on which screen is at just do the following:

CGWindowID windowID = ... // init with your value
NSWindow *window = [NSApplication windowWithWindowNumber:(NSInteger)windowID];

if ([[NSScreen screens] count] > 1)
{
    // you have more than one screen attached
    NSScreen *currentScreen = [window screen];

    // you can then test if the window is on the main display
    if (currentScreen == [NSScreen mainScreen])
    {
        // your window is on the main screen
    }
    else
    {
       // your window is not on the main screen
    }
}

但是,如果您不拥有该窗口,因为它是另一个应用程序所拥有的,那么我建议您首先了解 NSScreen 使用的Quartz坐标系与Core Graphics之间的区别.CGWindow API使用的坐标系.这里有一篇很好的文章(英文):

However, if you don't own the window, because it is owned by another app, then I suggest that you first understand the differences between the Quartz coordinate system used by NSScreen and the Core Graphics coordinate system used by the CGWindow API. There is a good article about this here (English):

http://www.thinkandbuild.it/deal-with-多屏编程/

和此处(日语)(如果您不懂日语,请使用Google翻译):

and here (Japanese) (use Google translator if you don't know Japanese):

http://xcatsan.blogspot.com/2009/09/nsscreen-cgwindow.html

第二,您需要按照我推荐的Son of Grab示例代码的说明或此处的说明来检索窗口边界:

Second, you need to retrieve the window bounds as explained by Son of Grab sample code I recommended or as explained here:

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

然后您需要按照建议的窗口边界计算它所在的屏幕.

Then you need to calculate the screen where it is at by the window bounds as suggested.

这篇关于确定在macOS上哪个窗口处于打开状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆