Solved

Window focus problems

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
countEvens challenge 2 113
bigDiff challenge 17 134
withoutTen challenge 14 137
fix34  challenge 9 164
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

740 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