Link to home
Start Free TrialLog in
Avatar of xentix
xentix

asked on

VB6 If AppActive - lost

(code that errors)

If AppActivate("Example Program") >= 0 Then numbertxt.Text = numbertxt.Text - 1

I'm trying to simply make it If the (targeted "Example Program") program isnt running still *due to crash*, DON'T make the counter - 1

But it wont do that...

Compile Error: Expect function or variable.

(Below is the entire sub)

Private Sub Command1_Click()
Command1.Caption = "Stop"
Do
If GetAsyncKeyState(vbKeyRButton) < 0 Then
Command1.Caption = "Start"
retry.Enabled = True
GoTo suicide
End If
cont:
retry.Enabled = False
MoveMouseCursor doorx, doory
ButtonClick (vbLeftButton)
Pause (retry)
MoveMouseCursor passx, passy
ButtonClick (vbLeftButton)
Pause (retry)
AppActivate "Example Program" ', 1
'---
Clipboard.SetText (numbertxt.Text)
'---
Pause (retry)
MoveMouseCursor okayx, okayy
ButtonClick (vbLeftButton)
SendKeys ("^v")
MoveMouseCursor Text1, Text2
ButtonClick (vbLeftButton)
If AppActivate("Example Program") >= 0 Then numbertxt.Text = numbertxt.Text - 1
Pause (retry)
Loop
suicide:
End Sub


--Thanks
Avatar of Michael_D
Michael_D
Flag of Canada image

Looks like AppActivate() Is sub and not function so it cannot return value.
AppActivate title[, wait].

However, I dont know if you are familiar with Win32API, because what we can help you do is load the program yourself and change focus to it. APPACTIVATE is not a very efficent method of doing anything.


-Brian
Avatar of xentix
xentix

ASKER

AppActivate "Example Program", 1  wont work because then it just wants to have the program I'm building in focus to continue...

I do have a module added in with the available -
Declare Function SetFocusApi Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long

which I did see when I searched out Win32API, but I dont know hot to apply it...
SetFocus "Example Program"
--Wrong number of arguments or invalid property assignment
I definately dont know what to do with the function to make it focus...
I am not sure this will help but it will illustrate how to start, track and terminate an exe simply from w/in a VB app.

On a form place a button called cmdCalc.

Drop this code into the form:

Option Explicit
Dim rtn As Double

Private Sub cmdCalc_Click()
If rtn <> 0 Then
  On Error GoTo startnew
  AppActivate rtn
Else
startnew:
  Screen.MousePointer = vbHourglass
  rtn = Shell("calc.exe", vbNormalFocus)
  AppActivate rtn
  Screen.MousePointer = vbDefault
End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
On Error GoTo skipkill
AppActivate rtn
SendKeys "%{F4}", True   ' Send ALT+F4 to close Calculator.
skipkill:
Unload Me
End Sub
Avatar of xentix

ASKER

Using
  rtn = Shell("calc.exe", vbNormalFocus)
  AppActivate rtn
because its all I technically need,doesnt actually focus on the program, it loads multiple instances of the program...

And looking through the FaQ, I added in the coded to a module (which didnt work out - so I added it to the frm code itsself...)
Added into my code
GetForegroundWindow "Example Program"
And now get the error "only comments may appear after End Sub, End Function, and End Property" so it wont compile like this

A friend even suggested using
Declare Function FindWindow% Lib "user32" Alias "FindWindowA" (ByVal lpClassName$, ByVal lpWindowName$)

But even the[ FindWindow "Example Program" ] doesnt work so hot...

Wow why does this seem so complicated? All I need to do is check to see if the "Calculator" is still running... Not itsself... Obviously if ITs not running, it cant check :-P
ASKER CERTIFIED SOLUTION
Avatar of trkcorp
trkcorp

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of xentix

ASKER

Ooooo that worked... Thanks for explaining that out, trkcorp. I'm fairly new to VB (like no one could notice) :)