1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
// --- Capture.h ; Rafael Perez-Santana
// I noticed that capturing the screen in win32/GDI required several steps!
// To avoid having to write all this code over and over again, I created this
// class to capture a screen section and display it again when required.
//
// To capture the screen use takepic() and specify the top, left, bottom, right
// co-ordinates of the screen to capture. You can re-display the captured image
// by copying the stored image into the device-context of another application
// by using the copyto() function.
#include <windows.h>
class Capture
{
public:
Capture(HWND hSethWnd); // class constructor
~Capture(); // class destructor
void TakePic(int top, int left, int bottom, int right);
void CopyTo(HWND hwnd);
void CopyClient();
protected:
HWND hWnd;
HDC hdcScreen; // handle to device driver
HDC hdcCompatible; // compatible dc to device driver handle
HBITMAP hbmScreen; // bitmap object
int Top; // top left vertical co-ordinate
int Left; // top left horizontal co-ordinate
int Bottom; // bottom right vertical co-ordinate
int Right; // bottom right horizontal co-ordinate
bool havedata; // states if we have captured the screen
};
// class constructor
Capture::Capture(HWND hSethWnd)
{
hWnd = hSethWnd;
havedata = false; // no image stored at start.
}
// class destructor
Capture::~Capture()
{
// when application terminates, all of the allocated
// resources are given back to the system by deleting them.
DeleteObject(hbmScreen); // delete bitmap object
DeleteDC(hdcScreen); // delete this device-context
DeleteDC(hdcCompatible); // delete this one too
}
// copies the stored image to another application handle
void Capture::CopyTo(HWND hwnd)
{
if (!havedata) return; // cannot proceed, we have nothing to copy!
HDC screendata = GetDC(hwnd); // the area to copy to...
// make the transfer using stored data
BitBlt( screendata, // handle to destination device context
Left, // x-coordinate of destination rectangle's upper-left corner
Top, // y-coordinate of destination rectangle's upper-left corner
Right, // width of destination rectangle
Bottom, // height of destination rectangle
hdcCompatible, // handle to source device context
Left, // x-coordinate of source rectangle's upper-left corner
Top, // y-coordinate of source rectangle's upper-left corner
SRCCOPY // raster operation code
);
ReleaseDC(hwnd,screendata); // release device-context
}
// captures the screen region specified in top, left, bottom & right
void Capture::TakePic(int top, int left, int bottom, int right)
{
// initialize capture settings
Top = top; Left = left; Bottom = bottom; Right = right; // save co-ordinates
int width = right-left; // width for bitmap object
int height = bottom; // height for bitmap object
// create a device-context, this will be the source to copy from.
hdcScreen = CreateDC(
"DISPLAY", // pointer to string specifying driver name
NULL, // pointer to string specifying device name
NULL, // do not use; set to NULL
NULL // pointer to optional printer data
);
// we need to create a CompatibleDC with 'hdcScreen' because
// CreateCompatibleBitmap() only accepts this type of handle.
hdcCompatible = CreateCompatibleDC(hdcScreen);
// now we define the handle to the compatible bitmap
// and we point it to 'hdcScreen' where it is saved.
hbmScreen = CreateCompatibleBitmap(
hdcScreen, // handle to device context
width, // width of bitmap, in pixels
height // height of bitmap, in pixels
);
// The SelectObject() function selects an object into the specified device context.
// The new object replaces the previous object of the same type.
SelectObject(hdcCompatible, hbmScreen);
// now we copy data from the source to our destination 'hdcCompatible'
// which, is actually our handle to the bitmap object we created.
BitBlt( hdcCompatible, // handle to destination device context
left, // x-coordinate of destination rectangle's upper-left corner
top, // y-coordinate of destination rectangle's upper-left corner
right, // width of destination rectangle
bottom, // height of destination rectangle
hdcScreen, // handle to source device context
left, // x-coordinate of source rectangle's upper-left corner
top, // y-coordinate of source rectangle's upper-left corner
SRCCOPY // raster operation code
);
havedata = true; // we now have a stored image.
}
|