Tech Support > Microsoft Windows > Development Resources > Bring process to foreground
Bring process to foreground
Posted by intell1 on October 8th, 2004


Hi,

I have two questions on programming with processes in vc++:

1. If I programmatically create an instance of, say, NotePad, using
ShellExecuteEx(), I can store the process handle for later use. I can also
check if this process is active at run-time. How do I bring the process to
the foreground, ie make it the active desktop window?

2. Now if I do the same with MS-Word, I think that no matter how many times
I use ShellExecuteEx() to launch Word with different documents, there is
only ONE process of Word persisting. So I can bring the program to the
foreground (assuming I get a response for item 1 above :-) ), but I am left
with the more difficult task to activate the desired document as well. I am
clueless as of how to do this because I cannot find any link between the
process handle (unique) and the document names (multiple).

Please advice. TIA,
Nikolas

PS. I can do the second part of (2) with automation, but I need a general
programming solution because I have to do similar things with non-MS
applications (Acrobat etc.)



Posted by David Lindauer on October 8th, 2004


to bring a process to the top do something like this:

BOOL CALLBACK topenumfunc(HWND wnd, LPARAM value)
{
DWORD id ;
GetWindowThreadProcessId(wnd, &id) ;
if (id == value) {
SetForegroundWindow(wnd) ;
if (IsIconic(wnd))
ShowWindow(wnd,SW_RESTORE) ;
return FALSE ;
}
return TRUE ;
}
void ProcessToTop(DWORD processid)
{
EnumWindows((WNDENUMPROC)topenumfunc, processid) ;
}

That will make the first window it finds in the desire process active.

You can do something similar with individual windows, providing you know for
example the text that will appear in the title bar you can do a GetWindowText
on each window and see if it matches your expectations.
Believe it or not that method is actually used in production programs! You may
have to experiment a bit if you need to activate a specific MDI Child window,
there is an EnumChildWindows or something to that effect which may help you in
that regard.

David



Posted by Michael Kurz on October 9th, 2004


"David Lindauer" <camille@bluegrass.net> schrieb im Newsbeitrag
news:41670768.934BB401@bluegrass.net...

But it only works if the calling program is the topmost program at the
moment, as a program which does not have the input focus can not bring other
programs to front, instead the system flashes the caption. This start



Posted by Michael Kurz on October 9th, 2004



sorry posted accidently to early meant:

But it only works if the calling program is the topmost program at the
moment, as a program which does not have the input focus can not bring other
programs to front, instead the system flashes the caption. This started with
Win2K I think. Look for ForegroundLockTimeOut, which controls this. If you
set the timeout systemwide to 0 then it will work in any case.


Regards
Michael.



Posted by Alf P. Steinbach on October 10th, 2004


* David Lindauer:
It's always a good idea to apply GetLastActivePopup.

Also see comments by Michael Kurtz.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Posted by intell1 on October 12th, 2004


Thank you all for the replies. I am half way through accomplishing task (1)
based on the code snippet posted here. One slight problem: When the process
window is minimized (iconic), I cannot get it to show at all (I cannot
maximize it). Any deas?

Sincere thanks,
Nikolas

"Alf P. Steinbach" <alfps@start.no> wrote in message
news:416897bc.1037879015@news.individual.net...



Similar Posts