I'm not a Carbon developer, so it took huge amount of time to find simple way how to get active window handle in Windows API it's simple as few commands and there is so much forums where you can find help for that. Don't know why, but mostly people are searching window title. My task was find window geometry on Qt. This example includes how to do this, but can be also used for retrieving other information. Still don't know how to cast AXUIElementRef to WId, so if someone have a clue let me know in comments.
Example:
QRect getActiveWindowGeometry()
{
QRect rect;
ProcessSerialNumber psn = { 0L, 0L };
OSStatus err = GetFrontProcess(&psn); // get front process PSN
/*error check*/
pid_t *pidt;
GetProcessPID(&psn ,pidt); // get Pid from ProcessSerialNumber
AXUIElementRef theApp = AXUIElementCreateApplication(*pidt); //get Application from Pid
AXUIElementRef focusWindow;
AXValueRef value;
CGPoint xy;
CGSize wh;
//get focused window from Carbon Application
AXUIElementCopyAttributeValue(theApp, kAXFocusedWindowAttribute, (CFTypeRef *)&focusWindow);
//Now we retrieve Position
AXUIElementCopyAttributeValue(focusWindow, kAXPositionAttribute, (CFTypeRef *)&value);
AXValueGetValue(value, kAXValueCGPointType, &xy);
rect.setX(xy.x);
rect.setY(xy.y);
//Now we retrieve Size
AXUIElementCopyAttributeValue(focusWindow, kAXSizeAttribute, (CFTypeRef *)&value);
AXValueGetValue(value, kAXValueCGSizeType, &wh);
rect.setWidth(wh.width);
rect.setHeight(wh.height);
delete pidt;
return rect;
}
I hope this post will save time I've spend to find it :) Good luck
Example:
QRect getActiveWindowGeometry()
{
QRect rect;
ProcessSerialNumber psn = { 0L, 0L };
OSStatus err = GetFrontProcess(&psn); // get front process PSN
/*error check*/
pid_t *pidt;
GetProcessPID(&psn ,pidt); // get Pid from ProcessSerialNumber
AXUIElementRef theApp = AXUIElementCreateApplication(*pidt); //get Application from Pid
AXUIElementRef focusWindow;
AXValueRef value;
CGPoint xy;
CGSize wh;
//get focused window from Carbon Application
AXUIElementCopyAttributeValue(theApp, kAXFocusedWindowAttribute, (CFTypeRef *)&focusWindow);
//Now we retrieve Position
AXUIElementCopyAttributeValue(focusWindow, kAXPositionAttribute, (CFTypeRef *)&value);
AXValueGetValue(value, kAXValueCGPointType, &xy);
rect.setX(xy.x);
rect.setY(xy.y);
//Now we retrieve Size
AXUIElementCopyAttributeValue(focusWindow, kAXSizeAttribute, (CFTypeRef *)&value);
AXValueGetValue(value, kAXValueCGSizeType, &wh);
rect.setWidth(wh.width);
rect.setHeight(wh.height);
delete pidt;
return rect;
}
I hope this post will save time I've spend to find it :) Good luck
Comments