Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Window focus problems

Posted on 2003-10-30
3
Medium Priority
?
319 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 500 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

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

722 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