使用 C# 的 MacOs active window title

MacOs active window title using C#

所以我在 Mac 上有一个使用 Mono 和 Gtk+2 运行 的 C# 应用程序 有什么方法可以让应用程序获得 window 标题?

说我需要 CGWindowListCopyWindowInfo(我已经有了他们的其余方法,例如进程标识符、前端 most 应用程序等)。

我从哪里可以得到 CGWindowListCopyWindowInfo 或类似的东西?或者在 mac os 上使用单声道时是否有其他方法可以激活 window 标题?

好的,终于在这里找到了:https://forums.xamarin.com/discussion/comment/95429/#Comment_95429

首先你像这样导入那个函数:

[DllImport(@"/System/Library/Frameworks/QuartzCore.framework/QuartzCore")]
static extern IntPtr CGWindowListCopyWindowInfo(CGWindowListOption option, uint relativeToWindow);

然后你这样称呼它:

    string result = null;
    IntPtr windowInfo = CGWindowListCopyWindowInfo(CGWindowListOption.OnScreenOnly, 0);
    NSArray values = (MonoMac.Foundation.NSArray)Runtime.GetNSObject(windowInfo);

    for (ulong i = 0, len = values.Count; i < len; i++)
    {
        NSObject window = Runtime.GetNSObject(values.ValueAt(i));

        NSString key = new NSString("kCGWindowOwnerPID");
        NSNumber value = (MonoMac.Foundation.NSNumber)window.ValueForKey(key);
        // and so on
    }

P.S。必须使用 NuGet

添加 MonoMac 包