1,105,214 Community Members

Setting focus on an external application

Member Avatar
spinnaret
Newbie Poster
16 posts since Sep 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

I have an application which calls an external process. I would like this process to be open as soon as it is called. i.e. with no user interaction.

This is because I would like to send Keys to it to interact.
I know how crude this is but this is a thrid party application that my boss has promised will open is a specific way.

Dim procSungard As System.Diagnostics.Process = _
           New System.Diagnostics.Process()
           procSungard.StartInfo.FileName = _
           System.Environment.GetFolderPath_
          (Environment.SpecialFolder.ProgramFiles)&_
          "sungard/sungard.exe"
          procSungard.StartInfo.WindowStyle = _
          System.Diagnostics.ProcessWindowStyle.Normal


           If iCode = 1 Then

            procSungard.Start()

            System.Threading.Thread.Sleep(100)
            System.Windows.Forms.SendKeys.Send("{Tab}")
            End If

So I declare the process, get its expected location and set a window style.
Is there any way to give it focus on load?
Spinner

Member Avatar
Teme64
Veteran Poster
1,039 posts since Aug 2008
Reputation Points: 189 [?]
Q&As Helped to Solve: 208 [?]
Skill Endorsements: 9 [?]
 
0
 

100 ms is a quite short time to application load. Instead of System.Threading.Thread.Sleep(100) you may try procSungard.WaitForInputIdle(10000) . You'll have a higher chances that the app is actually loaded, UI running and thus app is processing message queue.
Setting focus requires API calls.
Get handle to application window: Public Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long And set focus: Public Declare Function SetFocusAPI Lib "user32.dll" Alias "SetFocus" (ByVal hWnd As Long) As Long so the "set focus to Sungard app" would look something like this

Dim hWnd As Long
hWnd = FindWindow(vbNullString, "Sungard")
SetFocusAPI(hWnd)
' SendKeys

I didn't test the code but it's quite simple. One thing I do know for sure, SendKeys is never a 100% sure way to send keys :)

Member Avatar
Teme64
Veteran Poster
1,039 posts since Aug 2008
Reputation Points: 189 [?]
Q&As Helped to Solve: 208 [?]
Skill Endorsements: 9 [?]
 
0
 

Just happened to see this:

Private Declare Function OpenIcon Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As 
Long) As Long

Dim hWnd As Long
hWnd = FindWindow(vbNullString, "Sungard")
OpenIcon hWnd
SetForegroundWindow hWnd
' SendKeys
Member Avatar
eryrasa
Newbie Poster
1 post since Feb 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

tx guys ... u r really helping me out

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article