7.7 Sort an Array in Access Basic

11.2 Flash a Window's Titlebar or Icon

11.2.1 Problem

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?

11.2.2 Solution

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:

  1. Add this API declaration to your code in the declarations section of the form's module:

    Private Declare Function FlashWindow Lib "User32" _  (ByVal hWnd As Long, ByVal lngInvert As Long) As Long

    In our example, the declaration is in the module for frmControlFlash.

  2. Create a module-level variable (mhWnd in our example) to hold the flashed form's window handle:

    Dim mhWnd As Long
  3. Create a procedure attached to your controlling form's Timer event, causing the form to flash:

    Private Sub Form_Timer(  )    Static fFlash As Boolean        FlashWindow mhWnd, fFlash    fFlash = Not fFlash End Sub
  4. To turn the flashing on and off, add code like this to react to some event (on the sample form, you trigger the code in reaction to the Click event of the Flash button):

    Private Sub cmdFlash_Click(  )    Dim strCaption As String    Dim ctl As Control        Set ctl = Me!cmdFlash    strCaption = ctl.Caption    If strCaption = "Flash" Then       ' If the form's already open, this will just set       ' the focus to that form.       DoCmd.OpenForm "frmFlash"       mhWnd = Forms!frmFlash.hWnd       ' Change the button's caption to indicate its state.       ctl.Caption = "Stop Flashing"       Me.TimerInterval = 250    Else       ctl.Caption = "Flash"       Me.TimerInterval = 0       FlashWindow mhWnd, False    End If End Sub

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.

Figure 11-2. frmControlFlash causes frmFlash's titlebar to invert (flash)

figs/acb_1102.gif

11.2.3 Discussion

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 make it look as if it isn't selected. Alternating between these two calls 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 250, or 1/4 of a second (the TimerInterval property measures time in milliseconds, or 1/1,000 of a second):

Me.TimerInterval = 250

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.

The code called from the main form's Timer event uses a static variable to keep track of the state of the flashing. Static variables are local to the procedure in which they're declared, but maintain their values between calls to the procedure. This way, the first time the form's Timer event occurs, the static variable fFlash contains False (because Access initializes variables to 0 (False) for you). Once the routine has done its work, it toggles the state of that variable using this statement:

fFlash = Not fFlash

If fFlash was False, it becomes True; if it was True, it becomes False. The next time Access calls that procedure (in the example, after 250 ms), the variable is still available, containing the value it held the last time it was used.

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 250; 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, Section 11.9.2 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 250 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.



Access Cookbook
Access Data Analysis Cookbook (Cookbooks)
ISBN: 0596101228
EAN: 2147483647
Year: 2005
Pages: 174

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net