VB icon

An App.Previnstance example to call the running app to the front.

Email
Submitted on: 1/15/2015 4:11:00 PM
By: Brian Heath (from psc cd)  
Level: Intermediate
User Rating: By 18 Users
Compatibility: VB 5.0, VB 6.0
Views: 2381
 
     This code was useful to me when trying to find a way to call the exsisting application in memory to the front when another instance of the same application would be attempted by a user.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: An App.Previnstance example to call the running app to the front.
' Description:This code was useful to me when trying to find a way to call the exsisting application in memory to the front when another instance of the same application would be attempted by a user.
' By: Brian Heath (from psc cd)
'
' Assumes:That you know about window handles.
'**************************************

'This was found at the Microsoft Knowledgebase, Article ID: Q185730 
'Paste the following code into the code Module for Form1:
Option Explicit
Private Sub Form_Load()
If App.PrevInstance Then
 ActivatePrevInstance
End If
End Sub
'2) Add a Standard Module to the Project.
'3) Paste the following code into the module:
Option Explicit
Public Const GW_HWNDPREV = 3
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
Sub ActivatePrevInstance()
Dim OldTitle As String
Dim PrevHndl As Long
Dim result As Long
'Save the title of the application.
OldTitle = App.Title
'Rename the title of this application so FindWindow
'will not find this application instance.
App.Title = "unwanted instance"
'Attempt to get window handle using VB4 class name.
PrevHndl = FindWindow("ThunderRTMain", OldTitle)
'Check for no success.
If PrevHndl = 0 Then
 'Attempt to get window handle using VB5 class name.
 PrevHndl = FindWindow("ThunderRT5Main", OldTitle)
End If
'Check if found
If PrevHndl = 0 Then
'Attempt to get window handle using VB6 class name
PrevHndl = FindWindow("ThunderRT6Main", OldTitle)
End If
'Check if found
If PrevHndl = 0 Then
 'No previous instance found.
 Exit Sub
End If
'Get handle to previous window.
PrevHndl = GetWindow(PrevHndl, GW_HWNDPREV)
'Restore the program.
result = OpenIcon(PrevHndl)
'Activate the application.
result = SetForegroundWindow(PrevHndl)
'End the application.
End
End Sub
BHeath
Deffacto Web Designs Team
http://www.deffacto.com


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.