Activate a window to forground using API

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • D Witherspoon

    #1

    Activate a window to forground using API

    I developed this module, examples on the internet seem to be much longer..
    Can someone explain to me why my solution doesn't work?



    Module modAPI

    Declare Function SetForegroundWi ndow Lib "user32" (ByVal hwnd As Long) As
    Long



    Public Sub ActivateAppWind ow()

    SetForegroundWi ndow(fMain.Hand le.ToInt32)

    End Sub

    End Module


  • Ken Tucker [MVP]

    #2
    Re: Activate a window to forground using API

    Hi,

    API declare is wrong. Types have changed since vb6. Use integer
    instead of long. In this case i would use intptr.

    Declare Function SetForegroundWi ndow Lib "user32" (ByVal hwnd As intptr) As
    integer

    Public Sub ActivateAppWind ow()

    SetForegroundWi ndow(fMain.Hand le)

    End Sub


    Ken
    ----------------

    "D Witherspoon" <dwitherspoon@n oway.org> wrote in message
    news:unEqejNVFH A.3584@TK2MSFTN GP14.phx.gbl...
    I developed this module, examples on the internet seem to be much longer..
    Can someone explain to me why my solution doesn't work?



    Module modAPI

    Declare Function SetForegroundWi ndow Lib "user32" (ByVal hwnd As Long) As
    Long



    Public Sub ActivateAppWind ow()

    SetForegroundWi ndow(fMain.Hand le.ToInt32)

    End Sub

    End Module



    Comment

    • Crouchie1998

      #3
      Re: Activate a window to forground using API

      Module Module1
      Public Declare Function SetForegroundWi ndow Lib "user32" (ByVal hwnd As
      Integer) As Integer

      Public Sub SetForeground(B yVal iHandle As Integer)
      SetForegroundWi ndow(iHandle)
      End Sub
      End Module

      Usage:
      -------

      SetForeground (frm.Handle.Int 32)

      'frm' is the Form to set as the foreground window

      Example:
      ---------

      Declaration:
      ------------

      Dim frm As New Form2

      Behind Button1 (for example):
      ------------------------------

      frm.Show()

      Behind Button2 (for example):
      ------------------------------

      SetForeground (frm.Handle.Int 32)

      Crouchie1998
      BA (HONS) MCP MCSE


      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Activate a window to forground using API

        "Ken Tucker [MVP]" <vb2ae@bellsout h.net> schrieb:[color=blue]
        > API declare is wrong. Types have changed since vb6. Use integer
        > instead of long. In this case i would use intptr.
        >
        > Declare Function SetForegroundWi ndow Lib "user32" (ByVal hwnd As intptr)
        > As
        > integer
        >
        > Public Sub ActivateAppWind ow()
        >
        > SetForegroundWi ndow(fMain.Hand le)[/color]

        Note that this will only work as long as your application is the active
        application. Otherwise 'AttachThreadIn put' is needed too. In other words,
        it's not as easy (and not recommended) to "steal" the focus from another
        application.

        --
        M S Herfried K. Wagner
        M V P <URL:http://dotnet.mvps.org/>
        V B <URL:http://classicvb.org/petition/>

        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: Activate a window to forground using API

          "Crouchie19 98" <crouchie1998@s pamcop.net> schrieb:[color=blue]
          > Public Declare Function SetForegroundWi ndow Lib "user32" (ByVal hwnd As
          > Integer) As Integer[/color]

          I prefer this declaration/usage, although your code should work:

          \\\
          Public Declare Function SetForegroundWi ndow Lib "user32.dll " ( _
          ByVal hWnd As IntPtr _
          ) As Boolean
          ..
          ..
          ..
          Dim Success As Boolean = SetForegroundWi ndow(frm.Handle )
          ///

          Just my 2 Euro cents...

          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://classicvb.org/petition/>

          Comment

          • Cor Ligthert

            #6
            Re: Activate a window to forground using API

            D,

            These questions make me forever curious.

            Although as Herfried already said have I the same opinion that you should
            avoid this kind of instruction.

            Why not just
            me.topmost = true

            Cor


            Comment

            • Herfried K. Wagner [MVP]

              #7
              Re: Activate a window to forground using API

              Cor,

              "Cor Ligthert" <notmyfirstname @planet.nl> schrieb:[color=blue]
              > Although as Herfried already said have I the same opinion that you should
              > avoid this kind of instruction.
              >
              > Why not just
              > me.topmost = true[/color]

              I doubt that a topmost window is a viable solution in this situation...

              --
              M S Herfried K. Wagner
              M V P <URL:http://dotnet.mvps.org/>
              V B <URL:http://classicvb.org/petition/>

              Comment

              • Cor Ligthert

                #8
                Re: Activate a window to forground using API

                > "Cor Ligthert" <notmyfirstname @planet.nl> schrieb:[color=blue][color=green]
                >> Although as Herfried already said have I the same opinion that you should
                >> avoid this kind of instruction.
                >>
                >> Why not just
                >> me.topmost = true[/color]
                >
                > I doubt that a topmost window is a viable solution in this situation...
                >[/color]


                After that I had sent this message I thought,
                me.topmost = true
                me.topmost = false

                Maybe does that the job that is asked, however I am more interested in that
                statusbar problem, but the information from the OP is to few and I think
                that this is a on the border solution.

                Cor


                Comment

                • D Witherspoon

                  #9
                  Re: Activate a window to forground using API

                  Me.TopMost will make a form the topmost in the application but not over
                  other applications.

                  When you refer to the status bar problem are you talking about my problem
                  with the app not showing up in the taskbar.

                  It's tricky and yes, is the only reason I have to resort to trying to bring
                  the app window into the foreground. Unfortunatly no luck yet with the
                  status bar thing.

                  "Cor Ligthert" <notmyfirstname @planet.nl> wrote in message
                  news:%23bfZGUUV FHA.2540@tk2msf tngp13.phx.gbl. ..[color=blue][color=green]
                  >> "Cor Ligthert" <notmyfirstname @planet.nl> schrieb:[color=darkred]
                  >>> Although as Herfried already said have I the same opinion that you
                  >>> should avoid this kind of instruction.
                  >>>
                  >>> Why not just
                  >>> me.topmost = true[/color]
                  >>
                  >> I doubt that a topmost window is a viable solution in this situation...
                  >>[/color]
                  >
                  >
                  > After that I had sent this message I thought,
                  > me.topmost = true
                  > me.topmost = false
                  >
                  > Maybe does that the job that is asked, however I am more interested in
                  > that statusbar problem, but the information from the OP is to few and I
                  > think that this is a on the border solution.
                  >
                  > Cor
                  >[/color]


                  Comment

                  • D Witherspoon

                    #10
                    Re: Activate a window to forground using API

                    Not recommended as in technically or morally ?


                    "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
                    news:%23IFCQ2NV FHA.2124@TK2MSF TNGP14.phx.gbl. ..[color=blue]
                    > "Ken Tucker [MVP]" <vb2ae@bellsout h.net> schrieb:[color=green]
                    >> API declare is wrong. Types have changed since vb6. Use integer
                    >> instead of long. In this case i would use intptr.
                    >>
                    >> Declare Function SetForegroundWi ndow Lib "user32" (ByVal hwnd As intptr)
                    >> As
                    >> integer
                    >>
                    >> Public Sub ActivateAppWind ow()
                    >>
                    >> SetForegroundWi ndow(fMain.Hand le)[/color]
                    >
                    > Note that this will only work as long as your application is the active
                    > application. Otherwise 'AttachThreadIn put' is needed too. In other
                    > words, it's not as easy (and not recommended) to "steal" the focus from
                    > another application.
                    >
                    > --
                    > M S Herfried K. Wagner
                    > M V P <URL:http://dotnet.mvps.org/>
                    > V B <URL:http://classicvb.org/petition/>[/color]


                    Comment

                    • Cor Ligthert

                      #11
                      Re: Activate a window to forground using API

                      D,
                      [color=blue]
                      > Me.TopMost will make a form the topmost in the application but not over
                      > other applications.
                      >[/color]
                      Than I have probably another system. When I use topmost = true I get the
                      form only from my screen when I minimalize it.

                      Cor


                      Comment

                      • Herfried K. Wagner [MVP]

                        #12
                        Re: Activate a window to forground using API

                        "D Witherspoon" <dwitherspoon@n oway.org> schrieb:[color=blue]
                        > Not recommended as in technically or morally ?[/color]

                        Morally -- I doubt that the application will pass the Windows Logo test when
                        it's stealing the focus.

                        --
                        M S Herfried K. Wagner
                        M V P <URL:http://dotnet.mvps.org/>
                        V B <URL:http://classicvb.org/petition/>

                        Comment

                        • Herfried K. Wagner [MVP]

                          #13
                          Re: Activate a window to forground using API

                          Cor,

                          "Cor Ligthert" <notmyfirstname @planet.nl> schrieb:[color=blue][color=green]
                          >> Me.TopMost will make a form the topmost in the application but not over
                          >> other applications.[/color]
                          >
                          > Than I have probably another system. When I use topmost = true I get the
                          > form only from my screen when I minimalize it.[/color]

                          ACK. MSDN:

                          | A topmost form is a form that overlaps all the other (non-topmost)
                          | forms even if it is not the active or foreground form. Topmost forms
                          | are always displayed at the highest point in the Z-order of the
                          | windows on the desktop.

                          --
                          M S Herfried K. Wagner
                          M V P <URL:http://dotnet.mvps.org/>
                          V B <URL:http://classicvb.org/petition/>

                          Comment

                          • D Witherspoon

                            #14
                            Re: Activate a window to forground using API

                            Thats a good point.

                            But until I can find out why it deosn't show up in the task (when it first
                            opens) bar I will have to do this.

                            "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
                            news:%23EUkAbWV FHA.2560@TK2MSF TNGP10.phx.gbl. ..[color=blue]
                            > "D Witherspoon" <dwitherspoon@n oway.org> schrieb:[color=green]
                            >> Not recommended as in technically or morally ?[/color]
                            >
                            > Morally -- I doubt that the application will pass the Windows Logo test
                            > when it's stealing the focus.
                            >
                            > --
                            > M S Herfried K. Wagner
                            > M V P <URL:http://dotnet.mvps.org/>
                            > V B <URL:http://classicvb.org/petition/>[/color]


                            Comment

                            • D Witherspoon

                              #15
                              Re: Activate a window to forground using API

                              It works now without even requiring 'AttachThreadIn put'. I changed the
                              declare to return an Interger instead of a Long. Seemed to fix it and
                              return 1 as the return code instead of a crazy long number.

                              Even if it isn't the active application it still works, 'AttachThreadIn put'
                              doesn't seem to be required.


                              "D Witherspoon" <dwitherspoon@n oway.org> wrote in message
                              news:u6tWZzWVFH A.3044@TK2MSFTN GP10.phx.gbl...[color=blue]
                              > Thats a good point.
                              >
                              > But until I can find out why it deosn't show up in the task (when it first
                              > opens) bar I will have to do this.
                              >
                              > "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
                              > news:%23EUkAbWV FHA.2560@TK2MSF TNGP10.phx.gbl. ..[color=green]
                              >> "D Witherspoon" <dwitherspoon@n oway.org> schrieb:[color=darkred]
                              >>> Not recommended as in technically or morally ?[/color]
                              >>
                              >> Morally -- I doubt that the application will pass the Windows Logo test
                              >> when it's stealing the focus.
                              >>
                              >> --
                              >> M S Herfried K. Wagner
                              >> M V P <URL:http://dotnet.mvps.org/>
                              >> V B <URL:http://classicvb.org/petition/>[/color]
                              >
                              >[/color]


                              Comment

                              Working...