js-ctypes聚焦窗口有问题

时间:2014-08-10 07:28:49

标签: firefox-addon jsctypes

我无法专注于另一个进程ID的窗口。

我在最小化时遇到问题,我通过测试IsIconic然后执行ShowWindow来解决这个问题。但是现在如果进程不是前台进程则不显示。

SetForeground文档说:

关于如何将流程带到前台的任何想法?

我看到了这个方法:

EnumWindows((WNDENUMPROC)topenumfunc, processid) ;

在这里使用: http://www.mombu.com/microsoft/windows-programmer-win32/t-bring-process-to-foreground-page2-1656417.html

但是,如果当前窗口pid等于提供给processid func的EnumWindows的窗口,则枚举所有窗口并进行回调测试。我只想在一个窗口上运行。

这是我的FocusWindow功能:

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

1 个答案:

答案 0 :(得分:0)

假设有一个父进程和几个子进程,那么它的一个子进程必须以父进程id作为参数执行AllowSetForegroundWindow函数。

然后父进程可以成功调用SetForegroundWindow