MacOs active window title using C#

萝らか妹 提交于 2020-01-06 07:42:27

问题


So I have a C# application using Mono and Gtk+2 running on Mac Is there some way to get active application window title?

https://stackoverflow.com/a/37368813/3794943 says that I need CGWindowListCopyWindowInfo (I already have the rest of their recipe, like process identifier, front most application, etc.).

Where can I get CGWindowListCopyWindowInfo or something similar from? Or is there some other way to get active window title when using mono on mac os?


回答1:


Ok, finally found it here: https://forums.xamarin.com/discussion/comment/95429/#Comment_95429

At first you import that function like this:

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

Then you call it like this:

    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. MonoMac package must be added using NuGet



来源:https://stackoverflow.com/questions/52441936/macos-active-window-title-using-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!