RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: Visual Basic
Expertise: Beginner
Aug 8, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Activating a Previous Instance of an Application

To prevent users from launching multiple instances of your application, check the PrevInstance property of VB's App object; if the property is True, the program is already running. To activate the previous instance, call Windows' FindWindow, ShowWindow, and SetFocus APIs:

Global Const SW_RESTORE=9
Declare Function FindWindow Lib "User" _ 
        (ByVal lpClassName As Any, ByVal _
        lbWindowName As Any) As Integer
Declare Function ShowWindow Lib "User" _ 
        (ByVal hWnd As Integer, ByVal _
        nCmdShow As Integer) As Integer
Declare Function SetFocusAPI Lib _
        "User" Alias "SetFocus" (ByVal _
        hWnd As Integer) As Integer

Sub Main ()

        Dim hWnd As Integer
        Dim iResult As Integer

        ' App object's Title property may be 
        ' set at runtime, as illustrated 
        ' here, or at compile time in VB's 
        ' Make .EXE' dialog.
        App.Title = "Test Application"

        If App.PrevInstance Then

                ' ThunderForm is the class name 
                ' for VB forms (Thunder was the 
                ' original code name for Visual 
                ' Basic within 'Microsoft)

                ' Find the existing instance
                hWnd = FindWindow("Thunder_
                        Form", App.Title)

                ' FindWindow function returns a 
                ' non-zero value if it finds a 
                matching window 
                If hWnd <> 0 Then
                        ' Restore window if minimized
                        iResult = ShowWindow(hWnd,
' Set focus to the specified window
                        iResult = SetFocusAPI(hWnd)
                        ' Close this instance
                End If
        End If

        ' If no previous instance, show
        ' main form and proceed normally.

End Sub

Senthil Shanmugham
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date