borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Best/simplest way to save user input)Next Topic (Newer IN .Net) New Topic New Poll Post Reply
AndreaVB Forum : VB.Net : Setfocus to an external window - Please help
Poster Message
notmeagain
Level: Trainee

Registered: 08-09-2006
Posts: 3

icon 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
View Profile Send Email to User Show All Posts | Quote Reply
yronium
Level: Moderator


Registered: 14-04-2002
Posts: 907
icon Re: Setfocus to an external window - Please help

Hello, and welcome. Yep, it appears the wrong page. Unfortunately I can not help you with VB.Net, but I can use my Magic Moderator Power to move the topic in the .Net section, where somebody good-willing and knowing it can help you.
So, let's cross our fingers.

____________________________
Real Programmer can count up to 1024 on his fingers

09-09-2006 at 05:57 PM
View Profile Send Email to User Show All Posts | Quote Reply
notmeagain
Level: Trainee

Registered: 08-09-2006
Posts: 3
icon Re: Setfocus to an external window - Please help

Thanks for moving it .

I found a _STOP GAP_ measure using a thread that gets the window handle of the desired window, and constantly uses GetForeGroundWindow() api to detect when drifting, then i use AppActivate(processid/windowname) to bring the window to front, however if the user has mcafee installed, it doesnt work, the process just freezes and the thread terminates.

:\

how rare. Even disabling mcafee's auto protection doesnt help, so if anyone has a better method of "stealing focus back" i'd greatly appreciate any help.

11-09-2006 at 02:11 PM
View Profile Send Email to User Show All Posts | Quote Reply
notmeagain
Level: Trainee

Registered: 08-09-2006
Posts: 3
icon Re: Setfocus to an external window - Please help

quote:
notmeagain wrote:
Thanks for moving it .

I found a _STOP GAP_ measure using a thread that gets the window handle of the desired window, and constantly uses GetForeGroundWindow() api to detect when drifting, then i use AppActivate(processid/windowname) to bring the window to front, however if the user has mcafee installed, it doesnt work, the process just freezes and the thread terminates.

:\

how rare. Even disabling mcafee's auto protection doesnt help, so if anyone has a better method of "stealing focus back" i'd greatly appreciate any help.



Using a GetForegroundwindow and comparing against the desired hwnd, then setforgroundwindow(desiredhwnd) before every keysend seems to work, but only on machines without mcafee av... weird.
11-09-2006 at 05:06 PM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB.Net : Setfocus to an external window - Please help
Previous Topic (Best/simplest way to save user input)Next Topic (Newer IN .Net) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2007 Andrea Tincaniborder