|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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 |
|
|
|
|||||
|
|||||
|
|||||
Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved. |