Click here to Skip to main content
15,881,836 members
Articles / Desktop Programming / Win32

Screen Capture (Simple Win32 Dialog Based)

Rate me:
Please Sign up or sign in to vote.
4.62/5 (19 votes)
11 Nov 2007CPOL3 min read 165.6K   11.6K   71   23
It's a simple dialog based Screen Capture that uses Global Mouse Hooks to capture the area selected by user, controls( i.e. Buttons, etc.), and any other Window.

Introduction

I have just started programming in VC++, and this is my first article on CodeProject. I have created this project after reading the materials from CodeProject itself.

Everybody knows about the Screen Capture (Prt Scr Button) utility that comes with each and every Microsoft Windows. So what is the necessity of another Screen Capture application. But the Screen Capture utility that ships with Windows is very simple, you can only capture the whole screen. But what if you have to capture the part of the screen or say only a particular control on other window. Don't worry; here is the application for that. Using this application, you can capture the Selected area rather than the whole window. You can also capture any window or control by just clicking on it. (You can even capture only the start button or clock on task bar). Isn't it good! And it uses a simple Win32 API function for that.

Background

This article uses the two main functions of the Win32 API:

  1. SetWindowsHookEx(): To Set the global Mouse Hook (see MSDN for more help about this.) Global Mouse Hook is necessary to capture the Mouse Event from outside of our window.
  2. BitBlt() (pronounced as Bitblit): Used to transfer color data between source device context and destination device context. It is used here to Capture the Screen.

Using the Code

MyMouseHook.cpp

Below is the code from MyMouseHook.cpp. Here we are sending MOUSE_DOWN message when user presses the left Mouse Button and MOUSE_UP when user leaves the left Mouse Button. Both of these are user messages.

C++
LRESULT CALLBACK MyMouseHookProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
     //Below Function will Return the Handle of window whose Caption is "Capture "
     //We will use this handle to send Message.(Handle is handle to Our Application )

            HWND hWndApp = FindWindow(0,L"Capture");
              //Wpram will contain which mouse event has been generated 
              // We will send the User Message for each event we require.
            if(wParam== WM_LBUTTONDOWN || wParam == WM_NCLBUTTONDOWN)
            {
                PostMessage(hWndApp,MOUSE_DOWN,0,0);
            } 
            if(wParam == WM_LBUTTONUP)
            {   
            PostMessage(hWndApp,MOUSE_UP,0,0);
            }
        }

ScreenCaptureDlg.cpp

Below is the part of the code used in ScreenCaptureDlg.cpp File. When the user first presses the Mouse Button after pressing the Capture button, we take that point as the starting point in Begpt as a starting point. And after dragging the Mouse when the user releases the button, we take the End points of the Selected Rectangle in Endpt. Then we capture the area between this Begpt and Endpt from Desktop to MemDC and send it to the SaveBitmap()function which will save DC into .bmp file. And then we will create a process to show the .bmp file into PAIN application. Don't worry about the other function I haven't explain here. I have explained all the things well in the code:

C++
case MOUSE_DOWN:
    if(CaptureActWnd == TRUE)
        {   
        GetCursorPos(&Begpt);              	//Will take the Cursor Position
        hWndActWnd= WindowFromPoint(Begpt);	//Will get the Handle of Window 
					//Where Your Mouse is Currently
        CaptureActiveWindow();            	//Will Call the Function to Capture 
					//That Window    
        EndDialog(hWndDlg,LOWORD(lParam)); 	//Will End the Dialog
        PostQuitMessage(0);                 	//Will destroy Window
        }
        OutputDebugString( L"Desktop -GDI, MOUSE_DOWN");
        GetCursorPos(&Begpt);        	// Will Store the Current Cursor 
					// position in Begpt
        break;                        	// Mouse is Down Begpt.x = left, 
					// Begpt.y = Top     

case MOUSE_UP:
    if(CaptureActWnd == FALSE)
        {
        GetCursorPos(&Endpt);	//As the Mouse is UP We will get Out Bottom 
				//Right Points in EndPt.
        DrawBox();            	//Will Draw the Rectangle Around selected area
        // Create a memory device context Compatible with Dialog DC
        MemDC = CreateCompatibleDC(DlgDC);
        // Create a compatible bitmap 
        hBitmap = CreateCompatibleBitmap(DlgDC,width,height);
        // Select the bitmap into the memory context
        SelectObject(MemDC,hBitmap);
        // This will copy the our Selected Area into MemDC
        BitBlt(MemDC,0,0,width,height ,DeskDC,Begpt.x,Begpt.y,SRCCOPY);    
        //Will Save the Device Context into Sample.bmp
        SaveBitmap(MemDC, hBitmap,"Sample.bmp");
         //Will show you the Capture Area Into MSPAINT
        ShowImage();                                
         //Send Message to Quit the Window.
        SendMessage(hWndDlg,WM_COMMAND,(WPARAM)IDC_Exit,0);
        }
        break;  

Points of Interest

I tried to capture WM_MOUSEMOVE event to draw the rectangle on screen rather than capture only WM_LBUTTONDOWN and WM_LBUTTONUP but it was making the application very sluggish. So I left it and took only Starting Point and End Points to draw the rectangle and capture the screen. So you will not be able to see the rectangle when you will be selecting the area, but the rectangle will appear when you will complete your selection.

Special Thanks

To Arun Krishnan for his nice article on Desktop Calendar from where I came to know how to save the DC(Device Context)in .bmp file.

To Mumtaz Zaheer for his nice project about Sweep the Minesweeper from where I came to know about global hook.

History

There was a little flaw in the previous version of the application. When it was capturing the Window, it was failing to capture the Title bar which is also important. I have fixed the problem in the updated version.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Tester / Quality Assurance
India India
His name is Gaurang Shah. He has completed his bachelor in computer science.Has worked as a System programmer. Currently working as QA Analyst in Pune.

Comments and Discussions

 
Suggestionoption without needing DLL Pin
Member 147526384-Mar-20 9:05
Member 147526384-Mar-20 9:05 
QuestionALT+PRINT prints the activated window Pin
francwalter16-Feb-14 19:06
francwalter16-Feb-14 19:06 
QuestionWill it work for Multiple screens? Pin
Member-42540443-Sep-13 8:06
Member-42540443-Sep-13 8:06 
Questioncapture Pin
zeppp24-Apr-12 20:06
zeppp24-Apr-12 20:06 
GeneralBLOWUP.C -- Video Magnifier Program by Charles Petzold, is very very better!!! Pin
merdaig1211-Jul-10 10:07
merdaig1211-Jul-10 10:07 
GeneralThank you share Pin
JoshuaTsai2-Dec-09 21:19
JoshuaTsai2-Dec-09 21:19 
QuestionDid you test your updated version ? Pin
Member 427768226-Jun-09 12:01
Member 427768226-Jun-09 12:01 
GeneralDraw Rectangle Pin
Render Plus9-May-09 13:04
Render Plus9-May-09 13:04 
GeneralRe: Draw Rectangle Pin
GauranG Shah24-Jun-09 2:29
GauranG Shah24-Jun-09 2:29 
GeneralFirst straightforward code Pin
bulg23-Oct-08 12:24
bulg23-Oct-08 12:24 
GeneralThank you for share! Pin
cigogo1-Jul-08 17:34
cigogo1-Jul-08 17:34 
GeneralThank you Pin
wenzis6-May-08 16:07
wenzis6-May-08 16:07 
Question::FindWindow problem.. Pin
shizu looi16-Dec-07 21:21
shizu looi16-Dec-07 21:21 
QuestionSave Bitmap to another format Pin
dubbele onzin21-Nov-07 22:06
dubbele onzin21-Nov-07 22:06 
QuestionHow can I modify this to Capture the Desktop? Pin
dubbele onzin16-Nov-07 1:08
dubbele onzin16-Nov-07 1:08 
AnswerRe: How can I modify this to Capture the Desktop? Pin
GauranG Shah19-Nov-07 19:34
GauranG Shah19-Nov-07 19:34 
GeneralRe: How can I modify this to Capture the Desktop? Pin
dubbele onzin21-Nov-07 22:07
dubbele onzin21-Nov-07 22:07 
AnswerRe: How can I modify this to Capture the Desktop? Pin
Manikandan22-Sep-08 17:49
Manikandan22-Sep-08 17:49 
GeneralVery good Pin
nirdhar22-Oct-07 23:04
nirdhar22-Oct-07 23:04 
GeneralRe: Very good Pin
GauranG Shah23-Oct-07 2:51
GauranG Shah23-Oct-07 2:51 
GeneralCapture the active window in windows Pin
GurliGebis6-Sep-07 23:00
GurliGebis6-Sep-07 23:00 
You don't need a special tool to capture the active window in Windows, all you need to do is use ALT+Print Screen.
GeneralRe: Capture the active window in windows Pin
GauranG Shah7-Sep-07 19:31
GauranG Shah7-Sep-07 19:31 
GeneralNice Work !!! Pin
DharmeshC6-Sep-07 3:39
DharmeshC6-Sep-07 3:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.