CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1

    ShellExecute Window focus

    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


  2. #2
    Guest

    Re: ShellExecute Window focus

    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




  3. #3
    Join Date
    May 1999
    Posts
    69

    Re: ShellExecute Window focus

    How about SetWindowPos() with the wndNoTopMost argument?
    (After seeing the previous response, this seems too silly, but it works for me)


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured