Announcement

Collapse
No announcement yet.

Force a window to foreground

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Force a window to foreground

    How do you make a window come to the foreground or be the top window??

    This is how I'm showing the window, but it will not be the top most window
    if it was visible then i hide it.

    When i unhide it with.

    IF IsWindowVisible(hfrmMaX&) = %FALSE THEN
    ShowWindow hfrmMaX&, %SW_SHOW
    END IF

    the window becomes visible but maybe in the background.

    Thanks
    Jason

    ------------------

  • #2
    I assembled some fragments. A dialog restores itself every 5 sec, even if you switched to another app.
    Code:
      #Compile Exe
      #Register None
      #Dim All
      #Include "Win32Api.Inc"
      
      Declare Function SwitchToThisWindow(hWnd As Long, chState As Long) As Long
    
      Sub pSetForegroundWindow(hwnd As Long)
         Dim lForeThreadID As Long, lThisThreadID As Long, hWndF As Long
         Dim hSwitchToThisWindow As Dword, lResult As Long
         
         Do
            If IsIconic(hwnd) Then ShowWindow hwnd, %SW_RESTORE Else ShowWindow hwnd, %SW_SHOW
            hWndF = GetForegroundWindow: If hwnd = hWndF Then Exit Sub
    
            lForeThreadID = GetWindowThreadProcessId(hWndF, ByVal 0&)
            lThisThreadID = GetWindowThreadProcessId(hwnd, ByVal 0&)
            If lForeThreadID <> lThisThreadID Then _
               AttachThreadInput lForeThreadID, lThisThreadID, %True
            SetForegroundWindow hwnd
            If lForeThreadID <> lThisThreadID Then _
               AttachThreadInput lForeThreadID, lThisThreadID, %False
            hWndF = GetForegroundWindow: If hwnd = hWndF Then Exit Sub
        
            If IsFalse(hSwitchToThisWindow) Then hSwitchToThisWindow = GetProcAddress(GetModuleHandle("user32.dll"), "SwitchToThisWindow")
            If IsTrue(hSwitchToThisWindow) Then Call Dword hSwitchToThisWindow Using SwitchToThisWindow(hwnd, %True) To lResult: Sleep 1
            hWndF = GetForegroundWindow: If hwnd = hWndF Then Exit Sub
            
            ShowWindow hWnd, %SW_MINIMIZE
         Loop
    
      End Sub
    
      CallBack Function DlgProc
         Select Case CbMsg
            Case %WM_ACTIVATEAPP
               If IsFalse(CbWparam) Then
                  SetWindowPos CbHndl, %HWND_NOTOPMOST, 0, 0, 0, 0, %SWP_NOMOVE Or %SWP_NOSIZE Or %SWP_NOACTIVATE
                  ShowWindow GetForegroundWindow, %SW_SHOWNA
                  SetTimer CbHndl, 1, 5000, ByVal 0&
               Else
                  SetWindowPos CbHndl, %HWND_TOPMOST, 0, 0, 0, 0, %SWP_NOMOVE Or %SWP_NOSIZE Or %SWP_NOACTIVATE
                  KillTimer CbHndl, 1
               End If
            Case %WM_TIMER: pSetForegroundWindow CbHndl
         End Select
      End Function
    
      Function PbMain()
         Local hDlg As Long
         Dialog New 0 ,"I am here", , , 190, 60, %WS_CAPTION Or %WS_MINIMIZEBOX Or %WS_SYSMENU, %WS_EX_TOPMOST To hDlg
         Control Add TextBox, hDlg, 101, "", 10, 10, 170, 12
         Control Add TextBox, hDlg, 102, "", 10, 30, 170, 12
         Dialog Show Modal hDlg Call DlgProc
      End Function
    ------------------
    E-MAIL: [email protected]

    Comment


    • #3
      Thanks that helped

      IF IsWindowVisible(hfrmMaX&) = %FALSE THEN
      ShowWindow hfrmMaX&, %SW_RESTORE
      ELSE
      SetForegroundWindow hfrmMaX&
      END IF


      Jason

      ------------------


      [This message has been edited by jforgey (edited January 19, 2001).]

      Comment


      • #4
        Jason --

        I'd suggest searching this BBS for the word "SetForegroundWindow". Based on your last message I'm guessing that you're using Windows 95 or NT4... You'll find that SetForegroundWindow no longer works the way you expect on Windows 200, 98, or ME. You'll also find some spirited discussions about 1) whether or not it is a good idea to try to force your window to the foreground, and 2) whether or not the various workarounds are reliable.

        -- Eric


        ------------------
        Perfect Sync: Perfect Sync Development Tools
        Email: mailto:[email protected][email protected]</A>
        "Not my circus, not my monkeys."

        Comment


        • #5
          I just do a showWindow and then set focus to one of it's controls
          such as this. It works within one app.

          eg.

          DIALOG SHOW STATE hDlg, %SW_RESTORE
          CONTROL SET FOCUS hDlg, %bnClose

          ------------------
          Brent Boshart

          Comment


          • #6
            Works Great!!

            Have you tested this with WIN98/2000???


            IF IsWindowVisible(hfrmMaX&) = %FALSE THEN
            showWindow hfrmMaX&, %SW_RESTORE
            ELSE
            CONTROL SET FOCUS hfrmMaX&, %FRMMAX_LBLMAXSIZE
            END IF

            Thanks
            Jason

            ------------------

            Comment


            • #7
              OOPs!!

              Spoke to soon. If I click to other windows without hiding app the click the icon in task bar its works
              the 1st 2 times then quits.

              If I hide the window 1st then is works.

              I'm running on NT 4.0

              Jason

              ------------------

              Comment


              • #8
                -Eric

                You said you would post some source code that delt with this issue, were you able too?

                I tried to follow your posts, but I couldn't determin what the solution was.

                Jason

                ------------------

                Comment


                • #9
                  I was never able to come up with a foolproof method, other than the one we use in our Console Tools "ConsoleToForeground" function. And that is specific to console applications, so it would not do you any good.

                  There are significant problems with all of the GUI methods that I have ever seen posted here or elsewhere. Some methods work well, but they are very disruptive. For example, they require you to minimize and then restore your app, which most people would object to.

                  -- Eric


                  ------------------
                  Perfect Sync: Perfect Sync Development Tools
                  Email: mailto:[email protected][email protected]</A>
                  "Not my circus, not my monkeys."

                  Comment

                  Working...
                  X