Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Bring An Application To The Foreground Through Another Application

Bring An Application To The Foreground Through Another Application

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestiondiscussion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dean Michaud
    wrote on last edited by
    #1

    I have a need of a way to bring one application to the foreground from another application. Application 'A' is either laucnhed by itself or it can be launched from application 'B' upon a button press. There's no guarantee which app was started first, or if the application to be brought to the foreground (app 'A') is even running at that point in time. If 'A' is not running I am launching it upon a button press in app 'B' - but if 'A' is already running I want to bring it to the foreground to alert the user it's already running (and not spawn a second instance of app 'A'). I'm currently using a mutex to check to see if app 'A' is already running, but I am not sure how to bring it to the foreground when it is. Any thoughts or ideas on how to proceed? : Dean 'Karnatos' Michaud

    M D D S 4 Replies Last reply
    0
    • D Dean Michaud

      I have a need of a way to bring one application to the foreground from another application. Application 'A' is either laucnhed by itself or it can be launched from application 'B' upon a button press. There's no guarantee which app was started first, or if the application to be brought to the foreground (app 'A') is even running at that point in time. If 'A' is not running I am launching it upon a button press in app 'B' - but if 'A' is already running I want to bring it to the foreground to alert the user it's already running (and not spawn a second instance of app 'A'). I'm currently using a mutex to check to see if app 'A' is already running, but I am not sure how to bring it to the foreground when it is. Any thoughts or ideas on how to proceed? : Dean 'Karnatos' Michaud

      M Offline
      M Offline
      Michael Liu
      wrote on last edited by
      #2

      Try to use SetForegroundWindow() then use ShowWindow() with SW_SHOWNORMAL flag. Michael Liu

      D 1 Reply Last reply
      0
      • M Michael Liu

        Try to use SetForegroundWindow() then use ShowWindow() with SW_SHOWNORMAL flag. Michael Liu

        D Offline
        D Offline
        Dean Michaud
        wrote on last edited by
        #3

        To set the foreground, I need to either get: A) hWnd for app 'B' while in app 'A' (not sure how to do that) to use "BOOL SetForegroundWindow(HWND hWnd)" B) Get a pointer to the CWnd of app 'B' (not sure how to do this either) to use "BOOL CWnd::SetForegroundWindow( )" : Dean 'Karnatos' Michaud

        1 Reply Last reply
        0
        • D Dean Michaud

          I have a need of a way to bring one application to the foreground from another application. Application 'A' is either laucnhed by itself or it can be launched from application 'B' upon a button press. There's no guarantee which app was started first, or if the application to be brought to the foreground (app 'A') is even running at that point in time. If 'A' is not running I am launching it upon a button press in app 'B' - but if 'A' is already running I want to bring it to the foreground to alert the user it's already running (and not spawn a second instance of app 'A'). I'm currently using a mutex to check to see if app 'A' is already running, but I am not sure how to bring it to the foreground when it is. Any thoughts or ideas on how to proceed? : Dean 'Karnatos' Michaud

          D Offline
          D Offline
          David Viggiano
          wrote on last edited by
          #4

          Are you running under W2K or XP? If so, SetForegroundWindow no longer works unless you own the process. There is a workaround - you attach your thread to the thread with current foreground thread and then SetForegroundWindow works. Check the link for a good example. D http://www.mooremvp.freeserve.co.uk/Win32/framed\_tip033.htm

          D 1 Reply Last reply
          0
          • D Dean Michaud

            I have a need of a way to bring one application to the foreground from another application. Application 'A' is either laucnhed by itself or it can be launched from application 'B' upon a button press. There's no guarantee which app was started first, or if the application to be brought to the foreground (app 'A') is even running at that point in time. If 'A' is not running I am launching it upon a button press in app 'B' - but if 'A' is already running I want to bring it to the foreground to alert the user it's already running (and not spawn a second instance of app 'A'). I'm currently using a mutex to check to see if app 'A' is already running, but I am not sure how to bring it to the foreground when it is. Any thoughts or ideas on how to proceed? : Dean 'Karnatos' Michaud

            D Offline
            D Offline
            Dean Michaud
            wrote on last edited by
            #5

            Ok, after mucking around with this a little more, seems this is *one* way of getting the job done. I am calling this code once a button is pressed to bring up an application to the foreground. 1) CWnd::FindWindow() finds the window based on the file name. 2) CWnd::SetWindowPos() shows the found window as on top, without resizing it or moving it. 3) CWnd::ShowWindow() is making sure the application is not minimized/maximized.

            BOOL rc;
            
            CWnd\* my\_wnd = FindWindow(NULL, "Application-Name");
            
            if (my\_wnd != NULL)
            {
            	rc = my\_wnd->SetWindowPos(&wndTop, 0, 0, 0, 0, SWP\_NOMOVE|SWP\_NOSIZE|SWP\_SHOWWINDOW);
            
            	rc = my\_wnd->ShowWindow(SW\_SHOWNORMAL);
            }
            

            What I'd like to know is another way of getting the CWnd pointer to the window - I don't like writing code that depends on another applications having the same title (it very-well could change without me knowing, either by the dept. that wrote the code changing it and forgetting to notify me, or someone's changing the title of it for some purpose of their own). So, now my search moves onto looking for another way of finding a specific window that is running since FindWindow() relies on the window title not changing. : Dean 'Karnatos' Michaud

            1 Reply Last reply
            0
            • D David Viggiano

              Are you running under W2K or XP? If so, SetForegroundWindow no longer works unless you own the process. There is a workaround - you attach your thread to the thread with current foreground thread and then SetForegroundWindow works. Check the link for a good example. D http://www.mooremvp.freeserve.co.uk/Win32/framed\_tip033.htm

              D Offline
              D Offline
              Dean Michaud
              wrote on last edited by
              #6

              I need to support Win98 to WinXP... I'd noticed in the MSDN Library that SetForegroundWindow() was not working as it had before in WinXP. Thank you though :) : Dean 'Karnatos' Michaud

              1 Reply Last reply
              0
              • D Dean Michaud

                I have a need of a way to bring one application to the foreground from another application. Application 'A' is either laucnhed by itself or it can be launched from application 'B' upon a button press. There's no guarantee which app was started first, or if the application to be brought to the foreground (app 'A') is even running at that point in time. If 'A' is not running I am launching it upon a button press in app 'B' - but if 'A' is already running I want to bring it to the foreground to alert the user it's already running (and not spawn a second instance of app 'A'). I'm currently using a mutex to check to see if app 'A' is already running, but I am not sure how to bring it to the foreground when it is. Any thoughts or ideas on how to proceed? : Dean 'Karnatos' Michaud

                S Offline
                S Offline
                Scott H Settlemier
                wrote on last edited by
                #7

                SetForegroundWindow. with some caveats. Here's a great article : http://www.etree.com/tech/Articles/attachthreadinput.pdf And here's some code to do it: DWORD MyThreadId=GetCurrentThreadId(); DWORD ForeThreadId=::GetWindowThreadProcessId(::GetForegroundWindow(),0); HWND hForegroundMe=0; // Get handle to the window you want to place in the foreground. // For instance use EnumThreadWindows if you know the ThreadId // (perhaps you have it saved in a shared memory?) // or use EnumWindows if you know the window text of the window // you are looking for... if (GetWindowLong(hForegroundMe,GWL_STYLE)&WS_MINIMIZE) ::ShowWindow(hForegroundMe,SW_RESTORE),::UpdateWindow(hForegroundMe); if (ForeThreadId!=MyThreadId) AttachThreadInput(ForeThreadId,MyThreadId,TRUE); ::SetForegroundWindow(hForegroundMe); if (ForeThreadId!=MyThreadId) AttachThreadInput(ForeThreadId,MyThreadId,FALSE);

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups