In this application we are going to create two forms this way when the second form is active we can make the first form flash. To add the second form select Project -> Add Form. After Form2 is added go back to Form1.
The FlashWindow API flashes the tab by the start menu corresponding to the window you specify. The API only will flash the window once though so we will use a Timer control to have the tab flash until the user clicks on it. In a real world application you probably never want to make the window flash forever like this as it would be very annoying. Most of the time applications will flash the window 3 times or so - that way you can get the user's attention. At any rate for our sample application Add a Timer control to Form1. After adding the timer your screen should look like this (notice the timer control is on Form1 and we have a Form2 in our project.
Declaring Windows API's in Visual Basic
In order to make a Win32 API call in VB we need to declare it first. One easy way to do this is at the top of the Form1 code.
Option Explicit
Private Declare Function FlashWindow Lib "user32" ( _
*ByVal hwnd As Long, ByVal bInvert As Long) As Long
The FlashWindow function takes two parameters
- hWnd - Identifies the window to be flashed. The window can be either open or iconic.
- bInvert - Specifies whether the window is to be flashed or returned to its original state. The window is flashed from one state to the other if the bInvert parameter is nonzero. If the bInvert parameter is zero, the window is returned to its original state (either active or inactive).
FlashWindow returns a value that specifies the window's state before the call to the FlashWindow function. It is nonzero if the window was active before the call; otherwise, it is zero.
So now lets get to the coding. First the basics - lets do some initializing in our form load event.
Private Sub Form_Load()
Timer1.Enabled = False
Timer1.Interval = 1000
Form2.Show
End Sub
This code is pretty straight forward. Line 2 disables the timer until we need it. Line 3 sets our timer interval to 1000 miliseconds which is 1 second. Lastly in Line 4 we display Form2. Form2 is where we will control Form1 from.
Next we want to set up the timer so that at every interval it calls our FlashWindow API. We do that in the Timer event.
Private Sub Timer1_Timer()
FlashWindow Form1.hwnd, 1
End Sub
If you remember the FlashWindow API takes to parameters. The first is the handle to the window you want to flash. Visual Basic stores the handle for the window of our form in the hwnd parameter so we simply pass that to the FlashWindow function. The second parameter the API takes is either 0 or non-zero, by passing a non-zero value we tell the API that we want it to flash the window. Since we put this command in the Timer1_Timer() event the window will flash every interval which we set to 1 second in the Form_Load() event. So any time our Timer is enabled the form will flash.
Now open up Form 2 in design mode. Add a two command buttons to it. Set the caption for Command1 to "Start Flash" and the caption for Command2 to "Stop Flash".
Now lets add our code to Form2.
Option Explicit
Private Sub Command1_Click()
Form1.Timer1.Enabled = True
End Sub
Private Sub Command2_Click()
Form1.Timer1.Enabled = False
End Sub
This code is very simple when we enable the timer on form1 it starts making the form flash. When we disable the timer it stops the flashing. Now whenever you need to get the users attention you can use this API to flash your window
Source http://www.vb6.us/tutorials
Reviews:
Post a Comment