Example: Virtual Buffer PanningThe Panner program illustrates this process by panning a 640x480 BMP image through a smaller window. The window size is initially half the image size in each direction (that is, 320x240) and initially contains the upper left quadrant of the BMP image. The program has a top-level menu with selections Left, Right, Up, and Down that move the viewing area one pixel in that direction, plus a Reset selection that restores the image to its original position. Most of the work in Panner takes place in the WM_SIZE and menu handlers. First, however, the WM_CREATE handler sets the global variables x and y to zero. These variables define the virtual buffer coordinates for the upper left corner of that part of the image currently displayed in the client area. The WM_SIZE handler updates two other important globals, xLimit and yLimit, that define how much of the image can fit in the client area. These globals help ensure that we stay within the image limits when panning. The Left menu handler starts by checking if the starting x coordinate is equal to the value of xLimit. If so, it means we will once again be able to pan to the right after panning left, so we enable the Right menu item. We then decrement the starting x coordinate and call fg_vbpaste() to copy pixels from the virtual buffer to the client area, starting at the new x coordinate. This produces the panning effect. Finally, we check if the x coordinate has now reached zero. If so, we can pan no farther left, so we disable the Left menu item. The Right, Up, and Down menu handlers are all very similar to the Left handler. That is, they adjust the x or y coordinate, call fg_vbpaste() to achieve the panning, and enable or disable menu selections when needed. The Reset menu handler simply sets the x and y coordinates back to zero, calls fg_vbpaste(), enables the Right and Down menu items if we can pan in those directions, and disables Left and Up (because we're once again at the origin). In all cases, the program uses fg_vbpaste() to copy the region whose upper left corner is (x,y) into the client area. The amount copied depends on the client area size. If the client area is smaller than the image in a given direction, we display only as many pixels from the image as can fit in the window. If the image is smaller than the window, we display the full width or height of the image; Windows fills the remainder of the client area with background color pixels.
|
 
copyright 2001 Ted Gruber Software, Inc.