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


Our final step in mouse move is to make the current point the previous point now that the rectangle has been redrawn and will be drawn over the next time.

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));

        PreviousPoint = CurrentPoint;                      ⇐
    }

    CView::OnMouseMove(nFlags, point);

}

We’ve outlined the capture site on the screen; all that remains now is to capture it.

Capturing Bits from the Screen

When the user releases the right mouse button, our program should do three things. It should erase the last rectangle drawn on the screen, copy the capture site to the program’s view, and copy the capture site to the clipboard so other programs can make use of what’s been captured.

We start by erasing the last rectangle drawn on the screen; that rectangle stretches from the anchor point to the previous point, so that process looks like this in OnRButtonUp() (note that since we’re about to complete the capture, we set DrawFlag to false):

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

{
    if(CaptureFlag && DrawFlag){

        DrawFlag = false;

        int Width, Height;
        CPoint CurrentPoint = GetMessagePos();
        CDC ScreenDC;

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

        ScreenDC.Rectangle(CRect(AnchorPoint, PreviousPoint));
              .
              .
              .
    CView::OnRButtonUp(nFlags, point);

}

Now we copy the capture site from the screen to the clipboard.

Placing a Bitmap in the Clipboard

We’ve already seen how to copy text to the clipboard using the clipboard format CF_TEXT; now we use the bitmap format, CF_BITMAP.

To do the actual copying, we use the powerful BitBlt() function. This function needs to know the upper left point at which to start copying bits as well as the width and height of the bitmap to copy. That bitmap stretches on the screen from the anchor point to the current point, so we find the upper left corner, width, and height as shown in the following example:

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

{
    if(CaptureFlag && DrawFlag){

        DrawFlag = false;

        int Width, Height;
        CPoint CurrentPoint = GetMessagePos();
        CDC ScreenDC;

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

        ScreenDC.Rectangle(CRect(AnchorPoint, PreviousPoint));

        CPoint LeftUpper(min(AnchorPoint.x, CurrentPoint.x),
min(AnchorPoint.y, CurrentPoint.y));                               ⇐

        Height = abs(AnchorPoint.y - CurrentPoint.y);         ⇐
        Width = abs(AnchorPoint.x - CurrentPoint.x);           ⇐
            .
            .

            .

We also need a new bitmap to copy the bitmap to; to use BitBlt() with that new bitmap, we have to embed that new bitmap in a memory device context.

We create a new memory context that is compatible with the one we’ve already created for the screen, ScreenDC, and a new bitmap also compatible with that device context. Next, we load the new bitmap into the memory device context.

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

{
    if(CaptureFlag && DrawFlag){
           .
           .
           .
        Height = abs(AnchorPoint.y - CurrentPoint.y);
        Width = abs(AnchorPoint.x - CurrentPoint.x);

        CDC MemoryDC;                                                  ⇐
        CBitmap bitmap;                                                ⇐

        MemoryDC.CreateCompatibleDC(&ScreenDC);                        ⇐
        bitmap.CreateCompatibleBitmap(&ScreenDC, Width, Height);       ⇐
        MemoryDC.SelectObject(&bitmap);                                ⇐
           .
           .
           .
    }

    CView::OnRButtonUp(nFlags, point);

}

We’re ready to use BitBlt(). We pass that function the location in destination rectangle, (0, 0); the width and height of the bitmap to copy; a pointer to the source device context and the location of the source bitmap in that device context; and a constant, SRCCOPY, to indicate that we want to copy the bitmap as is without modification.

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

{
    if(CaptureFlag && DrawFlag){

        DrawFlag = false;
            .
            .
            .
        MemoryDC.CreateCompatibleDC(&ScreenDC);
        bitmap.CreateCompatibleBitmap(&ScreenDC, Width, Height);
        MemoryDC.SelectObject(&bitmap);

        MemoryDC.BitBlt(0, 0, Width, Height, &ScreenDC,
LeftUpper.x, 
LeftUpper.y, SRCCOPY);               ⇐
            .
            .

            .

We set the last parameter in the call to BitBlt() to SRCCOPY to make a faithful copy of the bitmap. The other possibilities appear in Table 6.2.


Table 6.2 BitBlt() Parameters

BitBlt() Parameter Does this
BLACKNESS Turns all output black.
DSTINVERT Inverts the destination bitmap.
MERGECOPY Combines the pattern and the source bitmap using the Boolean AND operator.
MERGEPAINT Combines the inverted source bitmap with the destination bitmap using the Boolean OR operator.
NOTSRCCOPY Copies the inverted source bitmap to the destination.
NOTSRCERASE Inverts the result of combining the destination and source bitmaps using the Boolean OR operator.
PATCOPY Copies the pattern to the destination bitmap.
PATINVERT Combines the destination bitmap with the pattern using the Boolean XOR operator.
PATPAINT Combines the inverted source bitmap with the pattern using the Boolean OR operator. Combines the result of this operation with the destination bitmap using the Boolean OR operator.
SRCAND Combines pixels of the destination and source bitmaps using the Boolean AND operator.
SRCCOPY Copies the source bitmap to the destination bitmap.
SRCERASE Inverts the destination bitmap and combines the result with the source bitmap using the Boolean AND operator.
SRCINVERT Combines pixels of the destination and source bitmaps using the Boolean XOR operator.
SRCPAINT Combines pixels of the destination and source bitmaps using the Boolean OR operator.
WHITENESS Turns all output white.


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.