|[ Team LiB ]|
Recipe 11.2 Flash a Window's Titlebar or Icon
With so many windows open in your Access applications, it can be difficult to force your user's attention to a specific form. Is there a way to make the titlebar flash so that a form really stands out?
Windows supplies a simple API call, FlashWindow, that allows you to flash the titlebar of a form or its icon (if it's iconized) on and off. This solution will demonstrate how you can use the FlashWindow API call to draw attention to a specific form.
To include this functionality in your own applications, follow these steps:
To see an example of a flashing form, load and run frmControlFlash from 11-02.MDB. That form loads a second form, frmFlash. By clicking the button on frmControlFlash, you can turn the flashing of frmFlash's titlebar on or off (see Figure 11-2). If you iconize frmFlash, it will continue to flash.
The FlashWindow API call takes two values as its parameters: the handle to a window and a logical value. When Windows creates a new window (as it does when you open a form in Access), it supplies the window with a unique 32-bit value, its handle, that any program can use to work directly with that window. Access gives you a form's handle in its hWnd property. Given that handle and a Boolean value (True or False) indicating whether you want the window to invert or not, FlashWindow takes the requested action with the window you've indicated. For example:
FlashWindow Forms("frmFlash").hWnd, True
would make the titlebar of frmFlash look like it is selected, even if it isn't the currently active form. Sending False for the second parameter would revert to the form's original state (selected or deselected). Calling FlashWindow, passing True in the second parameter, is what makes the window look like it's flashing; this is where the Timer event comes in.
By reacting to a form's Timer event, you can have your code take effect at a set interval. In this case, you set the timer interval to be 500, or 1/2 of a second (the TimerInterval property measures time in milliseconds, or 1/1,000 of a second):
Me.TimerInterval = 500
To make it so that the code attached to the Timer event never runs, set the TimerInterval property to 0. That's how you control the flashing in this example: to turn flashing on, set the TimerInterval property to the rate at which you'd like the flashing to occur; to turn it off, just set the TimerInterval property to 0.
This example takes one extra step: when it turns off the flashing, it also makes sure that the caption bar of the flashed form is no longer inverted. That is, it calls FlashWindow one more time, forcing the flashing off:
Me.TimerInterval = 0 FlashWindow mhWnd, False
This ensures that no matter where in the cycle you turn off the flashing, the flashed form reverts to its normal appearance.
You can control the speed of the flashing by changing the TimerInterval property value. Currently, it's set at 500; you may want to speed that up. Be aware, though, that flashing is not a normal Windows mechanism; it goes against the Windows design standards, and should be used only for brief periods of time and in special circumstances.
Because FlashWindow accepts the handle to any window as its parameter, you could use this same technique to cause an application's main window to flash as well. For example, The Solution in Recipe 11.9 shows how to retrieve a list of all open top-level windows, and you could use the hWnd properties from that list with FlashWindow as well.
Note that even though the form's Timer event is set to do its work every 500 ms, it may take longer for your flashing form to start flashing. The code in the form's Timer event sends a message to Windows, telling it to flash the other form's titlebar, but that may take a few milliseconds on a slower machine. For the same reason, your form may not flash at exactly regular intervals. The form's timer handler is non-preemptive, meaning that it must wait for keyboard, mouse, and screen events to be handled first.
|[ Team LiB ]|