We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,389 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Setting focus on an external application

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

3
Contributors
3
Replies
3 Years
Discussion Span
1 Year Ago
Last Updated
5
Views
spinnaret
Newbie Poster
16 posts since Sep 2008
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 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 :)

Teme64
Veteran Poster
1,040 posts since Aug 2008
Reputation Points: 218
Solved Threads: 206
Skill Endorsements: 5

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
Teme64
Veteran Poster
1,040 posts since Aug 2008
Reputation Points: 218
Solved Threads: 206
Skill Endorsements: 5

tx guys ... u r really helping me out

eryrasa
Newbie Poster
1 post since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0656 seconds using 2.63MB