Understanding How the Graphics2D Program Works
Now that you've had a chance to experiment with the Graphics2D application, you may want a quick rundown of how it does its thing. First, look at the Graphics2DView.h file. The CGraphics2DView class declares a number of data members, including m_polygon, which is a SHAPE structure that holds information about the current shape. The data member m_vectors is an array of VECTOR structures that hold the vertices of the m_polygon object. Finally, the data members m_rotate, m_xScale, m_yScale, m_xTranslate, and m_yTranslate hold the values for the currently selected transformations.
Look now at the Graphics2DView.cpp file. When the program runs, the CGraphics2DView class's constructor first initializes the m_polygon structure and the shape's vertices, which are stored in the m_vectors[] array. The constructor also initializes the transformation variables m_rotate, m_xScale, m_yScale, m_xTranslate, and m_yTranslate to their default values. These default values won't affect the shape's vertices if you happen to right-click in the window before setting the transformation variables with the commands in the Transform menu.
When the application's window appears onscreen, the CGraphics2DView class's OnDraw() function displays the shape by calling the DrawShape() function. This DrawShape() function is similar to the previous version, except that it now requires two arguments: a pointer to a device context and a reference to a SHAPE structure.
When you select a command from the Transform menu, the appropriate command-response function takes over, initializing and displaying the Transform dialog box, and then saving your response in the appropriate transformation variables. When you select the Scale command, for example, the OnTransformScale() function gets called. In that function, the program first sets the dialog box's m_xAxis and m_yAxis data members to 1, so that the default scaling values of 1 for both axes are in the edit controls when the dialog box appears. The call to the dialog box's DoModal() function then displays the dialog box so that you can enter new values into the edit controls. When you click OK, the program stores your responses in the m_xScale and m_yScale variables.
Finally, when you right-click in the application's window, the program calls the OnRButtonDown() function, which applies the selected transformations to the shape's vertices. First, the function declares a matrix called m to hold the transformations and calls InitMatrix() to initialize m to an identity matrix. Calling the Translate(), Scale(), and Rotate() functions (with the appropriate transformation variables as arguments) then composes the transformations in the matrix, after which a call to Transform() applies the transformations to the shape's vertices. The function then resets the transformation variables to their default values and calls Invalidate() to force the application's window to redraw.
You can add a couple of enhancements to the Graphics2D program to make it easier to use. First, try adding a response function for the WM_LBUTTONDOWN message that resets the shape to its starting point in the window. To do this, you'd reinitialize the shape's vertices to their starting values (as is done in the CGraphics2DView class's constructor) and then call Invalidate(TRUE) to force the window to repaint.
You may also want to add a dialog box that displays the values of the shape's current vertices. That way, if you should transform the shape such that it no longer appears in the window, you can see the type of translation you need to bring the shape back into view. To do this, you need to create a new dialog box with App Studio, and then create a new menu command that displays the dialog box. The response functions for the current Transform menu commands show you how to respond to a menu command and how to initialize and display a dialog box.