Saturday, April 3, 2010

VB6 Code - Mencegah Aplikasi Dijalankan Dua Kali - Part-2

Di bawah ini merupakan prosedure VB6 kedua masih mengenai cara mencegah aplikasi dijalankan dua kali. Bagaimana implementasinya dalam Visual Basic 6.0? bisa Anda simak kodenya di bawah ini:
Option Explicit

Declare Function OpenIcon Lib "user32" (ByVal hWnd As Long) As Long
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 SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long

Public Const GW_HWNDPREV = 3

Sub ActivatePrevInstance()

Dim AppTitle As String
Dim PrevHndl As Long
Dim result As Long

AppTitle = App.Title
App.Title = "unwanted instance"

If PrevHndl = 0 Then
PrevHndl = FindWindow("ThunderRT6Main", AppTitle)
If PrevHndl <> 0 Then
PrevHndl = GetWindow(PrevHndl, GW_HWNDPREV)
result = OpenIcon(PrevHndl)
result = SetForegroundWindow(PrevHndl)
End
End If
End If

End Sub
Contoh penggunaan prosedure VB6 di atas:
Private Sub Form_Load()
If App.PrevInstance Then ActivatePrevInstance
End Sub