Set Focus to another programs window?

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

    #1

    Set Focus to another programs window?

    I am using VB.NET to control another program. I have the program's window
    hwnd; however, I need to be able to set the focus to that other programs
    window so I can send it some keystrokes. I thought I could just use the
    SetFocus API, but I read somewhere that it will only work for programs
    running under that programs thread. Is this true? If so, how can I set focus
    to another program's window via VB.NET? Thanks.

    Tom


  • Crouchie1998

    #2
    RE: Set Focus to another programs window?

    Hi

    First of all 'hwnd' is now 'Handle' in VB.NET

    The easiest way would be to use 'AppActivate' if you know the window title:

    AppActivate("Un titled - Notepad")

    ---

    Another simple way is:

    Dim intNotePad As Integer = Shell("C:\WINNT \Notepad.exe",
    AppWinStyle.Min imizedNoFocus)

    AppActivate(int NotePad)

    ---

    I cannot see any point using FinWindow API if you already have the Handle of
    that window.

    "Tom" wrote:
    [color=blue]
    > I am using VB.NET to control another program. I have the program's window
    > hwnd; however, I need to be able to set the focus to that other programs
    > window so I can send it some keystrokes. I thought I could just use the
    > SetFocus API, but I read somewhere that it will only work for programs
    > running under that programs thread. Is this true? If so, how can I set focus
    > to another program's window via VB.NET? Thanks.
    >
    > Tom
    >
    >
    >[/color]

    Comment

    • Tom

      #3
      Re: Set Focus to another programs window?

      Crouchie: Thanks, forgot about AppActivate.

      One question, though: Is the process handle the same and the 'Handle' (i.e.
      old hwnd)?

      Tom

      "Crouchie19 98" <Crouchie1998@d iscussions.micr osoft.com> wrote in message
      news:A4C57854-BADD-4C99-80BA-1F32B1E5B396@mi crosoft.com...[color=blue]
      > Hi
      >
      > First of all 'hwnd' is now 'Handle' in VB.NET
      >
      > The easiest way would be to use 'AppActivate' if you know the window[/color]
      title:[color=blue]
      >
      > AppActivate("Un titled - Notepad")
      >
      > ---
      >
      > Another simple way is:
      >
      > Dim intNotePad As Integer = Shell("C:\WINNT \Notepad.exe",
      > AppWinStyle.Min imizedNoFocus)
      >
      > AppActivate(int NotePad)
      >
      > ---
      >
      > I cannot see any point using FinWindow API if you already have the Handle[/color]
      of[color=blue]
      > that window.
      >
      > "Tom" wrote:
      >[color=green]
      > > I am using VB.NET to control another program. I have the program's[/color][/color]
      window[color=blue][color=green]
      > > hwnd; however, I need to be able to set the focus to that other programs
      > > window so I can send it some keystrokes. I thought I could just use the
      > > SetFocus API, but I read somewhere that it will only work for programs
      > > running under that programs thread. Is this true? If so, how can I set[/color][/color]
      focus[color=blue][color=green]
      > > to another program's window via VB.NET? Thanks.
      > >
      > > Tom
      > >
      > >
      > >[/color][/color]


      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: Set Focus to another programs window?

        "Tom" <tom@nospam.com > schrieb:[color=blue]
        >I am using VB.NET to control another program. I have the program's window
        > hwnd; however, I need to be able to set the focus to that other programs
        > window so I can send it some keystrokes.[/color]

        See:

        <URL:http://www.google.de/groups?selm=uKq v47yaEHA.3820%4 0tk2msftngp13.p hx.gbl>

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

        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: Set Focus to another programs window?

          "Tom" <tom@nospam.com > schrieb:[color=blue]
          > One question, though: Is the process handle the same and the 'Handle'
          > (i.e.
          > old hwnd)?[/color]

          No. Process handles are different from window handles.

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

          Comment

          • Tom

            #6
            Re: Set Focus to another programs window?

            Herfried: Does this just bring the window to the front? Or does it also set
            focus to the window?

            "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
            news:uzHkx5wAFH A.3016@tk2msftn gp13.phx.gbl...[color=blue]
            > "Tom" <tom@nospam.com > schrieb:[color=green]
            > >I am using VB.NET to control another program. I have the program's window
            > > hwnd; however, I need to be able to set the focus to that other programs
            > > window so I can send it some keystrokes.[/color]
            >
            > See:
            >
            >[/color]
            <URL:http://www.google.de/groups?selm=uKq...ftngp13.phx.gb
            l>[color=blue]
            >
            > --
            > M S Herfried K. Wagner
            > M V P <URL:http://dotnet.mvps.org/>
            > V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
            >[/color]


            Comment

            • Herfried K. Wagner [MVP]

              #7
              Re: Set Focus to another programs window?

              "Tom" <tom@nospam.com > schrieb:[color=blue]
              > Does this just bring the window to the front? Or does it also set
              > focus to the window?[/color]

              MSDN on 'SetForegroundW indow':

              "
              The 'SetForegroundW indow' function puts the thread that created the
              specified window into the foreground and activates the window. Keyboard
              input is directed to the window, and various visual cues are changed for the
              user.
              "

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

              Comment

              • Tom

                #8
                Re: Set Focus to another programs window?

                Herfried: One more question related to this: Is it possible to set the focus
                to another program's window and send it keystroks, YET still keep my VB
                program in the foreground (i.e. showing on the screen)?

                "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
                news:uzHkx5wAFH A.3016@tk2msftn gp13.phx.gbl...[color=blue]
                > "Tom" <tom@nospam.com > schrieb:[color=green]
                > >I am using VB.NET to control another program. I have the program's window
                > > hwnd; however, I need to be able to set the focus to that other programs
                > > window so I can send it some keystrokes.[/color]
                >
                > See:
                >
                >[/color]
                <URL:http://www.google.de/groups?selm=uKq...ftngp13.phx.gb
                l>[color=blue]
                >
                > --
                > M S Herfried K. Wagner
                > M V P <URL:http://dotnet.mvps.org/>
                > V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
                >[/color]


                Comment

                • Tom

                  #9
                  Re: Set Focus to another programs window?

                  Herfried: One more question related to this: Is it possible to set the focus
                  to another program's window and send it keystroks, YET still keep my VB
                  program in the foreground (i.e. showing on the screen)?

                  "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
                  news:eHHOfCzAFH A.3940@TK2MSFTN GP09.phx.gbl...[color=blue]
                  > "Tom" <tom@nospam.com > schrieb:[color=green]
                  > > Does this just bring the window to the front? Or does it also set
                  > > focus to the window?[/color]
                  >
                  > MSDN on 'SetForegroundW indow':
                  >
                  > "
                  > The 'SetForegroundW indow' function puts the thread that created the
                  > specified window into the foreground and activates the window. Keyboard
                  > input is directed to the window, and various visual cues are changed for[/color]
                  the[color=blue]
                  > user.
                  > "
                  >
                  > --
                  > M S Herfried K. Wagner
                  > M V P <URL:http://dotnet.mvps.org/>
                  > V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
                  >[/color]


                  Comment

                  Working...