Solved

Window focus problems

Posted on 2003-10-30
3
316 Views
Last Modified: 2013-11-20
I have a dialog based app which is launching a process and then waiting for it to finish. The problem is that if I click on the parent window before the child process window displays then it seems to get the focus and is on top of the child window when it finally does display.

How can I make it so that child window will always be displayed on top of the launching app's window?

(I don't have access to the source code for the child process, so can't make any mods there)
0
Comment
Question by:paulburns
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 9

Expert Comment

by:Priyesh
ID: 9655139
Try mapping WM_ACTIVATEAPP
0
 
LVL 2

Author Comment

by:paulburns
ID: 9655290
that didin't seem to work, the message never fires when child process is started.

Maybe I could call SetWindowPos on it to move it to the top of the z-order. But all I have is the process handle, do you know a way to get the main window handle of an application from its process handle?
0
 
LVL 23

Accepted Solution

by:
Roshan Davis earned 125 total points
ID: 9655589
You will get the Main window handle from a created process by this code.
I got this code from one EE friend.

BOOL CProcessDlg::DoCreateProcess()
{
      STARTUPINFO StartupInfo;
      CString CommandLine;
      BOOL rc;
      // I am putting the (exe) filename in quotes in case there are blanks etc. in it
      CommandLine = CString('"') + m_Filename + CString("\" ") + m_Parameters;
      memset(&StartupInfo, 0, sizeof(StartupInfo));
      StartupInfo.cb = sizeof(StartupInfo);

      rc = CreateProcess(NULL, (LPTSTR)(LPCTSTR)CommandLine, NULL, NULL, FALSE, 0,
                                    NULL, NULL, &StartupInfo, &m_ProcessInformation);
      
      if (!rc)
      {
            m_Message = ShowError("CreateProcess");
            return FALSE;
      }
      
      switch (WaitForInputIdle(m_ProcessInformation.hProcess, 10000))
      {
      case 0:
            // The process has been created and has finished intializing
            // so now we look for it
            m_hProcessWnd = NULL;
            EnumWindows(EnumWindowsProc, (DWORD)this);
            m_Message.Format("Started: %s\r\nprocess id: %i; window handle: %X",
            CommandLine, m_ProcessInformation.dwProcessId, m_hProcessWnd);
            return TRUE;
      case 0xFFFFFFFF:
            m_Message = ShowError("WaitForInputIdle");
            break;
      default: // WAIT_TIMEOUT
            m_Message = "WaitForInputIdle timed out";
            break;
      }

      return TRUE;
}


BOOL CALLBACK CProcessDlg::EnumWindowsProc(HWND hWnd, LPARAM lParam)
{
      CProcessDlg *ppProcessDlg=(CProcessDlg *)lParam;
      DWORD dwProcessId;
      CString Message;
      
      if (!hWnd) return TRUE;

      GetWindowThreadProcessId(hWnd, &dwProcessId);
      if (pProcessDlg->m_ProcessInformation.dwProcessId != dwProcessId)
            return TRUE;
      if (!::IsWindowVisible(hWnd))
            return TRUE;
      pProcessDlg->m_hProcessWnd = hWnd;

      return TRUE;
}


CString CProcessDlg::ShowError(LPCTSTR pFunction)
{
      CString Message;
      LPTSTR pMsgBuf;
      FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
                              NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                              (LPTSTR)&pMsgBuf, 0, NULL);

      if (*pFunction)
            Message.Format("%s from: %s", pMsgBuf, pFunction);
      else
            Message = pMsgBuf;

      LocalFree(pMsgBuf);

      return Message;
}


Good luck
Rosh :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question