I was having trouble focusing window of another process id.
I had issue when it was minimized, i fixed that by testing IsIconic
then doing ShowWindow
. But now if the process is not the foreground process it doesn't show.
SetForeground documentation says:
Any ideas on how to bring process to foreground?
I saw this method:
EnumWindows((WNDENUMPROC)topenumfunc, processid) ;
However this enumerates ALL windows and does callback testing if current windows pid is equal to that of processid
which was supplied to the EnumWindows
func. I just want to run on a single window.
This is my FocusWindow
function:
Cu.import('resource://gre/modules/ctypes.jsm');
var user32 = ctypes.open('user32.dll');
/* http://msdn.microsoft.com/en-us/library/ms633539%28v=vs.85%29.aspx
* BOOL WINAPI SetForegroundWindow(
* __in HWND hWnd
* );
*/
var SetForegroundWindow = user32.declare('SetForegroundWindow', ctypes.winapi_abi, ctypes.bool,
ctypes.int32_t
);
/* http://msdn.microsoft.com/en-us/library/windows/desktop/ms633522%28v=vs.85%29.aspx
* DWORD WINAPI GetWindowThreadProcessId(
* __in_ HWND hWnd,
* __out_opt_ LPDWORD lpdwProcessId
* );
*/
var GetWindowThreadProcessId = user32.declare('GetWindowThreadProcessId', ctypes.winapi_abi, ctypes.unsigned_long, //DWORD
ctypes.int32_t, //HWND
ctypes.unsigned_long.ptr //LPDWORD
);
/* http://msdn.microsoft.com/en-us/library/windows/desktop/ms633507%28v=vs.85%29.aspx
* BOOL WINAPI IsIconic(
* __in HWND hWnd
* );
*/
var IsIconic = user32.declare('IsIconic', ctypes.winapi_abi, ctypes.bool, // BOOL
ctypes.int32_t // HWND
);
/* http://msdn.microsoft.com/en-us/library/windows/desktop/ms633507%28v=vs.85%29.aspx
* BOOL WINAPI ShowWindow(
* __in HWND hWnd
* __in INT nCmdShow
* );
*/
var ShowWindow = user32.declare('ShowWindow', ctypes.winapi_abi, ctypes.bool, // BOOL
ctypes.int32_t, // HWND
ctypes.int // INT
);
var SW_RESTORE = 9;
function FocusWindow(hwnd) {
if (IsIconic(hwnd)) {
console.warn('its minimized so un-minimize it');
//its minimized so unminimize it
var rez = ShowWindow(hwnd, SW_RESTORE);
if (!rez) {
throw new Error('Failed to un-minimize window');
}
}
var rez = SetForegroundWindow(hwnd);
if (!rez) {
console.log('could not set to foreground window for a reason other than minimized, maybe process is not foreground, lets try that now');
var cPid = ctypes.cast(ctypes.voidptr_t(0), ctypes.unsigned_long);
var rez = GetWindowThreadProcessId(hwnd, cPid.address());
if (!rez) {
throw new Error('Failed to get PID');
} else {
console.log('cPid=', cPid, uneval(cPid), cPid.toString());
console.log('trying to set pid to foreground process');
return false;
}
} else {
return rez;
}
}