Screen Capturing via GDI+ and GDI - Developing a Screen Capture Application
(Page 3 of 5 )
A screen capture application contains at least three steps:
(1) Capture mouse cursor
(2) Allow the user to draw the specified target
(3) Save the target window as a custom bitmap and terminate the operation that captures the mouse movement.
Now, let’s compose the sample application:
(1) Launch Visual Studio 2005, select Visual C++ to create a MFC styled dialog based project, and name it ScreenCapture. Then, to gain a better visual effect, download or manually compose a camera cursor file (*.cur), import it into the project, and name it IDC_CAMERA. After that, declare two public variables within the CscreenCaptureDlg class as follows:
public:
HWND hwndCapture;
CRect rectCapture;
hwndCapture is used to hold the handler of the target window that is to be captured and rectCapture corresponds to the rectangle of the capturing window.
(2) Define the two (WM_MOUSEMOVE and WM_LBUTTONUP) event handlers via the wizard. The complete source code of the two event handlers is listed below:
void CScreenCaptureDlg::OnMouseMove(UINT nFlags, CPoint point)
{
//if the user press the left mouse button and does not release it (i.e. drag), then start to capture
if(nFlags==MK_LBUTTON){
//hide the demo application window in order not to affect the vision of the mouse capturing
ShowWindow(SW_HIDE);
//load the camera shaped mouse cursor, and start to track the movement of the mouse cursor
HCURSOR cur=LoadCursor(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDC_CAMERA));
SetCursor(cur);
SetCapture();
//obtain the handler of the window that corresponds to the current mouse cursor
this->ClientToScreen(&point);
hwndCapture=(HWND)::WindowFromPoint(point);
//obtain the handler of the device context of the screen so as to draw at any positon on the screen
HDC hDC=::GetDC(NULL);
//create a new red paint brush
HPEN hPen=CreatePen(PS_INSIDEFRAME,6,RGB(255,0,0));
//set the draw mode to R2_NOTXORPEN so as not to destroy the original background color
int nMode=SetROP2(hDC,R2_NOTXORPEN);
HPEN hpenOld=(HPEN)SelectObject(hDC,hPen);
//get the rectangle of the window relating to the current mouse cursor
::GetWindowRect(hwndCapture,&rectCapture);
//draw a red rectangle around the window relating to the current mouse cursor to friendly prompt the user
POINT pt[5];
pt[0]=CPoint(rectCapture.left,rectCapture.top);
pt[1]=CPoint(rectCapture.right,rectCapture.top);
pt[2]=CPoint(rectCapture.right,rectCapture.bottom);
pt[3]=CPoint(rectCapture.left,rectCapture.bottom);
pt[4]=CPoint(rectCapture.left,rectCapture.top);
::Polyline(hDC,pt,5);
//first sleep 100 milliseconds and then redraw the red rectangle so as not to destroy the original content
Sleep(100);
::Polyline(hDC,pt,5);
//seemly verbose while effective
::SelectObject(hDC,hpenOld);
::ReleaseDC(NULL,hDC);
}
CDialog::OnMouseMove(nFlags, point);
}
void CScreenCaptureDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
// obtain the width and height of the window the mouse cursor points to
int nWidth=rectCapture.Width();
int nHeight=rectCapture.Height();
HDC hdcScreen,hMemDC;
HBITMAP hBitmap,hOldBitmap;
//set up a handler of the device context of the screen
hdcScreen=CreateDC("DISPLAY",NULL,NULL,NULL);
hMemDC=CreateCompatibleDC(hdcScreen);
// set up a bitmap that is compatible with the above handler of the screen device context and has the same size as the window the current mouse cursor points to
hBitmap=CreateCompatibleBitmap(hdcScreen,nWidth,nHeight);
//select the newly-built bitmap into the memory device context
hOldBitmap=(HBITMAP)SelectObject(hMemDC,hBitmap);
//copy the screen device context into the memory device context
BitBlt(hMemDC,0,0,nWidth,nHeight,hdcScreen,rectCapture.left,rectCapture.top,
SRCCOPY);
DeleteDC(hdcScreen);
DeleteDC(hMemDC);
//return the handler of the bitmap
// Open the clipboard and copy the bitmap to it
OpenClipboard();
EmptyClipboard();
SetClipboardData(CF_BITMAP,hBitmap);
// Close the clipboard
CloseClipboard();
MessageBox("The specified content has been copied to the clipboard!");
ReleaseCapture();
//restore the original state of the demo applicaion’s window
ShowWindow(SW_NORMAL);
CDialog::OnLButtonUp(nFlags, point);
}
Since there are already detailed explanations in the above source code, we don't need to give uncecessary details. The core module of a professional screen capturing application has been accomplished.
Next: Persisting the Captured Images via GDI >>
More Windows Scripting Articles
More By Xianzhong Zhu