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 propertyis True, the program is already running. To activate the previousinstance, call Windows’ FindWindow, ShowWindow, and SetFocus APIs:

 Global Const SW_RESTORE=9Declare Function FindWindow Lib "User" _         (ByVal lpClassName As Any, ByVal _        lbWindowName As Any) As IntegerDeclare Function ShowWindow Lib "User" _         (ByVal hWnd As Integer, ByVal _        nCmdShow As Integer) As IntegerDeclare Function SetFocusAPI Lib _        "User" Alias "SetFocus" (ByVal _        hWnd As Integer) As IntegerSub 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,                                SW_RESTORE)' Set focus to the specified window                        iResult = SetFocusAPI(hWnd)                        ' Close this instance                        End                End If        End If        ' If no previous instance, show        ' main form and proceed normally.        frmMain.ShowEnd Sub
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: