Back

// at the very top of the Form's .h file ...  
#pragma once
#include <windows.h>
#include "Winuser.h"

// then to use the sample ( i've used a button and 2 radio buttons in this case ) ...  
    private: void button1_Click(System::Object *  sender, System::EventArgs *  e)
             {
                 IntPtr handle = IntPtr::Zero;
                 int width = NULL;
                 int height = NULL;
                 int x = NULL;
                 int y = NULL;

                 if(radioButton1->Checked == true)
                 {
                     ///
                     // specify the handle of the Form
                     // and the form's sizes / positions.

                     handle = this->get_Handle();
                     width = this->get_Width();
                     height = this->get_Height();
                     x = this->get_Location().get_X();
                     y = this->get_Location().get_Y();
                 }
                 else
                 {
                     ///
                     // specify the handle of the Desktop
                     // and the Desktop's sizes / positions.

                     handle =  GetDesktopWindow();
                     Screen *r = Screen::PrimaryScreen;
                     width = r->Bounds.get_Width();
                     height = r->Bounds.get_Height();
                     x = 0;
                     y = 0;
                 }
                 ///
                 // now lets try to make a Screenshot which includes the Cursor ( unlike the Screenshot Windows takes )

                 Snapshot(handle , width , height , x , y);
             }

    private: void Snapshot(IntPtr hHandle , int width , int height , int x , int y)
             {
                 ///
                 //The GetWindowDC function retrieves the device context (DC) for the entire window, including title bar, menus, and scroll bars.
                 ///See Msdn Article for more info ... http://msdn.microsoft.com/library/d...ntdraw_0hcz.asp

                 HDC srcHDC = GetWindowDC((HWND)hHandle.ToInt32());
                 ///
                 //The CreateCompatibleDC function creates a memory device context (DC) compatible with the specified device.
                 ///See Msdn Article for more info ... http://msdn.microsoft.com/library/d...evcons_499f.asp

                 HDC destHDC = CreateCompatibleDC(srcHDC);
                 ///
                 //The CreateCompatibleBitmap function creates a bitmap compatible with the device that is associated with the specified device context.
                 ///See Msdn Article for more info ... http://msdn.microsoft.com/library/d...itmaps_1cxc.asp

                 HBITMAP bmpDC = CreateCompatibleBitmap(srcHDC , width , height);
                 ///
                 //The SelectObject function selects an object into the specified device context (DC). The new object replaces the previous object of the same type.
                 ///See Msdn Article for more info ... http://msdn.microsoft.com/library/d...evcons_9v3o.asp

                 HGDIOBJ objHDC = SelectObject(destHDC, bmpDC);
                 ///
                 //The BitBlt function performs a bit-block transfer of the color data corresponding to a rectangle of pixels from the specified source device context into a destination device context.
                 ///See Msdn Article for more info ... http://msdn.microsoft.com/library/d...itmaps_0fzo.asp

                 BitBlt(destHDC, 0, 0, width, height, srcHDC, 0, 0, SRCCOPY);
                 SelectObject(destHDC, objHDC);
                 ///
                 // here i cast a Bitmap ( .NET / C# type Bitmap ) from the C++ style HBITMAP
                 /// for this i use " dynamic_cast " , which is seen in .NET as DirectCast.
 
                 Bitmap *bmp = dynamic_cast<Bitmap*>(Bitmap::FromHbitmap(bmpDC));
                 ///
                 //now i create a Graphics object , to enable the Cursor to be Drawn in it's Correct Position.

                 Graphics *g = dynamic_cast<Graphics*>(Graphics::FromImage(bmp));
                 System::Windows::Forms::Cursor *csr=Cursor::get_Current();
                 System::Drawing::Point pnt = (System::Drawing::Point)csr->get_Position();
                 ///
                 // this was quite tricky as creating a new Rectangle threw alsorts of problems, until i found the FromLTRB property.

                 System::Drawing::Rectangle r= System::Drawing::Rectangle::FromLTRB((int)(pnt.get_X() - 10) - x,(int)(ppnt.get_Y() - 10) - y,(int)csr->get_Size().get_Width() ,(int)csr->get_Size().get_Width());
                 ///
                 // now i draw the Cursor on to the Graphics object of the bitmap.

                 csr->Draw( g , r);
                 ///
                 //next i clean up any Resources used by the above calls.

                 ReleaseDC((HWND)hHandle.ToInt32(), srcHDC);
                 DeleteDC(destHDC);
                 DeleteObject(bmpDC);
                 ///
                 // finally i save the Bitmap ( this was the Reason for using a .NET Bitmap , makes saving alot simpler ) .

                 bmp->Save("C:\\test123.bmp" , System::Drawing::Imaging::ImageFormat::Bmp );
                 MessageBox(NULL , "ScreenShot Complete","Dynamic's C++ ScreenShot App",MB_ICONINFORMATION);
             }