Solved

Window focus problems

Posted on 2003-10-30
3
312 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
3 Comments
 
LVL 9

Expert Comment

by:Priyesh
Comment Utility
Try mapping WM_ACTIVATEAPP
0
 
LVL 2

Author Comment

by:paulburns
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mixString challenge 36 98
maxBlock challenge 30 99
haveThree challenge 22 100
unix example issues 18 44
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: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now