Visual Basic Programming Code Examples Visual Basic > Other Code Examples Flashing a window to get a users attention Flashing a window to get a users attention The following code demonstrates the different API calls required to flash windows on different operating systems: Const FLASHW_STOP = 0 'Stop flashing. The system restores the window to its original state. Const FLASHW_CAPTION = &H1 'Flash the window caption. Const FLASHW_TRAY = &H2 'Flash the taskbar button. Const FLASHW_ALL = (FLASHW_CAPTION Or FLASHW_TRAY) 'Flash both the window caption and taskbar button. This is equivalent to setting the FLASHW_CAPTION Or FLASHW_TRAY flags. Const FLASHW_TIMER = &H4 'Flash continuously, until the FLASHW_STOP flag is set. Const FLASHW_TIMERNOFG = &HC 'Flash continuously until the window comes to the foreground. Private Type FLASHWINFO cbSize As Long hwnd As Long dwFlags As Long uCount As Long dwTimeout As Long End Type Private Declare Function FlashWindowEx Lib "user32" (pfwi As FLASHWINFO) As Boolean Private Declare Function FlashWindow Lib "user32" (ByVal hwnd As Long, ByVal bInvert As Long) As Long Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Private Declare Function GetActiveWindow Lib "user32" () As Long 'Purpose : Causes the specified window to "flash". 'Inputs : lhwndWindow The handle to the window to flash. ' [lFlags] One or a combinate of the "FLASHW_" flags listed in the declaration section above ' [lFlashRate] If specified is the rate at which the screen flashes in ms, ' else uses the default. ' [lNumFlashes] The number of times to flash the window. The default, zero, causes it to flash indefinitely. 'Outputs : Returns True if the window was active before the call else returns False. 'OS : Windows 2000; Windows 98 'Notes : 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.) Function WindowFlashA(lhwndWindow As Long, Optional lFlags As Long = FLASHW_ALL Or FLASHW_TIMER, Optional lFlashRate As Long = 0, Optional lNumFlashes As Long = 0) As Boolean Dim tFlash As FLASHWINFO On Error GoTo ErrFailed tFlash.cbSize = Len(tFlash) 'Specifies the flash status tFlash.dwFlags = lFlags 'Specifies the rate, in milliseconds, at which the window will be flashed. 'If dwTimeout is zero, the function uses the default cursor blink rate. tFlash.dwTimeout = lFlashRate 'Handle to the window to be flashed. The window can be either opened or minimized. tFlash.hwnd = lhwndWindow 'Specifies the number of times to flash the window. Zero will cause it to flash indefinately. tFlash.uCount = lNumFlashes 'Call the API to flash the window WindowFlashA = FlashWindowEx(tFlash) Exit Function ErrFailed: Debug.Print "Error in WindowFlashA: " & Err.Description WindowFlashA = False End Function 'Purpose : Causes the specified window to "flash" a specified number of times. 'Inputs : lhwndWindow The handle to the window to flash. ' [lFlashRate] The rate at which the flashing occurs (ms). ' [lNumFlashes] The number of times to flash the window. 'Outputs : Returns True if suceeded in flashing the window. 'OS : Windows NT 3.1 or later; Windows 95 or later 'Notes : 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.) Function WindowFlashB(lhwndWindow As Long, Optional lFlashRate As Long = 500, Optional lNumFlashes As Long = 4) As Boolean Dim lThisFlash As Long Const clInvert As Long = 1 For lThisFlash = 1 To lNumFlashes Sleep lFlashRate FlashWindow lhwndWindow, clInvert Next 'Restore the window FlashWindow lhwndWindow, 0 WindowFlashB = True Exit Function ErrFailed: Debug.Print "Error in WindowFlashB: " & Err.Description WindowFlashB = False End Function 'Demonstration routine Sub Test() 'Works on 98/2000 WindowFlashA GetActiveWindow 'Works on NT WindowFlashB GetActiveWindow End Sub