Selamat berkreatifitas sobat pecinta visual basic 6
Trik kali ini adalah mencegah aplikasi yang sama dijalankan lebih dari 1x. Kegunaan fitur ini untuk mencegah user menjalankan aplikasi lebih dari 1 dalam satu waktu.
Langsung saja ke sourcenya
1. Buat satu project
2. Buat Form dengan 1 textbox didalamnya
3. Masih di Form1. Masukkan Kode berikut ini
Dim PreviewsInstanceExists As Boolean
Private Sub Form_Load()
On Error GoTo ErrorHandler
PreviewsInstanceExists = True
Text1.LinkTopic = App.Title & "|" & Me.Name
Text1.LinkItem = "Text1"
Text1.LinkMode = 1 'If no Application is listening it will cause an error no DDE responce
'If the code continues without error means ths is not the first instance
'and we have connected with the first via DDE.
Text1.Text = "NEW INSTANCE OPENED " & Interaction.Command
Text1.LinkPoke
Unload Me
ErrorHandler:
If Err.Number = 282 Then
'Seems no application is listening in DDE this means its the first instance.
'Me.LinkMode = 1 'Done manualy
Me.LinkTopic = Me.Name
Me.Caption = "This is the first instance"
PreviewsInstanceExists = False
End If
End Sub
Private Sub Text1_Change()
'NOTE: Do not have any modal forms or textboxes popping up in the _Change event
'because after the commands in this event are executed a DDE transfer ok is sent
If (PreviewsInstanceExists = False) And Left(Text1.Text, 19) = "NEW INSTANCE OPENED" Then
Me.Visible = True
Me.WindowState = vbNormal
Me.SetFocus
If Len(Text1.Text) > 20 Then Label2.Caption = "Command line parameters of the other instance:" & vbCrLf & Mid(Text1.Text, 20)
Text1.Text = ""
End If
End Sub
Sumber: Planet Source Code
Tidak ada komentar:
Posting Komentar