Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 8th, 2005, 12:57 PM   #1
hipolito
Programmer
 
Join Date: Feb 2005
Posts: 41
Rep Power: 0 hipolito is on a distinguished road
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.
hipolito is offline   Reply With Quote
Old Apr 8th, 2005, 2:02 PM   #2
Rory
Expert Programmer
 
Rory's Avatar
 
Join Date: Jan 2005
Location: London
Posts: 542
Rep Power: 4 Rory is on a distinguished road
Send a message via MSN to Rory
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 Sub

Unfortunately 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
Rory is offline   Reply With Quote
Old Apr 14th, 2005, 6:09 PM   #3
drruggy
Newbie
 
Join Date: Apr 2005
Posts: 19
Rep Power: 0 drruggy is on a distinguished road
If App.PrevInstance Then
        Unload Me
        End
    End If
drruggy is offline   Reply With Quote
Old Apr 15th, 2005, 7:02 AM   #4
hipolito
Programmer
 
Join Date: Feb 2005
Posts: 41
Rep Power: 0 hipolito is on a distinguished road
nope

Quote:
Originally Posted by drruggy
If App.PrevInstance Then
        Unload Me
        End
    End If

didnt work too....

:-(
hipolito is offline   Reply With Quote
Old Apr 15th, 2005, 1:17 PM   #5
Rory
Expert Programmer
 
Rory's Avatar
 
Join Date: Jan 2005
Location: London
Posts: 542
Rep Power: 4 Rory is on a distinguished road
Send a message via MSN to Rory
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?
Rory is offline   Reply With Quote
Old Apr 20th, 2005, 3:54 PM   #6
hipolito
Programmer
 
Join Date: Feb 2005
Posts: 41
Rep Power: 0 hipolito is on a distinguished road
Smile Sorry

Quote:
Originally Posted by Rory
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?
Hey rory, thx man, yes it did work just fine, great work man..

Thank you.
hipolito is offline   Reply With Quote
Old Apr 21st, 2005, 11:55 AM   #7
Rory
Expert Programmer
 
Rory's Avatar
 
Join Date: Jan 2005
Location: London
Posts: 542
Rep Power: 4 Rory is on a distinguished road
Send a message via MSN to Rory
No probs!
Rory is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:06 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC