Avis Junior Poster

Joined: 07 Oct 2003 Posts: 510 Location: India
|
Posted: Aug 14th, 2004 05:49 AM Post subject: Allow Only 1 Instance Of Your Program (And Activate Previous |
|
|
Module Code:
[vb:1:53e81edb03]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[/vb:1:53e81edb03]
Form Code:
[vb:1:53e81edb03]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[/vb:1:53e81edb03] _________________ Code Snippets, Tutorials, Utilities, Controls
Low cost Web Hosting
Hosting starts at as low as $4 per year!
Always follow posting guidelines
Put your VB code in [vb ] your code [ /vb] tags! |
|