![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Feb 2005
Posts: 41
Rep Power: 0
![]() |
Application opens twice !
Hello all,
I have done an application TESTE.EXE using VB 6.0, but every time I click on the file the application opens, like it should, but even if it is already open... What do I do for the application just pops back up when it is minimised and I click on the TESTE.EXE again??? Thx in advance. |
|
|
|
|
|
#2 |
|
Expert Programmer
|
I assume what you mean is restricting use to one instance of the app, and giving focus to the existent instance if another copy is run.
The VB way of doing this would be: Sub Main()
If App.PrevInstance Then
VBA.AppActivate App.Title, 0
Else
Form1.Show
End If
End SubUnfortunately that doesn't work (it doesn't bring focus back to the application. Try this in your sub main: it uses an API method to find the handle of the active window of the previous instance and give focus to it. Obviously your app must have a window! Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function GetWindow Lib "user32" (ByVal Hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function OpenIcon Lib "user32" (ByVal Hwnd As Long) As Long
Public Declare Function SetForegroundWindow Lib "user32" (ByVal Hwnd As Long) As Long
Sub Main()
If App.PrevInstance Then
FocusPrevInstance
Else
Form1.Show
End If
End Sub
Private Sub FocusPrevInstance()
Dim ThisObject As String
Dim ObjHwnd As Long
ThisObject = App.Title
App.Title = "_" & App.Title 'rename this instance so we don't find ourselves!
' Find runnning instance's application object, trying various class names
ObjHwnd = FindWindow("ThunderRTMain", ThisObject)
If ObjHwnd = 0 Then ObjHwnd = FindWindow("ThunderRT5Main", ThisObject)
If ObjHwnd = 0 Then ObjHwnd = FindWindow("ThunderRT6Main", ThisObject)
ObjHwnd = GetWindow(ObjHwnd, &H3) ' Get active window
hFocusWnd ObjHwnd
End Sub
Private Sub hFocusWnd(ByVal Hwnd As Long) 'Focuses specified window
OpenIcon Hwnd
SetForegroundWindow Hwnd
End Sub |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Apr 2005
Posts: 19
Rep Power: 0
![]() |
If App.PrevInstance Then
Unload Me
End
End If |
|
|
|
|
|
#4 | |
|
Programmer
Join Date: Feb 2005
Posts: 41
Rep Power: 0
![]() |
nope
Quote:
didnt work too.... :-( |
|
|
|
|
|
|
#5 |
|
Expert Programmer
|
I'm very suprised: re my second code block are you sure you've copied it in its entirity into a Module (.bas) and have set the startup mode of the project to sub main?
|
|
|
|
|
|
#6 | |
|
Programmer
Join Date: Feb 2005
Posts: 41
Rep Power: 0
![]() |
Quote:
Thank you. ![]() |
|
|
|
|
|
|
#7 |
|
Expert Programmer
|
No probs!
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|