Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Activate (focus to) another application/process

269 views
Skip to first unread message

R.Wieser

unread,
Mar 1, 2015, 8:42:37 AM3/1/15
to
Hello all,

How do I switch to an already running program, as if its started by
double-clicking it ?

I thought it would be easy to, when you have a window-handle belonging to
another process (think FindWindow), switch over to that program. Alas, it
isn't (nonwithstanding the existance of functions like SetFocus,
SetForegroundWindow, SetWindowPos, SwitchToThisWindow, and possibly a few
more).

I found a very promissing solution (attach to other thread,
SetForegroundWindow, SetFocus, detach from thread), but it fails miserably
when the program that executes that code isn't (I assume) a top-level window
anymore -- When I clicked on the desktop and than tried to re-activate a
program all I got was that the taskbar showed a shortly blinking and than
steady orangy program button, while the desktop kept focus (yes, I see some
logic in not letting another program focus yet another program when the user
is busy elsewhere, but now its definitily working against me :-\ ).


tl;dr:
How do I get my program, focussed or not, to activate another, already
running program ?

Regards,
Rudy Wieser



Christian Astor

unread,
Mar 3, 2015, 5:30:08 AM3/3/15
to
R.Wieser a écrit :
I use the following function, which works most of the time (tested only
on XP SP3 & Windows 7 SP1)
In some rare cases, I add a "fake" SendInput() then I call the function
again.


void SwitchToWindow(HWND hWnd)
{
DWORD dwLockTimeOut = 0;
HWND hWndForeground = GetForegroundWindow();
DWORD dwCurrentTID = GetCurrentThreadId(), dwForegroundTID =
GetWindowThreadProcessId(hWndForeground, 0);

if (dwCurrentTID != dwForegroundTID)
{
AttachThreadInput(dwCurrentTID, dwForegroundTID, TRUE);
SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, &dwLockTimeOut, 0);
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, 0,
SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);
AllowSetForegroundWindow(ASFW_ANY);
}

if (IsIconic(hWnd))
ShowWindow(hWnd, SW_RESTORE);
HWND hWndLastActivePopup = GetLastActivePopup(hWnd);
void (FAR STDAPICALLTYPE * pSTTW)(HWND hWnd, BOOL bAltTab);
HINSTANCE hInst = LoadLibrary(L"USER32.DLL");
if (hInst)
{
(FARPROC&)pSTTW = GetProcAddress(hInst, "SwitchToThisWindow");
if (pSTTW != NULL)
pSTTW(hWndLastActivePopup, TRUE);
FreeLibrary(hInst);
}
SetForegroundWindow(hWnd);

if (dwCurrentTID != dwForegroundTID)
{
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0,
(PVOID)dwLockTimeOut, SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);
AttachThreadInput(dwCurrentTID, dwForegroundTID, FALSE);
}
}

R.Wieser

unread,
Mar 3, 2015, 7:11:20 AM3/3/15
to
Christian,

> I use the following function, which works most of the time

Thanks, I will certainly try it. Any indication to the circumstances in
which it refuses to work ?

> In some rare cases, I add a "fake" SendInput() then I call
> the function again.

Lol. As I could not dependantly start an app and set focus to it I thought
that maybe just "taking over" the mouse & keyboard (I'm behind a computer
looking out on the computer I want to control) would maybe be the way to go
(Didn't want to use RPC, 'cause the copying of the screen-contents would
cause a massive data transfer -- Think Media-center with a movie running),
which landed me on exactly that command.

Now I think of it, how would that command solve the problem of the
just-started program not getting mouse-and-keyboard focus ? Wouldn't the
fake mouse/keyboard data not just go to whichever program that retained the
focus ?

Regards,
Rudy Wieser


-- Origional message:
Christian Astor <cast...@club-internet.fr> schreef in berichtnieuws
md42f8$fa$1...@speranza.aioe.org...

Christian Astor

unread,
Mar 4, 2015, 10:32:30 AM3/4/15
to
R.Wieser a écrit :

> Thanks, I will certainly try it. Any indication to the circumstances in
> which it refuses to work ?


It is with an application written in Qt, where I have to call
SendInput() with 0, then it works on the next call.
I did not try to find why, because it works for other cases...

R.Wieser

unread,
Mar 5, 2015, 4:37:35 AM3/5/15
to
Christian,

Thanks.

Regards,
Rudy Wieser


-- Origional message:
Christian Astor <cast...@club-internet.fr> schreef in berichtnieuws
md78i8$iv7$1...@speranza.aioe.org...
0 new messages