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. SetForegroundWindow fails

SetForegroundWindow fails

Scheduled Pinned Locked Moved C / C++ / MFC
comhelp
12 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.
  • V vcplusplus

    That's how XP works. It flashes the taskbar instead of having the application take over the screen. If you can, try your App. on a Win 98 machine just to make sure it's XP and not your code.

    V Offline
    V Offline
    vcplusplus
    wrote on last edited by
    #3

    I found a work-around for this problem. http://codeproject.com/dialog/dlgboxtricks.asp[^]

    N 2 Replies Last reply
    0
    • V vcplusplus

      I found a work-around for this problem. http://codeproject.com/dialog/dlgboxtricks.asp[^]

      N Offline
      N Offline
      Neville Franks
      wrote on last edited by
      #4

      Thanks a bunch.:rose: Looks promising. Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com

      1 Reply Last reply
      0
      • V vcplusplus

        I found a work-around for this problem. http://codeproject.com/dialog/dlgboxtricks.asp[^]

        N Offline
        N Offline
        Neville Franks
        wrote on last edited by
        #5

        Well my initial hopes have been dashed.:( It appears to work when I run the App in the Debugger, but not otherwise. I think the problem is the first app is stealing back or not relinquishing focus. I have another path I'm about to head down. I use SetForegroundWindow() at other times in the App to restore from the System Tray for example and they work fine. Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com

        1 Reply Last reply
        0
        • N Neville Franks

          Hi, I have two windows processes and one sends (posts) a mesage to the other to activate it etc. When the main app gets this message it calls SetForegroundWindow() to bring the app to the foreground so it is the active/on top application. The problem is SetForegroundWindow() is failing (returning 0) and my app stays in the background. I've also tried BringWindowToTop(). The WinXP Task Bar flashes the Icon for my app. I'm going nuts here.:( Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com

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

          The old way was to use AttachThreadInput-- (there's a hefty list of under just what conditions SetForegroundWindow will work) search for that in the Message Boards. The new way to use MS's settlement API: SwitchToThisWindow http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/switchtothiswindow.asp[^]

          N 1 Reply Last reply
          0
          • S Scott H Settlemier

            The old way was to use AttachThreadInput-- (there's a hefty list of under just what conditions SetForegroundWindow will work) search for that in the Message Boards. The new way to use MS's settlement API: SwitchToThisWindow http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/switchtothiswindow.asp[^]

            N Offline
            N Offline
            Neville Franks
            wrote on last edited by
            #7

            Hi Scott, Thanks for that. I'd never heard of SwitchToThisWindow(). This sort of works, however there are two problems. First the Task Bar pops-up (I have it to Auto-Hide) and flashes the app icon. Second and worse is if I Alt+Tab to another app afer my app has come to the foreground using SwitchToThisWindow() then cause my app to activate using SwitchToThisWindow() the aforementioned app is activated not mine. This is a killer. Finally SwitchToThisWindow sends the previous app to the bottom of the Z-Order which seems very strange. AFAIK this isn't what SetForegroundWindow() does. I can't believe something as simple as this is causing me so much grief. Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com

            1 Reply Last reply
            0
            • N Neville Franks

              Hi, I have two windows processes and one sends (posts) a mesage to the other to activate it etc. When the main app gets this message it calls SetForegroundWindow() to bring the app to the foreground so it is the active/on top application. The problem is SetForegroundWindow() is failing (returning 0) and my app stays in the background. I've also tried BringWindowToTop(). The WinXP Task Bar flashes the Icon for my app. I'm going nuts here.:( Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com

              P Offline
              P Offline
              Phil J Pearson
              wrote on last edited by
              #8

              As others have said this is the way XP is supposed to work; users don't like have the rug window they are working in being pulled away without warning. However, if you are certain you have a really good reason to do it (warning of an impending nuclear strike or the re-election of Dubbya), you can (usually) do it this way:

              ///
              // Bring a window to the foreground. Ignore the howls of protest from users.
              ///
              BOOL StealFocus(CWnd *pWnd)
              {
              // Attach foreground window thread to our thread
              DWORD ForeGroundID = GetWindowThreadProcessId(::GetForegroundWindow(), NULL);
              DWORD CurrentID = GetCurrentThreadId();
              BOOL bRet = AttachThreadInput(ForeGroundID, CurrentID, TRUE);
              pWnd->SetForegroundWindow();
              pWnd->SetFocus(); //Just playing safe
              // Detach the attached thread
              AttachThreadInput(ForeGroundID, CurrentID, FALSE);
              return bRet;
              }


              The opinions expressed in this communication do not necessarily represent those of the author (especially if you find them impolite, discourteous or inflammatory).

              N 1 Reply Last reply
              0
              • P Phil J Pearson

                As others have said this is the way XP is supposed to work; users don't like have the rug window they are working in being pulled away without warning. However, if you are certain you have a really good reason to do it (warning of an impending nuclear strike or the re-election of Dubbya), you can (usually) do it this way:

                ///
                // Bring a window to the foreground. Ignore the howls of protest from users.
                ///
                BOOL StealFocus(CWnd *pWnd)
                {
                // Attach foreground window thread to our thread
                DWORD ForeGroundID = GetWindowThreadProcessId(::GetForegroundWindow(), NULL);
                DWORD CurrentID = GetCurrentThreadId();
                BOOL bRet = AttachThreadInput(ForeGroundID, CurrentID, TRUE);
                pWnd->SetForegroundWindow();
                pWnd->SetFocus(); //Just playing safe
                // Detach the attached thread
                AttachThreadInput(ForeGroundID, CurrentID, FALSE);
                return bRet;
                }


                The opinions expressed in this communication do not necessarily represent those of the author (especially if you find them impolite, discourteous or inflammatory).

                N Offline
                N Offline
                Neville Franks
                wrote on last edited by
                #9

                Hi Phil, Thanks for the reply. I couldn't get this to work reliably either as indicated in an earlier reply. It worked in the debugger, but not outside. Both of these apps are mine, so I just need the ability to get one to bring the other to the foreground. I've read up on all the issues and what MS is trying to do and why and I'm basically ok with that. But in my case I'm not pulling any rugs out from under anyone but myself.;) I have a solution which seems to work, but I'm not as happy as I'd like with it. I'm about to try doing the SetForgroundWindow() in in App A to get it to bring App B to the foreground and my understanding is this should work. Up untill now I was trying to get App B to bring itself to the foreground. This is the start of a new day for me (here in Oz) which I hope will be better than yesterday.:| Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com

                P 1 Reply Last reply
                0
                • N Neville Franks

                  Hi Phil, Thanks for the reply. I couldn't get this to work reliably either as indicated in an earlier reply. It worked in the debugger, but not outside. Both of these apps are mine, so I just need the ability to get one to bring the other to the foreground. I've read up on all the issues and what MS is trying to do and why and I'm basically ok with that. But in my case I'm not pulling any rugs out from under anyone but myself.;) I have a solution which seems to work, but I'm not as happy as I'd like with it. I'm about to try doing the SetForgroundWindow() in in App A to get it to bring App B to the foreground and my understanding is this should work. Up untill now I was trying to get App B to bring itself to the foreground. This is the start of a new day for me (here in Oz) which I hope will be better than yesterday.:| Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com

                  P Offline
                  P Offline
                  Phil J Pearson
                  wrote on last edited by
                  #10

                  It works fine for me in and out of the debugger as a way for an application to bring itself to the foreground. I simply have StealFocus(this) in a dialog that shows a vital message.


                  The opinions expressed in this communication do not necessarily represent those of the author (especially if you find them impolite, discourteous or inflammatory).

                  N 1 Reply Last reply
                  0
                  • P Phil J Pearson

                    It works fine for me in and out of the debugger as a way for an application to bring itself to the foreground. I simply have StealFocus(this) in a dialog that shows a vital message.


                    The opinions expressed in this communication do not necessarily represent those of the author (especially if you find them impolite, discourteous or inflammatory).

                    N Offline
                    N Offline
                    Neville Franks
                    wrote on last edited by
                    #11

                    Phil J Pearson wrote: It works fine for me in and out of the debugger as a way for an application to bring itself to the foreground. I simply have StealFocus(this) in a dialog that shows a vital message. What version of Windows are you using? Does this bring the app to the foreground or just the dialog? I assume both as one will follow the other but need to ask. I'm using XP with SP1. I'm also using a P3 550Mhz box which tends to show problems that newer, faster machines don't. Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com

                    P 1 Reply Last reply
                    0
                    • N Neville Franks

                      Phil J Pearson wrote: It works fine for me in and out of the debugger as a way for an application to bring itself to the foreground. I simply have StealFocus(this) in a dialog that shows a vital message. What version of Windows are you using? Does this bring the app to the foreground or just the dialog? I assume both as one will follow the other but need to ask. I'm using XP with SP1. I'm also using a P3 550Mhz box which tends to show problems that newer, faster machines don't. Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com

                      P Offline
                      P Offline
                      Phil J Pearson
                      wrote on last edited by
                      #12

                      It works on Windows 2000, XP, XP SP1a and XP SP2 on a range of hardware from Celeron 300 onwards. It brings the whole app to the foreground.


                      The opinions expressed in this communication do not necessarily represent those of the author (especially if you find them impolite, discourteous or inflammatory).

                      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