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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. focus to the current instance of a program

focus to the current instance of a program

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++comtools
10 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.
  • T Offline
    T Offline
    toxcct
    wrote on last edited by
    #1

    ok ok, i stop after this one :laugh: my question (cross posting avoided again) is this one[^] any suggestion on how one can set the focus again to an already existing ainstance of a program ? thanks to all of you in advance


    TOXCCT >>> GEII power
    [toxcct][VisualCalc]

    T D F 3 Replies Last reply
    0
    • T toxcct

      ok ok, i stop after this one :laugh: my question (cross posting avoided again) is this one[^] any suggestion on how one can set the focus again to an already existing ainstance of a program ? thanks to all of you in advance


      TOXCCT >>> GEII power
      [toxcct][VisualCalc]

      T Offline
      T Offline
      ThatsAlok
      wrote on last edited by
      #2

      toxcct wrote: (cross posting avoided again) Oops, I Posted my answer there!

      "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

      cheers, Alok Gupta

      1 Reply Last reply
      0
      • T toxcct

        ok ok, i stop after this one :laugh: my question (cross posting avoided again) is this one[^] any suggestion on how one can set the focus again to an already existing ainstance of a program ? thanks to all of you in advance


        TOXCCT >>> GEII power
        [toxcct][VisualCalc]

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

        toxcct wrote: any suggestion on how one can set the focus again to an already existing ainstance of a program ? Something like:

        if (bFound)
        {
        if (it's a popup window)
        SetForegroundWindow(handle to the popup window);

        if (it's an icon)
            ShowWindow(handle to the popup window, SW\_RESTORE);
        
        return FALSE;
        

        }


        "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

        T 1 Reply Last reply
        0
        • D David Crow

          toxcct wrote: any suggestion on how one can set the focus again to an already existing ainstance of a program ? Something like:

          if (bFound)
          {
          if (it's a popup window)
          SetForegroundWindow(handle to the popup window);

          if (it's an icon)
              ShowWindow(handle to the popup window, SW\_RESTORE);
          
          return FALSE;
          

          }


          "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

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

          thanks david, but firstly, Alok answer satisfied my plainly (with code), and secondly, your sample is not exactly what i need. anyway, i apreciate.


          TOXCCT >>> GEII power
          [toxcct][VisualCalc]

          D 1 Reply Last reply
          0
          • T toxcct

            thanks david, but firstly, Alok answer satisfied my plainly (with code), and secondly, your sample is not exactly what i need. anyway, i apreciate.


            TOXCCT >>> GEII power
            [toxcct][VisualCalc]

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

            toxcct wrote: ...your sample is not exactly what i need. Strange, as it has always worked for me. After you find that the mutex exists, indicating that the application is already running, it's simply a matter of restoring the window.


            "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

            T 1 Reply Last reply
            0
            • D David Crow

              toxcct wrote: ...your sample is not exactly what i need. Strange, as it has always worked for me. After you find that the mutex exists, indicating that the application is already running, it's simply a matter of restoring the window.


              "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

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

              oh sorry, i didn't understood your *code* at first time. DavidCrow wrote: SetForegroundWindow(handle to the popup window); ShowWindow(handle to the popup window, SW_RESTORE); hum well, how do you get handle to the popup window ?


              TOXCCT >>> GEII power
              [toxcct][VisualCalc]

              D 1 Reply Last reply
              0
              • T toxcct

                oh sorry, i didn't understood your *code* at first time. DavidCrow wrote: SetForegroundWindow(handle to the popup window); ShowWindow(handle to the popup window, SW_RESTORE); hum well, how do you get handle to the popup window ?


                TOXCCT >>> GEII power
                [toxcct][VisualCalc]

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

                toxcct wrote: how do you get handle to the popup window ? Use EnumWindows().


                "Ideas are a dime a dozen. People who put them into action are priceless." - Unknown

                1 Reply Last reply
                0
                • T toxcct

                  ok ok, i stop after this one :laugh: my question (cross posting avoided again) is this one[^] any suggestion on how one can set the focus again to an already existing ainstance of a program ? thanks to all of you in advance


                  TOXCCT >>> GEII power
                  [toxcct][VisualCalc]

                  F Offline
                  F Offline
                  Flit
                  wrote on last edited by
                  #8

                  This is how I limit my apps to a single instance and activate the first instance if another is run. HANDLE hm = CreateMutex(NULL, TRUE, "Mutex Name"); DWORD dw = GetLastError(); if(dw == ERROR_ALREADY_EXISTS) { SetForegroundWindow(FindWindow("ClassName", "WindowName")); return(0); } There are probably better ways but this is the simplest I know and has always worked just fine.

                  T 1 Reply Last reply
                  0
                  • F Flit

                    This is how I limit my apps to a single instance and activate the first instance if another is run. HANDLE hm = CreateMutex(NULL, TRUE, "Mutex Name"); DWORD dw = GetLastError(); if(dw == ERROR_ALREADY_EXISTS) { SetForegroundWindow(FindWindow("ClassName", "WindowName")); return(0); } There are probably better ways but this is the simplest I know and has always worked just fine.

                    T Offline
                    T Offline
                    toxcct
                    wrote on last edited by
                    #9

                    interesting code. but what do ClassName and WindowName represent exactly in your above line code ?

                    SetForegroundWindow(FindWindow("ClassName", "WindowName"));

                    thanks in advance.


                    TOXCCT >>> GEII power
                    [toxcct][VisualCalc]

                    F 1 Reply Last reply
                    0
                    • T toxcct

                      interesting code. but what do ClassName and WindowName represent exactly in your above line code ?

                      SetForegroundWindow(FindWindow("ClassName", "WindowName"));

                      thanks in advance.


                      TOXCCT >>> GEII power
                      [toxcct][VisualCalc]

                      F Offline
                      F Offline
                      Flit
                      wrote on last edited by
                      #10

                      ClassName is the name of the type of class of the window you are looking for. Such as the name you used when you called RegisterClass. WindowName is the name of the window you wish to find. This is the window title. Such as "The Code Project - Free Source Code and Tutorials - Internet Explorer". Read the MSDN entry for FindWindow, that should explain it better than me. Hope this is helpful.

                      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