2009-11-18 32 views
2

我有以下代碼來檢測當前窗口。我如何獲得1)應用程序內部名稱,2)位置,3)發佈者和4)窗口/應用程序的描述?如何使用Objective-C獲取應用程序的詳細信息?

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]; 

回答

1

您應該使用Process Manager API中的ProcessInformationCopyDictionary函數。給它&myPSNkProcessDictionaryIncludeAllInformationMask作爲參數,你會得到你正在尋找的信息。

0

我正在尋找與此主題相關的內容。我需要一個WindowRef的窗口或窗口部分在一定的位置(鼠標位置),它必須在所有正在運行的應用程序的所有窗口... ...

我試過用碳('我的應用程序是完全用C++編寫的),但我發現一些碳功能不能正常工作(MacFindWindow,FindWindow,HIWindowFindAtLocation,FindWindowOfClass,HIWindowGetCGWindowID ...)

也許我做錯了,這很難相信那些碳功能在64位體系結構中不再有用...

因此,與您的問題相關,我找到了相同的代碼,我嘗試了這一點,但這不是我所需要的,我希望它幫助喲ü以任何方式,我會繼續搜索並嘗試,直到我得到它(如果O.S可以做到每個人都應該)。

//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

相關問題