Instance Count

Category: Application Features

Date: 02-16-2022

Return to Index


 
'Credit: Pierre Bellisle
 
'Compilable Code:
#Compile Exe '#Win 8.04#
#Register None
#Dim All
#Include "Win32Api.inc'#2005-01-27#
 
%Label1      = 101
%MaxInstance = 3
'______________________________________________________________________________
 
CallBack Function DlgProc
 Static WhosThere      As Dword
 Static ImHere         As Dword
 Static SetCount       As Dword
 Static InstancesCount As Dword
 
 Select Case CbMsg
 
   Case %WM_InitDialog
     WhosThere = RegisterWindowMessage("WhosThere")  'Message to provoke the "ImHere" response
     ImHere    = RegisterWindowMessage("ImHere")     'Message to count instances
     SetCount  = RegisterWindowMessage("SetCount")   'Message to show instances count
     PostMessage(%HWND_BROADCAST, WhosThere, 0, 0)   'Ask every instances to send a "ImHere" message
 
   Case WhosThere                                    'An new instance want to count total instances
     PostMessage(%HWND_BROADCAST, ImHere, 0, 1)      'Send "ImHere" message with "1" to increment InstancesCount
 
   Case ImHere                                       'Respond to an "ImHere" message
     If CbLParam = 1 Then                            'Increment InstancesCount if 1
       If GetForeGroundWindow = CbHndl Then          'Are we foreground
         Incr InstancesCount                         'Increment InstancesCount
         PostMessage(%HWND_BROADCAST, SetCount, 0, InstancesCount) 'Set InstancesCount to every instance
       End If
     Else
       Decr InstancesCount                           'Decrement InstancesCount
       PostMessage(%HWND_BROADCAST, SetCount, 0, InstancesCount) 'Set InstancesCount to every instance
     End If
 
     If InstancesCount > %MaxInstance Then           'Check if maximum count is reached
       If GetForeGroundWindow = CbHndl Then          'Supress only the foreground windows
         Control Set Text CbHndl, %Label1, "Sorry, no more than" & Str$(%MaxInstance) & _
                                           "instances," & $CrLf & "quitting now !"
         Sleep 2000
         PostMessage(%HWND_BROADCAST, SetCount, 0, InstancesCount) 'Set InstancesCount to every instance
         Dialog End CbHndl                           'End application
       End If
     End If
 
   Case SetCount
     InstancesCount = CbLParam                      'Set count total instances
     Control Set Text CbHndl, %Label1, Format$(InstancesCount) & " instance(s)'Show result
 
   Case %WM_Destroy
     PostMessage(%HWND_BROADCAST, ImHere, 0, 0) 'Say we're leaving to decrement InstancesCount
 
  End Select
 
End Function
'______________________________________________________________________________
 
Function PBMain()
 Local hDlg As Dword
 
 Dialog New %HWND_Desktop ,"Max 3 instance", , , 150, 70, _
   %WS_Caption Or %WS_MinimizeBox Or %WS_SysMenu, %WS_Ex_Topmost To hDlg
 SetClassLong hDlg, %GCL_HICON, LoadIcon(ByVal %NULL, ByVal %IDI_INFORMATION) 'Set a nice dialog icon
 
 Control Add Label, hDlg, %Label1, "Instances count", 10, 30, 130, 20, %SS_Center, 0
 
 Dialog Show Modal hDlg Call DlgProc
 
End Function   
 
'gbs_01203
'Date: 05-11-2013


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm