Register for EarthWeb's Million Dollar Sweepstakes!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Fast Track Visual C++ 6.0 Programming
(Publisher: John Wiley & Sons, Inc.)
Author(s): Steve Holzner
ISBN: 0471312908
Publication Date: 09/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


That’s the way our drawing process works. Let’s start implementing this process in OnMouseMove() now.

Stretching Graphics Figures

We draw the outlining rectangle in OnMouseMove(), and as the mouse moves, the program gives the impression that the user is stretching a rectangle over the capture site. We start OnMouseMove() by making sure that we’re capturing the screen and that we’re supposed to draw the outlining rectangle.

void CCopierView::OnMouseMove(UINT nFlags, CPoint point)

{
    if(CaptureFlag && DrawFlag){                 ⇐
        .
        .
        .
    }                                            ⇐

    CView::OnMouseMove(nFlags, point);

}

If the drawing process has begun, we get the current location of the mouse in screen coordinates.

void CCopierView::OnMouseMove(UINT nFlags, CPoint point)

{
    if(CaptureFlag && DrawFlag){
        CDC ScreenDC;

        CPoint CurrentPoint = GetMessagePos();          ⇐
            .
            .
            .
    }

    CView::OnMouseMove(nFlags, point);

}

The next step is to draw the outlining rectangle from the anchor point to the current point. To do that, we need a device context corresponding to the whole screen, as shown in the following example (the three NULLs in the call to CreateDC() correspond to the name of the device, its type, and initialization data, none of which we need here):

void CCopierView::OnMouseMove(UINT nFlags, CPoint point)

{
    if(CaptureFlag && DrawFlag){
        CDC ScreenDC;

        CPoint CurrentPoint = GetMessagePos();

        ScreenDC.CreateDC("DISPLAY", NULL, NULL, NULL);        ⇐
            .
            .
            .
    }

    CView::OnMouseMove(nFlags, point);

}

Now we can draw the rectangle itself from the anchor point to the current point. To make sure the rectangle is not filled in, we select the null brush in the device context and set the ROP2 mode to R2_NOT.

void CCopierView::OnMouseMove(UINT nFlags, CPoint point)

{
    if(CaptureFlag && DrawFlag){
        CDC ScreenDC;

        CPoint CurrentPoint = GetMessagePos();

        ScreenDC.CreateDC("DISPLAY", NULL, NULL, NULL);
        ScreenDC.SelectStockObject(NULL_BRUSH);             ⇐
        ScreenDC.SetROP2(R2_NOT);                              ⇐
                .
                .
                .
    }

    CView::OnMouseMove(nFlags, point);

}


Device Context Brushes

When you draw figures in device contexts, you use a pen to draw and a brush to fill in figures. You can specify hatch styles and colors when creating a brush type; some of the standard hatch styles include: HS_BDIAGONAL, HS_CROSS, HS_DIAGCROSS, HS_FDIAGONAL, HS_HORIZONTAL, and HS_VERTICAL.


Binary Raster Operations

What’s the ROP2 mode? ROP2 is shorthand for binary raster operation, which is the way bit-by-bit drawing is handled on the screen. In this case, we’re setting the ROP2 mode to R2_NOT, which means that everything we draw is the inverse of what’s on the screen. This makes life simpler for us, because if we redraw the same rectangle in this mode, that rectangle disappears.

The ROP2 modes appear in Table 6.1.

Table 6.1 ROP2 Modes

ROP2 Mode Means

R2_BLACK Pixel is always black.
R2_COPYPEN Pixel is the pen color.
R2_MASKNOTPEN Pixel is a combination of the colors common to both the screen and the inverse of the pen (final pixel = (NOT pen) AND screen pixel).
R2_MASKPEN Pixel is a combination of the colors common to both the pen and the screen (final pixel = pen AND screen pixel).
R2_MASKPENNOT Pixel is a combination of the colors common to both the pen and the inverse of the screen (final pixel = (NOT screen pixel) AND pen).
R2_MERGENOTPEN Pixel is a combination of the screen color and the inverse of the pen color (final pixel = (NOT pen) OR screen pixel).
R2_MERGEPEN Pixel is a combination of the pen color and the screen color (final pixel = pen OR screen pixel).
R2_MERGEPENNOT Pixel is a combination of the pen color and the inverse of the screen color (final pixel = (NOT screen pixel) OR pen).
R2_NOP Pixel remains unchanged.
R2_NOT Pixel is the inverse of the screen color.
R2_NOTCOPYPEN Pixel is the inverse of the pen color.
R2_NOTMASKPEN Pixel is the inverse of the R2_MASKPEN color (final pixel = NOT(pen AND screen pixel)).
R2_NOTMERGEPEN Pixel is the inverse of the R2_MERGEPEN color (final pixel = NOT(pen OR screen pixel)).
R2_NOTXORPEN Pixel is the inverse of the R2_XORPEN color (final pixel = NOT(pen XOR screen pixel)).
R2_WHITE Pixel is always white.
R2_XORPEN Pixel is a combination of the colors that are in the pen or in the screen, but not in both (final pixel = pen XOR screen pixel).

Finally, we draw the rectangle from the anchor point to the previous point to erase the last rectangle and then from the anchor point to the current point to draw the new rectangle.

void CCopierView::OnMouseMove(UINT nFlags, CPoint point)

{
    if(CaptureFlag && DrawFlag){
        CDC ScreenDC;

        CPoint CurrentPoint = GetMessagePos();

        ScreenDC.CreateDC("DISPLAY", NULL, NULL, NULL);
        ScreenDC.SelectStockObject(NULL_BRUSH);
        ScreenDC.SetROP2(R2_NOT);

        ScreenDC.Rectangle(CRect(PreviousPoint, AnchorPoint));         ⇐
        ScreenDC.Rectangle(CRect(AnchorPoint, CurrentPoint));          ⇐
            .
            .
            .
    }

    CView::OnMouseMove(nFlags, point);

}

Note that if there was no previous point set, this would be a problem the first time we drew a rectangle because we’d be drawing a wild rectangle. To avoid that, we set the previous point to the anchor point when the right mouse button first goes down in OnRButtonDown().

void CCopierView::OnRButtonDown(UINT nFlags, CPoint point)

{

    if(CaptureFlag){
        DrawFlag = true;

        AnchorPoint = GetMessagePos();
        PreviousPoint = AnchorPoint;         ⇐
    }

    CView::OnRButtonDown(nFlags, point);

}


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.