Solved

Window focus problems

Posted on 2003-10-30
3
315 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ODBC Connection Logging, ADO.NET 6 73
Creating Labels and Frequency list style reports in SAS code 9 127
viewing source code from eclipse 13 111
Not needed 13 133
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

738 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