Visual Basic Window/Form Routines
FlashWindow: Timing Window Notifications using SetTimer
     
Posted:   Wednesday October 30, 2002
Updated:   Monday December 26, 2011
     
Applies to:   VB4-32, VB5, VB6
Developed with:   VB5, Windows XP
OS restrictions:   None
Author:   VBnet - Randy Birch
     

Related:  

Pure VB: Cue Banners for the XP Impaired
Manifests: Make the VB IDE use Windows XP Styles
SendMessage: Use Cue Banners to Prompt Users

Shell_NotifyIcon: Windows Systray NOTIFYICONDATA Overview

Shell_NotifyIcon: Add Icon to Windows System Tray
Shell_NotifyIcon: Respond to Systray Icon/Menu Interaction
Shell_NotifyIcon: Respond to Systray Icon/Menu Interaction in a MDI App

Shell_NotifyIcon: Animate the System Tray Icon

FlashWindowEx: Extended Window Notification (Flashing) Options
FlashWindow: Timing Window Notifications using SetTimer
FlashWindow: Timing Window Notifications using VB's Timer Control

Shell_NotifyIcon: Display Systray Balloon Tips
Shell_NotifyIcon: Respond to Systray Balloon Tip Clicks
Shell_NotifyIcon: Use SetTimer to Define Balloon Tip Life

SendMessage: Add Balloon Tips to a Combo Edit Box
SendMessage: Add Balloon Tips to a Text Box

     
 Prerequisites
None.

For those developers who may prefer to use the API over VB's intrinsic timer control, this page shows how to cause an application to flash its window (on screen or in the taskbar) using FlashWindow.  In addition, it details how to create a timer using the SetTimer API, and how to respond to the TimerProc callback message raised each time the API's timer's elapsed interval expires.

Flashing a window means changing the appearance of its caption bar as if the window were changing from inactive to active status, or vice versa. (An inactive caption bar changes to an active caption bar; an active caption bar changes to an inactive caption bar.) Typically, a window is flashed to inform the user that the window requires attention but that it does not currently have the keyboard focus. The FlashWindow function flashes the window only once; for repeated flashing, the application requires calling via a VB or system timer.

When FlashWindow's bInvert parameter is TRUE, the window is flashed from one state to the other. If it is FALSE, the window is returned to its original state (either active or inactive). When an application is minimized and this parameter is TRUE, the taskbar window button flashes active/inactive. If it is FALSE, the taskbar window button flashes inactive, meaning that it does not change colors. It flashes, as if it were being redrawn, but it does not provide the visual invert clue to the user.

With SetTimer, the nIDEvent member specifies a non-zero timer identifier. If the hwnd parameter of SetTimer is null, nIDEvent is ignored. If hwnd is non-null and the window specified by hwnd already has a timer with the value nIDEvent, then the existing timer is replaced by the new timer. When SetTimer replaces a timer, the timer is reset. Therefore, a message will be sent after the current time-out value elapses, but the previously set time-out value is ignored.  The hwnd parameter specifies the handle to the window to be associated with the timer --- this window must be owned by the calling thread. If this parameter is null (0&)  no window is associated with the timer and the nIDEvent parameter is ignored.

 BAS Module Code
Because this requires a callback, a public function in a bas module is needed. Add the following code to a BAS module:

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private tmrRunning As Boolean
Private Const nEventID As Long = 999
Private Const WM_TIMER = &H113

Private Declare Function FlashWindow Lib "user32" _
  (ByVal hwnd As Long, _
   ByVal bInvert As Long) As Long

Private Declare Function SetTimer Lib "user32" _
  (ByVal hwnd As Long, _
   ByVal nIDEvent As Long, _
   ByVal uElapse As Long, _
   ByVal lpTimerFunc As Long) As Long
   
Private Declare Function KillTimer Lib "user32" _
  (ByVal hwnd As Long, _
   ByVal nIDEvent As Long) As Long



Public Sub FlashBegin(ByVal hwnd As Long, ByVal Frequency As Long)

   If Not tmrRunning Then
      
      If Frequency > 0 Then
         Call SetTimer(hwnd, nEventID, Frequency, AddressOf TimerProc)
         tmrRunning = True
      End If
      
   End If

End Sub


Public Sub FlashEnd(ByVal hwnd As Long)

   If tmrRunning = True Then
      Call FlashWindow(hwnd, False)
      Call KillTimer(hwnd, nEventID)
      tmrRunning = False
   End If
   
End Sub


Public Function TimerProc(ByVal hwnd As Long, _
                          ByVal uMsg As Long, _
                          ByVal idEvent As Long, _
                          ByVal dwTime As Long) As Long

   Select Case uMsg
      Case WM_TIMER
            
         If idEvent = nEventID Then
            Call FlashWindow(hwnd, True)
         End If
         
      Case Else
   End Select
   
End Function

 Form Code
Add the following to a form containing a combo (Combo1) and two command buttons (Command1, Command2):

Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Distribution: You can freely use this code in your own
'               applications, but you may not reproduce 
'               or publish this code on any web site,
'               online service, or distribute as source 
'               on any media without express permission.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub Command1_Click()

   Call FlashBegin(Me.hwnd, Val(Combo1.List(Combo1.ListIndex)))
   Combo1.Enabled = False
   Command1.Enabled = False
   
End Sub


Private Sub Command2_Click()
 
   Call FlashEnd(Me.hwnd)
   Command1.Enabled = True
   Combo1.Enabled = True
   
End Sub


Private Sub Form_Load()

   With Combo1
      .AddItem "50"
      .AddItem "100"
      .AddItem "250"
      .AddItem "500"
      .AddItem "1000"
      .ListIndex = 2
   End With
   
   Command1.Caption = "Begin Flash"
   Command2.Caption = "Stop Flash"
   
End Sub
 Comments

 
 

PayPal Link
Make payments with PayPal - it's fast, free and secure!

 
 
 
 

Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved.
Terms of Use  |  Your Privacy

 

Hit Counter