Results 1 to 1 of 1

Thread: Printscreen / alt+Printscreen including Cursor , in C++

Hybrid View

  1. #1

    Thread Starter
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Printscreen / alt+Printscreen including Cursor , in C++

    An example of taking a screenshot of either the entire desktop , or a window by it's handle. included in the screenshot is the Cursor at it's position at the time of the shot ( unlike windows , which doesn't show the Cursor )
    This for me has proved to be the most challenging of projects, i've built it all in C++ ( .NET win forms , but using standard C++ api calls etc... )
    VB Code:
    1. // at the very top of the Form's .h file ...  
    2. #pragma once
    3. #include <windows.h>
    4. #include "Winuser.h"
    5.  
    6. // then to use the sample ( i've used a button and 2 radio buttons in this case ) ...  
    7.     private: void button1_Click(System::Object *  sender, System::EventArgs *  e)
    8.              {
    9.                  IntPtr handle = IntPtr::Zero;
    10.                  int width = NULL;
    11.                  int height = NULL;
    12.                  int x = NULL;
    13.                  int y = NULL;
    14.  
    15.                  if(radioButton1->Checked == true)
    16.                  {
    17.                      ///
    18.                      // specify the handle of the Form
    19.                      // and the form's sizes / positions.
    20.                      handle = this->get_Handle();
    21.                      width = this->get_Width();
    22.                      height = this->get_Height();
    23.                      x = this->get_Location().get_X();
    24.                      y = this->get_Location().get_Y();
    25.                  }
    26.                  else
    27.                  {
    28.                      ///
    29.                      // specify the handle of the Desktop
    30.                      // and the Desktop's sizes / positions.
    31.                      handle =  GetDesktopWindow();
    32.                      Screen *r = Screen::PrimaryScreen;
    33.                      width = r->Bounds.get_Width();
    34.                      height = r->Bounds.get_Height();
    35.                      x = 0;
    36.                      y = 0;
    37.                  }
    38.                  ///
    39.                  // now lets try to make a Screenshot which includes the Cursor ( unlike the Screenshot that Windows takes )
    40.                  Snapshot(handle , width , height , x , y);
    41.              }
    42.  
    43.     private: void Snapshot(IntPtr hHandle , int width , int height , int x , int y)
    44.              {
    45.                  ///
    46.                  //The GetWindowDC function retrieves the device context (DC) for the entire window, including title bar, menus, and scroll bars.
    47.                  ///See Msdn Article for more info ... [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/pantdraw_0hcz.asp[/url]
    48.                  HDC srcHDC = GetWindowDC((HWND)hHandle.ToInt32());
    49.                  ///
    50.                  //The CreateCompatibleDC function creates a memory device context (DC) compatible with the specified device.
    51.                  ///See Msdn Article for more info ... [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/devcons_499f.asp[/url]
    52.                  HDC destHDC = CreateCompatibleDC(srcHDC);
    53.                  ///
    54.                  //The CreateCompatibleBitmap function creates a bitmap compatible with the device that is associated with the specified device context.
    55.                  ///See Msdn Article for more info ... [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_1cxc.asp[/url]
    56.                  HBITMAP bmpDC = CreateCompatibleBitmap(srcHDC , width , height);
    57.                  ///
    58.                  //The SelectObject function selects an object into the specified device context (DC). The new object replaces the previous object of the same type.
    59.                  ///See Msdn Article for more info ... [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/devcons_9v3o.asp[/url]
    60.                  HGDIOBJ objHDC = SelectObject(destHDC, bmpDC);
    61.                  ///
    62.                  //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.
    63.                  ///See Msdn Article for more info ... [url]http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_0fzo.asp[/url]
    64.                  BitBlt(destHDC, 0, 0, width, height, srcHDC, 0, 0, SRCCOPY);
    65.                  SelectObject(destHDC, objHDC);
    66.                  ///
    67.                  // here i cast a Bitmap ( .NET / C# type Bitmap ) from the C++ style HBITMAP
    68.                  /// for this i use " dynamic_cast " , which is seen in .NET as DirectCast.  
    69.                  Bitmap *bmp = dynamic_cast<Bitmap*>(Bitmap::FromHbitmap(bmpDC));
    70.                  ///
    71.                  //now i create a Graphics object , to enable the Cursor to be Drawn in it's Correct Position.
    72.                  Graphics *g = dynamic_cast<Graphics*>(Graphics::FromImage(bmp));
    73.                  System::Windows::Forms::Cursor *csr=Cursor::get_Current();
    74.                  System::Drawing::Point pnt = (System::Drawing::Point)csr->get_Position();
    75.                  ///
    76.                  // this was quite tricky as creating a new Rectangle threw alsorts of problems, until i found the FromLTRB property.
    77.                  System::Drawing::Rectangle r= System::Drawing::Rectangle::FromLTRB((int)(pnt.get_X() - 10) - x,(int)(pnt.get_Y() - 10) - y,(int)csr->get_Size().get_Width() ,(int)csr->get_Size().get_Width());
    78.                  ///
    79.                  // now i draw the Cursor on to the Graphics object of the bitmap.
    80.                  csr->Draw( g , r);
    81.                  ///
    82.                  //next i clean up any Resources used by the above calls.
    83.                  ReleaseDC((HWND)hHandle.ToInt32(), srcHDC);
    84.                  DeleteDC(destHDC);
    85.                  DeleteObject(bmpDC);
    86.                  ///
    87.                  // finally i save the Bitmap ( this was the Reason for using a .NET Bitmap , makes saving alot simpler ) .
    88.                  bmp->Save("C:\\test123.bmp" , System::Drawing::Imaging::ImageFormat::Bmp );
    89.                  MessageBox(NULL , "ScreenShot Complete","Dynamic's C++ ScreenShot App",MB_ICONINFORMATION);
    90.  
    91.              }
    i've included a sample project ( it's VS2003 , so VS2002 users would need to convert the project or copy the code in to their version )
    Attached Files Attached Files
    Last edited by Hack; Jul 18th, 2008 at 12:50 PM.
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width