我有以下代码来检测当前窗口。如何获得1)应用程序内部名称、2)位置、3)发布者和4)窗口/应用程序的描述?
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//Get info about the currently active application.
NSWorkspace* workspace = [NSWorkspace sharedWorkspace];
NSDictionary* currentAppInfo = [workspace activeApplication];
//Get the PSN of the current application.
UInt32 lowLong = [[currentAppInfo objectForKey:@"NSApplicationProcessSerialNumberLow"] longValue];
UInt32 highLong = [[currentAppInfo objectForKey:@"NSApplicationProcessSerialNumberHigh"] longValue];
ProcessSerialNumber currentAppPSN = {highLong,lowLong};
//Grab window information from the window server.
CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
ProcessSerialNumber myPSN = {kNoProcess, kNoProcess};
//Loop through the windows, the window list is ordered from front to back.
for (NSMutableDictionary* entry in (NSArray*) windowList)
{
int pid = [[entry objectForKey:(id)kCGWindowOwnerPID] intValue];
GetProcessForPID(pid, &myPSN);
//If the process of the current window in the list matches our process, get the front window number.
if(myPSN.lowLongOfPSN == currentAppPSN.lowLongOfPSN && myPSN.highLongOfPSN == currentAppPSN.highLongOfPSN)
{
NSNumber *windowNumber = [entry objectForKey:(id)kCGWindowNumber];
windowNumber = [entry objectForKey:(id)kCGWindowNumber];
NSString* applicationName = [entry objectForKey:(id)kCGWindowOwnerName];
NSLog(@"Capture the window: %@ with window ID: %@.",applicationName,windowNumber);
return applicationName;
//Break because we only want the front window.
break;
}
}
CFRelease(windowList);
[pool release];发布于 2009-11-18 17:23:15
您应该使用Process Manager API中的ProcessInformationCopyDictionary函数。给它&myPSN和kProcessDictionaryIncludeAllInformationMask作为参数,你就会得到你想要的信息。
发布于 2010-04-13 17:46:52
我在找一些和这个话题相关的东西。我需要一个窗口或窗口部件在某个位置(鼠标位置)的WindowRef,它必须覆盖所有正在运行的应用程序的所有窗口…
我用Carbon试过了(因为我的应用完全是用C++写的),但我发现一些Carbon功能不能正常工作(MacFindWindow,FindWindow,HIWindowFindAtLocation,FindWindowOfClass,HIWindowGetCGWindowID……)
也许我做错了,很难相信这些碳函数在64位架构中不再起作用……
所以,关于你的问题,我找到了相同的代码,我试过了,但它不是我需要的,我希望它能以任何方式帮助你,我会继续搜索和尝试,直到我得到它(如果操作系统可以做到这一点,每个人都应该)。
//if the process of the current window in the list matches our process, get the front window number
if(myPSN.lowLongOfPSN == currentAppPSN.lowLongOfPSN && myPSN.highLongOfPSN == currentAppPSN.highLongOfPSN)
{
NSNumber* windowNumber = [entry objectForKey:(id)kCGWindowNumber];
NSString* applicationName = [entry objectForKey:(id)kCGWindowOwnerName];
NSLog(@"The current app is %@ and the window number of its front window is %@.",applicationName,windowNumber);
CGRect bounds;
CGRectMakeWithDictionaryRepresentation((CFDictionaryRef)[entry objectForKey:(id)kCGWindowBounds], &bounds);
NSLog(@"WINDOW RECT BOUNDS; (x,y,width, height) = (%d,%d, %d, %d)", bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
break;
}另外,点击这个链接,我不会帮你的。我敢肯定:
http://code.google.com/p/blazingstars/source/browse/trunk/PokerHK/HKLowLevel.m?r=70
https://stackoverflow.com/questions/1754160
复制相似问题