 |
|
 |
notmeagain Level: Trainee
 Registered: 08-09-2006 Posts: 3
|
Setfocus to an external window - Please help
Hello all, i thought i'd try my luck here.
Problem:
I am using Visual Basic .net, and i'm having difficulty in setting a windows focus...
I have to send a string of key presses to a particular application, and that works fine, If their is no input from the user.
But if the user does stray away from the active window the program still continues to send the keys into whatever window the user has open. Annoying.
The application is launched via
Dim NewProcess As Process = New Process
NewProcess.StartInfo.FileName = filename 'file to start = Filename
NewProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal ' Normal window style
NewProcess.EnableRaisingEvents = False 'Enable events to take control?
txtlog.Text += "Preparing to launch " & filename & vbNewLine
Try 'Check for errors
NewProcess.Start()
NewProcess.WaitForInputIdle(2000)
Catch exc As System.ComponentModel.Win32Exception
txtlog.Text += "ABORT DUE TO ERROR: NOT FOUND" & vbNewLine
'thrdNotFound()
Exit Sub
End Try |
and there is a simple findwindow loop which checks for when the window has loaded, and it then sends the keys using | System.Windows.Forms.SendKeys.SendWait("{DOWN}") |
i need a way to check before each key press that the desired window is active, and if it isn't then to activate it.
all of this is run as a new thread, as there are other background operations going on and in between the key presses there is a thread.sleep(100) delay.
I've tried varying methods like SetForeGroundWindow()...
i can detect when the user has moved away from the window by using the "Targetwindow = FindWindowByTitle(windowtitle)" api, and using
Dim test As IntPtr
Dim hwnd As Integer
hwnd = GetForegroundWindow()
If hwnd <> TargetWindow Then
txtlog.Text += "Target window changed! New Target = " & hwnd & vbNewLine
good = False
End If
If good = False Then
'txtlog.Text += "PAUSED"
NewThread1.Suspend()
'threadCheckWindow.Abort()
Else
good = True
End If
End Sub
|
its just i'm unable to then force the input focus onto the desired window, using any means of ShowWindow(hwnd,style)...
im totally stumped!
Any help will be greatly appreciated!
W.G.
Ps i appear to have posted in the wrong forum, great start eh? 
[Edited by notmeagain on 08-09-2006 at 09:51 PM GMT]
[Edited by notmeagain on 08-09-2006 at 09:52 PM GMT]
|
|
08-09-2006 at 09:43 PM |
|
|
|
|
 |
 |