Example: Virtual Buffer ScrollingThe Scroller program demonstrates a circular scroll within a virtual buffer. It uses two 320x200 virtual buffers: one that is copied to the window, and another that provides the workspace needed when fg_scroll() performs a circular scroll. Scroller first fills the virtual buffer with a light blue rectangle (color 19), displays a smaller white (color 25) rectangle in the center of the screen, and then uses fg_move(), fg_draw(), and fg_paint() to display a light green (color 20) star within the white rectangle, as shown here:
The program scrolls the star upward in four-pixel increments. Because the scroll is circular, rows of the star that "scroll off" the top edge of the white rectangle (whose height is the same as the scrolling region) reappear at its bottom edge. This continues until we exit the program. In a DOS program, we might perform the scrolling by calling fg_scroll() in a while loop that executes until, say, the user presses Escape. In Windows, we can't have a single application tie up the system like that, for it would prevent the user from switching back and forth between our scrolling program and other applications, nor would it be possible to do things such as change the window size. The key to achieving the continuous scrolling lies in the program's message loop. As usual, the message loop executes continuously until the program exits, but unlike the "traditional" message loop, we don't just continue looping when no messages are available (which will be the vast majority of the time). Instead, we call Scroll() to perform one iteration of the scroll, and doing this over and over achieves the scrolling effect. We'll refer to this style of message loop as an animation-based message loop.
|
 
copyright 2001 Ted Gruber Software, Inc.