Using API to set focus to application

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Martin2007
    New Member
    • Jul 2007
    • 21

    Using API to set focus to application

    Hi,

    I'm trying to use SendKeys to control an application, however it keeps losing focus when i call it, cuold anyone tell me how to set the focus to the window using the API?

    Cheers

    (Visual Basic 2005 Express)
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Originally posted by Martin2007
    I'm trying to use SendKeys to control an application, however it keeps losing focus when i call it, cuold anyone tell me how to set the focus to the window using the API? (Visual Basic 2005 Express)
    Not sure you need to use the API. I don't know about 2005, but in VB6 there's an AppActivate statement which sets focus to another window. Perhaps you can use that?

    Comment

    • chezz
      New Member
      • Jun 2007
      • 13

      #3
      I know that vb 6 that if u have a text box or something you can do this

      Code:
      TextBox.SetFocus
      This places the focus on the specified object in this case the text box.

      It should be something similar to vb 2005

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        I assumed we were talking about setting the focus to another application. I could be wrong, of course.

        Comment

        • hariharanmca
          Top Contributor
          • Dec 2006
          • 1977

          #5
          Originally posted by Martin2007
          Hi,

          I'm trying to use SendKeys to control an application, however it keeps losing focus when i call it, cuold anyone tell me how to set the focus to the window using the API?

          Cheers

          (Visual Basic 2005 Express)
          can you explain

          Why and what exactly you want to do? (if no problem, you can post what you had tried. Because, we are not getting your problem properly).

          Comment

          • Martin2007
            New Member
            • Jul 2007
            • 21

            #6
            Thanks for all the replies...

            I'm trying to control a second application from my own VB 2005 project... In an ideal world i would like it to happen all in the background.

            Basically i create a new instance of an applicaiton, use sendkeys to make it open a new command file, then again to force it to execute the command file, however when it opens it can often lose focus, so i wanted a call (i thought to the api) to ensure that the window is focused.

            I originally wrote this in vb for applications and used the appactiviate with success, but unfortunately I can't find a direct replacement for it in 2005.

            This is my code to launch and control the application...

            Code:
                 Private Sub launchApplication()
                    Dim Application As Process = New Process()
                    Me.WindowState = FormWindowState.Minimized
                   Application.StartInfo.FileName = sPath & "\Application.exe"
                   Application.StartInfo.WindowStyle = _
                       ProcessWindowStyle.Maximized
                    Try
                        Application.Start()
                    Catch ex As Exception
                        Me.WindowState = FormWindowState.Normal
                        Err.Raise(53)
                    End Try
                    Application.WaitForInputIdle(4000)
                    SendKeys.Send("%F")
                    SendKeys.Send("O")
                    SendKeys.Flush()
                    Application.WaitForInputIdle(100)
                    SendKeys.SendWait(sPath & "\" & commandFileName)
                    SendKeys.Flush()
                    SendKeys.SendWait("~")
                    SendKeys.Flush()
                    SendKeys.SendWait("%FE")
                    Application.WaitForExit(60000)
                    If Not Application.HasExited Then
                        Application.Kill()
                    End If
                    Me.WindowState = FormWindowState.Normal
                    MsgBox("Done")
                End Sub
            As I said above, in an ideal world I would like the application to never appear (as it requires no user input), my second question is to ask can you launch the application hidden and set the focus to it? (i know u can launch with the startup info set to hidden, just wondering if it is possible to focus to an invisible window)

            Thanks for all the help guys!
            Last edited by Martin2007; Jul 26 '07, 08:13 AM. Reason: code mistake

            Comment

            • hariharanmca
              Top Contributor
              • Dec 2006
              • 1977

              #7
              Originally posted by Martin2007
              Thanks for all the replies...

              I'm trying to control a second application from my own VB 2005 project... In an ideal world i would like it to happen all in the background.

              Basically i create a new instance of an applicaiton, use sendkeys to make it open a new command file, then again to force it to execute the command file, however when it opens it can often lose focus, so i wanted a call (i thought to the api) to ensure that the window is focused.

              I originally wrote this in vb for applications and used the appactiviate with success, but unfortunately I can't find a direct replacement for it in 2005.

              This is my code to launch and control the application...

              Code:
                   Private Sub launchApplication()
                      Dim Application As Process = New Process()
                      Me.WindowState = FormWindowState.Minimized
                     Application.StartInfo.FileName = sPath & "\Application.exe"
                     Application.StartInfo.WindowStyle = _
                         ProcessWindowStyle.Maximized
                      Try
                          Application.Start()
                      Catch ex As Exception
                          Me.WindowState = FormWindowState.Normal
                          Err.Raise(53)
                      End Try
                      Application.WaitForInputIdle(4000)
                      SendKeys.Send("%F")
                      SendKeys.Send("O")
                      SendKeys.Flush()
                      Application.WaitForInputIdle(100)
                      SendKeys.SendWait(sPath & "\" & commandFileName)
                      SendKeys.Flush()
                      SendKeys.SendWait("~")
                      SendKeys.Flush()
                      SendKeys.SendWait("%FE")
                      Application.WaitForExit(60000)
                      If Not Application.HasExited Then
                          Application.Kill()
                      End If
                      Me.WindowState = FormWindowState.Normal
                      MsgBox("Done")
                  End Sub
              As I said above, in an ideal world I would like the application to never appear (as it requires no user input), my second question is to ask can you launch the application hidden and set the focus to it? (i know u can launch with the startup info set to hidden, just wondering if it is possible to focus to an invisible window)

              Thanks for all the help guys!


              Hi Martin2007,
              1. This will happen if your code does not refer any event handling.
              2. If it refers, it won’t be invisible.
              3. If the window is accessing in invisible mode then your mouse pointer will not give command to other application.
              4. That other application should controlled by your application (That app. should not have event Control).
              5. if we consider that it happens, your application will send some key, if you press any key which is going to operate other application.



              I am not sure with your code. But just think your code is satisfy my above Query?

              Comment

              • Martin2007
                New Member
                • Jul 2007
                • 21

                #8
                Originally posted by hariharanmca
                Hi Martin2007,
                1. This will happen if your code does not refer any event handling.
                2. If it refers, it won’t be invisible.
                3. If the window is accessing in invisible mode then your mouse pointer will not give command to other application.
                4. That other application should controlled by your application (That app. should not have event Control).
                5. if we consider that it happens, your application will send some key, if you press any key which is going to operate other application.



                I am not sure with your code. But just think your code is satisfy my above Query?
                i'm a little new to this, so i'm not sure I understand... but i will try.

                my application does not need any event handling from the other application, it just needs to set it running, which i think u say means it can be invisible?

                the idea is that the user does not press any keys while the other application is launched, and once it has started to execute the other application will not respond to keypresses anyway. Basically the user presses a button in my application, a message box pops up saying that the other application is running, please wait.. then my application continues on... there is no interface once the other application has been stared.

                Comment

                • hariharanmca
                  Top Contributor
                  • Dec 2006
                  • 1977

                  #9
                  Originally posted by Martin2007
                  i'm a little new to this, so i'm not sure I understand... but i will try.

                  my application does not need any event handling from the other application, it just needs to set it running, which i think u say means it can be invisible?

                  the idea is that the user does not press any keys while the other application is launched, and once it has started to execute the other application will not respond to keypresses anyway. Basically the user presses a button in my application, a message box pops up saying that the other application is running, please wait.. then my application continues on... there is no interface once the other application has been stared.
                  Originally posted by Martin2007
                  the idea is that the user does not press any keys while the other application is launched, and once it has started to execute the other application will not respond to keypresses anyway.
                  then why you want, your application invisible? (As you told, Other App. will not work (Events), when your App. is started.)

                  Originally posted by Martin2007
                  Basically the user presses a button in my application, a message box pops up saying that the other application is running, please wait.. then my application continues on...
                  If your App. is invisible(User dont know your App. is running). then how can you say other App. is Running(in Message Box).

                  Originally posted by Martin2007
                  there is no interface once the other application has been stared.
                  Then what is the necessary to make all this commands.

                  Just go through what is your requirement.
                  I think, you are not aware of what you are going to do.

                  Comment

                  • Martin2007
                    New Member
                    • Jul 2007
                    • 21

                    #10
                    I dont think you understand me... i will try to be clear.

                    My application is visible, and allways needs to be...

                    I need to send commands to a second application to make it do something...

                    The second application (not my own) needs to be hidden from view, it just needs to run then close (which it will do)...

                    Basically i need to open the other application, send it a sequency of keystrokes, then leave it alone...

                    Hope this clarifies...

                    Comment

                    • hariharanmca
                      Top Contributor
                      • Dec 2006
                      • 1977

                      #11
                      Originally posted by Martin2007
                      I dont think you understand me... i will try to be clear.

                      My application is visible, and allways needs to be...

                      I need to send commands to a second application to make it do something...

                      The second application (not my own) needs to be hidden from view, it just needs to run then close (which it will do)...

                      Basically i need to open the other application, send it a sequency of keystrokes, then leave it alone...

                      Hope this clarifies...

                      is your above code is working fine? then you carry On.

                      Originally posted by Martin2007
                      As I said above, in an ideal world I would like the application to never appear (as it requires no user input), my second question is to ask can you launch the application hidden and set the focus to it? (i know u can launch with the startup info set to hidden, just wondering if it is possible to focus to an invisible window)
                      You mention this, there the problem start for us.

                      Comment

                      • Killer42
                        Recognized Expert Expert
                        • Oct 2006
                        • 8429

                        #12
                        Martin2007, it looks as though you used the Report button instead of the Reply button. (A mistake I think we've all made from time to time.)

                        Please send again.

                        Comment

                        • Martin2007
                          New Member
                          • Jul 2007
                          • 21

                          #13
                          Originally posted by Killer42
                          Martin2007, it looks as though you used the Report button instead of the Reply button. (A mistake I think we've all made from time to time.)

                          Please send again.

                          Sorry for the report...

                          The code above does work fine... some of the time... that is the problem which I have been trying to communicate to you.

                          back to my original question.... can you use the API to force the focus onto an application while it is not visible... that is all i ask... thanks..

                          Comment

                          • Killer42
                            Recognized Expert Expert
                            • Oct 2006
                            • 8429

                            #14
                            Originally posted by Martin2007
                            back to my original question.... can you use the API to force the focus onto an application while it is not visible... that is all i ask... thanks..
                            My guess would be... minimised, yes. Invisible, probably not.
                            Don't know, though. This might be a question for the Windows forum, as it's not specific to VB and we don't seem to have the answer.

                            Comment

                            Working...