Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Activating an application 1

Status
Not open for further replies.

inexperienced1

IS-IT--Management
Aug 9, 2002
49
US
I have an application that is normally minimised

I use app.previnstance to stop multiple instances running, but I would also like to maximise the "already running" instance.

Is this possible and could anyone provide some outline code?

Many Thanks
 
If you know the name shown in the title bar of the application then use the AppActivate function.
 
I have tried appactivate but the programme that I am trying to start is the same as the one that I am running.

I cannot end the running programme as this would then not start appactivate.

Any other suggestions?

 
A simple solution is to change your own form's caption to some other value before calling AppActivate so that AppActivate does not try to activate your own program.
___
[tt]
Private Sub Form_Load()
If App.PrevInstance Then
Dim S As String
S = Caption
Caption = "Something else..."
AppActivate S
Unload Me
Else
'start program normally
End If
End Sub[/tt]
___

This code works fine... but still has a small problem.
If the previous instance of the program is minimized, AppActivate is not able to restore that minimized window, although the window is activated on the taskbar!

To work around this problem, here is some API based solution which does the trick neatly.
___
[tt]
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function OpenIcon Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

Private Sub Form_Load()
If App.PrevInstance Then
Dim S As String, H As Long
S = Caption
Caption = "Something else..."
H = FindWindow(vbNullString, S)
If IsIconic(H) Then OpenIcon H
SetForegroundWindow H
Unload Me
Else
'start program normally
End If
End Sub[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top