Allow Only 1 Instance Of Your Program (And Activate Previous Instance)
Preparations
Copy the following
code to your form and compile it to exe file.
Now double click the exe file,
minimize it, and double click it again. Instead of launching another instance of
your program, the previous
instance is being activated.
Module Code
Declare Function
FindWindow Lib "user32" Alias "FindWindowA"
_
(ByVal lpClassName As
String, ByVal lpWindowName As String) As Long
Declare Function GetWindow Lib
"user32" (ByVal hwnd As Long,
_
ByVal wCmd As Long) As
Long
Declare Function OpenIcon Lib "user32" (ByVal hwnd As Long) As
Long
Declare Function SetForegroundWindow Lib "user32"
_
(ByVal hwnd As Long) As
Long
Public Const
GW_HWNDPREV = 3
Form Code
Private Sub
ShowPrevInstance()
Dim OldTitle As
String
Dim ll_WindowHandle As Long
'saving the current title in OldTitle
variable
'and changing the application
title
OldTitle = App.Title
App.Title = "abcba - This App Will Be Closed"
'finding the previous instance. if you are using VB
5.0,
'change "ThunderRT6Main" to
"ThunderRT5Main"
ll_WindowHandle =
FindWindow("ThunderRT6Main", OldTitle)
'if there is no old instances of your application -
exit.
If ll_WindowHandle = 0 Then Exit
Sub
'Find the window we need to
restore
ll_WindowHandle =
GetWindow(ll_WindowHandle, GW_HWNDPREV)
'Now restore it
Call
OpenIcon(ll_WindowHandle)
'And Bring
it to the foreground
Call
SetForegroundWindow(ll_WindowHandle)
End
End Sub
Private Sub Form_Load()
If App.PrevInstance Then
ShowPrevInstance
End Sub