Joined: 02 Dec 2003
|Posted: Wed Aug 11, 2004 8:37 pm Post subject: Programming Tip #8: The Windows Refresh Event
|Programming Tip #8: The Windows Refresh Event
In this article we take a look at the RefreshEvent. The RefreshEvent is a Windows applications event that is raised when one of the following events occur:
- The user minimized the window of your application and then restored it.
- The user moved another window on top of your application window.
- The user resized your application window.
Essentially, any scenario in which the contents of your application window need to be redrawn will trigger a Windows RefreshEvent.
Let's take a look at an example program to see how this works. Below we have a simple Synopsis program that displays a graphic rectangle when the user clicks anywhere inside the application window.
Here is the link to the corresponding VPD source file:
The program above uses the "Graphic Interface" component (from the Graphics tab of the component tray) to draw a simple gradient style rectangle at the coordinates (50, 50) of the application window. The "Graphic Interface" component uses the "Graphic Brush" component which gives us the red and black striped gradient style.
Now the program runs fine but try this. Drag the window off the sides of the screen or minimize and then restore the window. Or try dragging another window over our application window.
What happened to our graphic rectangle? It seems to get either partially erased or disappears altogether.
Why does this happen? The reason is that graphics inside a window is kind of like a customized user interface. The window cannot "remember" what graphics you drew in the window. Having the capability for storing an unknown and potentially huge amount of graphics is beyond the duties of the window.
So how can we get our rectangle back whenever something erases the contents of the application window? Well, we have to redraw it whenever a window refresh is needed.
This is the purpose of the RefreshEvent in the Windows Event component. Whenever something happens that causes the contents of a window to be erased, the Windows system tells us. It is then up to us to decide what needs to be done. In our program above, what we need to do is refresh the rectangle.
To do this, double click on the Windows Event component to bring up its component property dialog. Scroll down the list of properties and look for the "RefreshEvent" property. Then set the event so that it has the status "Enabled":
Here is the VPD source file for our slightly modified program:
Our program now responds to the RefreshEvent and will draw the rectangle whenever a windows refresh event occurs. If we re-run the program we see that the rectangle gets redrawn whenever we do something that would cause the contents to get erased. You might noticed that in addition, now the rectangle gets drawn without even having to click in the window. This happens because when the window is created, a refresh event is generated automatically to ensure that the contents are refreshed correctly.
Now this is only a simple example using the RefreshEvent. Typically in a more complex program you will test for the various events that can occur and then take appropriate actions for each event.