Click to See Complete Forum and Search --> : ShellExecute Window focus


Steve White
May 6th, 1999, 04:49 PM
Hi;
I am calling ShellExecute from the OnInitDialog() function of a Dialog to edit a file with an external application (probably MS Word). I want the editor Window to appear on top but it is appearing behind the calling process. I suspect that the Dialog's ShowWindow function is receiving the focus after the external Process window is displayed. I've tried calling ShellExecute from the Dialogs ShowWindow function but this did not work. Any idea on how I can force the focus to the external app?

Thanks

May 7th, 1999, 05:30 PM
Not an answer (I have the same problem), but if you can get the process id, you can get a window associated with the process (see code below). With a window handle, you can, of course, send messages to the window.

The problem I (and you) have is that there appears to be no way to get a process id from a process handle (which ShellExecuteEx gives you). You can get a handle from a process id however.

BOOL CALLBACK EnumChildProc(HWND hWnd, LPARAM lParam)
{
DWORD pid = 0;

// get the processid for this window
GetWindowThreadProcessId(hWnd, &pid);

if(pid == (DWORD)lParam)
{
if(GetParent(hWnd))
return TRUE;

if(!IsWindowVisible(hWnd))
return TRUE;

if(IsIconic(hWnd))
{
ShowWindow(hWnd, SW_RESTORE);
}

SetForegroundWindow(hWnd);
SetActiveWindow(hWnd);
SetFocus(hWnd);

return FALSE;
}

return TRUE;

}

BOOL SwitchProcessIntoForeground(HANDLE hProcess)
{
DWORD pid = 0;

HANDLE hSnapshot;

hSnapshot = CreateToolhelp32Snapshot(
TH32CS_SNAPPROCESS, // flags: only make a process snapshot
0 // process identifier (0 == current)
);

DWORD dwPID = 0;

if(hSnapshot)
{
PROCESSENTRY32 pe ;
pe.dwSize = sizeof( PROCESSENTRY32 ) ;

// Now iterate the snapshot.
for( BOOL bOK = Process32First(hSnapshot, &pe);
GetLastError() != ERROR_NO_MORE_FILES;
bOK = Process32Next(hSnapshot, &pe))
{
if(bOK)
{
dwPID = pe.th32ProcessID;
}
}

::CloseHandle(hSnapshot);
}

if(pid)
{
if(!EnumWindows(EnumChildProc, (LPARAM)pid))
{
GetWin32Error();
}
}
}

#endif

Hardeep Singh
May 8th, 1999, 01:09 AM
How about SetWindowPos() with the wndNoTopMost argument?
(After seeing the previous response, this seems too silly, but it works for me)