Get a bounds of WindowRef?
Asked Answered
I

1

5

I am trying to find a Carbon API which can give me the WindowRef from window id and with that windowref I want to have bounds?

EDIT: I found API extern WindowRef HIWindowFromCGWindowID(CGWindowID inWindowID); But I am not able to use it. I have included carbon header and have also added its framework to project. Is there something else required to HI apis?

Any help is appreciated. Thank you for your time.

Ilonailonka answered 6/7, 2011 at 8:44 Comment(0)
I
10

Following should do -

        CGRect rect;
        uint32_t windowid[1] = {windowID};
        CFArrayRef windowArray = CFArrayCreate ( NULL, (const void **)windowid, 1 ,NULL);
        CFArrayRef windowsdescription = CGWindowListCreateDescriptionFromArray(windowArray);
        CFDictionaryRef windowdescription = (CFDictionaryRef)CFArrayGetValueAtIndex ((CFArrayRef)windowsdescription, 0);
        if(CFDictionaryContainsKey(windowdescription, kCGWindowBounds))
        {
            CFDictionaryRef bounds = (CFDictionaryRef)CFDictionaryGetValue (windowdescription, kCGWindowBounds);
            if(bounds)
            {
                CGRectMakeWithDictionaryRepresentation(bounds, &rect);
            }
        }
        CFRelease(windowArray);
Ilonailonka answered 6/7, 2011 at 10:3 Comment(1)
The following line eliminates one of arrays, returning an array with one dictionary in it: CFArrayRef windowArray = CGWindowListCopyWindowInfo(kCGWindowListOptionIncludingWindow, windowID); Also, in your example, the array of descriptions is being leaked (missing CFRelease). Other than that, this fixed my issue. The [NSWindow frame] model is updated asynchronously. If you're trying to attach a child window to a parent window that was/is being dragged around by the user, this is the only way to get the current value from the window manager. Thanks!Jarv

© 2022 - 2024 — McMap. All rights reserved.