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. How to bring an App to front, from another App

How to bring an App to front, from another App

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++tutorial
9 Posts 3 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.
  • T Offline
    T Offline
    timbk
    wrote on last edited by
    #1

    Hello , I'm programing in VC++ 6, and the entire project runs in a computer without keyboard but with touch screen. The thing is that a have two app runnig at the same time, and I want the user to be able to switch between these apps by touching in a third app (called "Switcher",wich is always on top), this third app is a dialog based app, dialog's size is small and it has only two buttons, one for bring the first app to front and the other button to bring to the second app. The project has to run over win 98 an over win XP. How can i do to get wich app is The "Switcher" and bring the other to front but under the "switcher"?

    D A 2 Replies Last reply
    0
    • T timbk

      Hello , I'm programing in VC++ 6, and the entire project runs in a computer without keyboard but with touch screen. The thing is that a have two app runnig at the same time, and I want the user to be able to switch between these apps by touching in a third app (called "Switcher",wich is always on top), this third app is a dialog based app, dialog's size is small and it has only two buttons, one for bring the first app to front and the other button to bring to the second app. The project has to run over win 98 an over win XP. How can i do to get wich app is The "Switcher" and bring the other to front but under the "switcher"?

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Have you considered SetWindowPos()?

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "Man who follows car will be exhausted." - Confucius

      1 Reply Last reply
      0
      • T timbk

        Hello , I'm programing in VC++ 6, and the entire project runs in a computer without keyboard but with touch screen. The thing is that a have two app runnig at the same time, and I want the user to be able to switch between these apps by touching in a third app (called "Switcher",wich is always on top), this third app is a dialog based app, dialog's size is small and it has only two buttons, one for bring the first app to front and the other button to bring to the second app. The project has to run over win 98 an over win XP. How can i do to get wich app is The "Switcher" and bring the other to front but under the "switcher"?

        A Offline
        A Offline
        Anthony Mushrow
        wrote on last edited by
        #3

        You can use SetWindowPos to move, resize and change the z-index of a window, SetWindowPos[^] So you could use this: SetWindowPos(targetWindow, switcherHandle, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE); To bring the window to the front, by passing the handle to your switcher window as the second parameter the target application should appear just behind it. Rather than at the front of all windows.

        My current favourite phrase: I've seen better!

        -SK Genius

        Source Indexing and Symbol Servers Vehicle Simulation Demo - Mostly Works

        T 1 Reply Last reply
        0
        • A Anthony Mushrow

          You can use SetWindowPos to move, resize and change the z-index of a window, SetWindowPos[^] So you could use this: SetWindowPos(targetWindow, switcherHandle, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE); To bring the window to the front, by passing the handle to your switcher window as the second parameter the target application should appear just behind it. Rather than at the front of all windows.

          My current favourite phrase: I've seen better!

          -SK Genius

          Source Indexing and Symbol Servers Vehicle Simulation Demo - Mostly Works

          T Offline
          T Offline
          timbk
          wrote on last edited by
          #4

          Thanks, So I have to use this instruccion SetWindowPos(targetWindow, switcherHandle, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE); on my "Switcher" dialog, ok, my last doubt is how can I get the handler to the target window from the "Switcher" dialog?

          A 1 Reply Last reply
          0
          • T timbk

            Thanks, So I have to use this instruccion SetWindowPos(targetWindow, switcherHandle, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE); on my "Switcher" dialog, ok, my last doubt is how can I get the handler to the target window from the "Switcher" dialog?

            A Offline
            A Offline
            Anthony Mushrow
            wrote on last edited by
            #5

            If you know the title of the window, or its class name then you can use FindWindow[^]. If you don't have the name of the window, but do have a HINSTANCE then you can search through all of the windows until you find the one you need:

            HWND tmpWnd;

            bool foundWindow = false;

            //Get topmost window (which would probably be switcher in this case but it doesn't really matter)
            tepWnd = FindWindow(NULL, NULL)

            while(tmpWnd != NULL)
            {
            HINSTANCE hInstance = ProcIDFromWnd(tmpWnd);
            if(hInstance == windowInst)
            {
            foundWindow = true;
            break;
            }
            else
            {
            tmpWnd = GetWindow(tmpWnd, GW_HWNDNEXT);
            }
            }

            Without either a HINSTANCE the class name, or the window name then I don't think there's much you could do.

            My current favourite phrase: I've seen better!

            -SK Genius

            Source Indexing and Symbol Servers Vehicle Simulation Demo - Mostly Works

            T 1 Reply Last reply
            0
            • A Anthony Mushrow

              If you know the title of the window, or its class name then you can use FindWindow[^]. If you don't have the name of the window, but do have a HINSTANCE then you can search through all of the windows until you find the one you need:

              HWND tmpWnd;

              bool foundWindow = false;

              //Get topmost window (which would probably be switcher in this case but it doesn't really matter)
              tepWnd = FindWindow(NULL, NULL)

              while(tmpWnd != NULL)
              {
              HINSTANCE hInstance = ProcIDFromWnd(tmpWnd);
              if(hInstance == windowInst)
              {
              foundWindow = true;
              break;
              }
              else
              {
              tmpWnd = GetWindow(tmpWnd, GW_HWNDNEXT);
              }
              }

              Without either a HINSTANCE the class name, or the window name then I don't think there's much you could do.

              My current favourite phrase: I've seen better!

              -SK Genius

              Source Indexing and Symbol Servers Vehicle Simulation Demo - Mostly Works

              T Offline
              T Offline
              timbk
              wrote on last edited by
              #6

              Ok, it's working for the app1 that I know his title, let say from the "Switcher" app it's possible bring the other app1(the one i know his title) behind the Switcher, but with the other app2 , (wich is no-modal dialog) it doesn't work , i do not know the name of his class and the dialog it has not title, so i went whith the code that you wrote, but compiler complains with ProcIDFromWnd, it says that is undeclared identifier, it seems that is not a valid intruction for VC++ 6 , ?? Anyway , since in the final projet will be runnig only these three apps, i thoth that the Switcher can bring the app 1(the one working) behind it and send it to bottom when the user wants to see the app2, mfc library says that it's possible to use the parameter HWND_BOTTOM instead hWndSwitcher in SetWindowPos(hWndTarget, hWndSwitcher, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE); to do that, but it's doesn't work neither. What do you think?

              A 1 Reply Last reply
              0
              • T timbk

                Ok, it's working for the app1 that I know his title, let say from the "Switcher" app it's possible bring the other app1(the one i know his title) behind the Switcher, but with the other app2 , (wich is no-modal dialog) it doesn't work , i do not know the name of his class and the dialog it has not title, so i went whith the code that you wrote, but compiler complains with ProcIDFromWnd, it says that is undeclared identifier, it seems that is not a valid intruction for VC++ 6 , ?? Anyway , since in the final projet will be runnig only these three apps, i thoth that the Switcher can bring the app 1(the one working) behind it and send it to bottom when the user wants to see the app2, mfc library says that it's possible to use the parameter HWND_BOTTOM instead hWndSwitcher in SetWindowPos(hWndTarget, hWndSwitcher, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE); to do that, but it's doesn't work neither. What do you think?

                A Offline
                A Offline
                Anthony Mushrow
                wrote on last edited by
                #7

                I'm not sure why SetWindowPos wouldn't work, unless you have the wrong hWnd, but as for finding a window by its HINSTANCE this code should actually work. The sample I originally found was in VB so I guess that ProcIDFromWnd is specific to that, GetWindowThreadProcessId seems to be what we really need.

                HWND tmpWnd;

                bool foundWindow = false;

                //Get topmost window (which would probably be switcher in this case but it doesn't really matter)
                tepWnd = FindWindow(NULL, NULL)
                DWORD procID = GetProcessId(hInstace);

                while(tmpWnd != NULL)
                {
                DWORD id = 0;
                GetWindowThreadProcessId(tmpWnd, &id);
                if(id == procID)
                {
                foundWindow = true;
                break;
                }
                else
                {
                tmpWnd = GetWindow(tmpWnd, GW_HWNDNEXT);
                }
                }

                Bear in mind that this code would find all windows that a process owns, so if it has more than one then you'll have to try and figure out which one you really need.

                My current favourite phrase: I've seen better!

                -SK Genius

                Source Indexing and Symbol Servers Vehicle Simulation Demo - Mostly Works

                T 1 Reply Last reply
                0
                • A Anthony Mushrow

                  I'm not sure why SetWindowPos wouldn't work, unless you have the wrong hWnd, but as for finding a window by its HINSTANCE this code should actually work. The sample I originally found was in VB so I guess that ProcIDFromWnd is specific to that, GetWindowThreadProcessId seems to be what we really need.

                  HWND tmpWnd;

                  bool foundWindow = false;

                  //Get topmost window (which would probably be switcher in this case but it doesn't really matter)
                  tepWnd = FindWindow(NULL, NULL)
                  DWORD procID = GetProcessId(hInstace);

                  while(tmpWnd != NULL)
                  {
                  DWORD id = 0;
                  GetWindowThreadProcessId(tmpWnd, &id);
                  if(id == procID)
                  {
                  foundWindow = true;
                  break;
                  }
                  else
                  {
                  tmpWnd = GetWindow(tmpWnd, GW_HWNDNEXT);
                  }
                  }

                  Bear in mind that this code would find all windows that a process owns, so if it has more than one then you'll have to try and figure out which one you really need.

                  My current favourite phrase: I've seen better!

                  -SK Genius

                  Source Indexing and Symbol Servers Vehicle Simulation Demo - Mostly Works

                  T Offline
                  T Offline
                  timbk
                  wrote on last edited by
                  #8

                  Now i get these error messages from compiler:

                  error C2065: 'GetProcessID' : undeclared identifier
                  error C2065: 'hInstace' : undeclared identifier

                  I can't find a the equivalent instruction in VC++6 for GetProcessID

                  A 1 Reply Last reply
                  0
                  • T timbk

                    Now i get these error messages from compiler:

                    error C2065: 'GetProcessID' : undeclared identifier
                    error C2065: 'hInstace' : undeclared identifier

                    I can't find a the equivalent instruction in VC++6 for GetProcessID

                    A Offline
                    A Offline
                    Anthony Mushrow
                    wrote on last edited by
                    #9

                    Well, the function does exist MSDN[^] hInstace is just the handle for the process that started the window that you want to move, if you don't have that and want to find and move a window that has no title that you don't have a class name for, then your last option would be to look through all of the running processes and find the one you need, only then would you be able to look through all of the windows in search of the one the process opened.

                    My current favourite phrase: I've seen better!

                    -SK Genius

                    Source Indexing and Symbol Servers Vehicle Simulation Demo - Mostly Works

                    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