![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Fast Track Visual C++ 6.0 Programming
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); } Weve outlined the capture site on the screen; all that remains now is to capture it. Capturing Bits from the ScreenWhen 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 programs view, and copy the capture site to the clipboard so other programs can make use of whats 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 were 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 ClipboardWeve 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 weve 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); } Were 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.
|
![]() |
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. |