使用C#的MacOs活动窗口标题 [英] MacOs active window title using C#
问题描述
所以我有一个在Mac上运行的使用Mono和Gtk + 2的C#应用程序 有什么方法可以获取活动的应用程序窗口标题?
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 说我需要CGWindowListCopyWindowInfo(我已经有了其余的配方,例如进程标识符,最前面的应用程序等).
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.).
我从哪里可以得到 CGWindowListCopyWindowInfo 或类似的东西?还是在Mac OS上使用Mono时有其他方法来获取活动窗口标题吗?
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?
推荐答案
好,终于在这里找到了它: https://forums.xamarin.com/discussion/comment/95429/#Comment_95429
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);
然后您这样称呼它:
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软件包
P.S. MonoMac package must be added using NuGet
这篇关于使用C#的MacOs活动窗口标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!