bluatigro
Artikelen: 0
Berichten: 6
Lid geworden op: di 30 jul 2013, 16:15

[c++][win32] CaptureScreen( file.bmp )

hoe save ik n bmp van scherm met c++ & win32 ?

dit heb ik al :

ik weet echter niet of CaptureScreen( file ) goed is

Canvas.h :

Code: Selecteer alles


//Canvas.h   4-10-2010

#ifndef CANVAS_H

#define CANVAS_H

#include <windows.h>

#include "CColor.h"

enum ePen { psolid	 = PS_SOLID

		  , dot		= PS_DOT

		  , dash	   = PS_DASH

		  , dashDot	= PS_DASHDOT

		  , dashDotDot = PS_DASHDOTDOT } ;

enum eBrush { bDiagonal  = HS_BDIAGONAL

			, fDiagonal  = HS_FDIAGONAL

			, cross	  = HS_CROSS

			, horizontal = HS_HORIZONTAL

			, diagCross  = HS_DIAGCROSS

			, vertical   = HS_VERTICAL

			, bsolid	 = 0 } ;

class CCanvas

{

public :

  HDC hdc ;

  HWND window ;

  int height , width ;

  void backColor( int clr ) ;

  void brush( int clr , eBrush b = bsolid ) ;

  void CaptureScreen( const char* filename ) ;

  void chord( int x1 , int y1 , int x2 , int y2

			, int x3 , int y3 , int x4 , int y4 ) ;

  void ellipse( int x , int y , int dx , int dy ) ;

  void pen( int clr , int thick = 1

  , ePen line = psolid ) ;

  void pset( int x , int y , int clr ) ;

  void polyBlezier( POINT *p , int max ) ;

  void polygon( POINT *p , int max ) ;

  int getPixel( int x , int y ) ;

  void rectangle( int x , int y , int dx , int dy ) ;

  void roundRect( int x , int y , int dx , int dy

				, int hx , int hy ) ;

  void text( int x , int y

  , char* txt , char* fontname

  , int fontsize  , int clr ) ;

private :

  HDC _hdc ;

  HPEN _pen ;

  HBRUSH _brush ;

} ;

extern CCanvas Canvas ;

#endif



Canvas.cpp

Code: Selecteer alles


//Canvas.cpp   4-10-2010

#include "Canvas.h"

CCanvas Canvas ;

void CCanvas::backColor(int clr)

{

  SetBkColor( hdc , clr ) ;

}

void CCanvas::brush(int clr , eBrush b)

{

  if ( b == bsolid )

  {

	_brush = CreateSolidBrush( clr ) ;

  }

  else

  {

	_brush = CreateHatchBrush( b , clr ) ;

  }

}

/*

void CCanvas::CaptureScreen( const char* filename )

{

  // get screen rectangle

  RECT windowRect ;

  GetWindowRect( window, &windowRect ) ;

  // bitmap dimensions

  int bitmap_dx = windowRect.right - windowRect.left;

  int bitmap_dy = windowRect.bottom - windowRect.top;

  // create file

  ofstream file( filename , ios::binary ) ;

	if( !file ) return ;

	// save bitmap file headers

	BITMAPFILEHEADER fileHeader ;

	BITMAPINFOHEADER infoHeader ;

   fileHeader.bfType	  = 0x4d42 ;

   fileHeader.bfSize	  = 0 ;

   fileHeader.bfReserved1 = 0 ;

   fileHeader.bfReserved2 = 0 ;

   fileHeader.bfOffBits   = sizeof( BITMAPFILEHEADER )

						  + sizeof( BITMAPINFOHEADER ) ;

   infoHeader.biSize		  = sizeof( infoHeader ) ;

   infoHeader.biWidth		 = bitmap_dx ;

   infoHeader.biHeight		= bitmap_dy ;

   infoHeader.biPlanes		= 1 ;

   infoHeader.biBitCount	  = 24 ;

   infoHeader.biCompression   = BI_RGB ;

   infoHeader.biSizeImage	 = 0 ;

   infoHeader.biXPelsPerMeter = 0 ;

   infoHeader.biYPelsPerMeter = 0 ;

   infoHeader.biClrUsed	   = 0 ;

   infoHeader.biClrImportant  = 0 ;

   file.write((char*)&fileHeader, sizeof(fileHeader));

   file.write((char*)&infoHeader, sizeof(infoHeader));

   // dibsection information

   BITMAPINFO info ;

   info.bmiHeader = infoHeader ;

   // ------------------

   // THE IMPORTANT CODE

   // ------------------

   // create a dibsection and

   // blit the window contents to the bitmap

   HDC winDC = GetWindowDC( window ) ;

   HDC memDC = CreateCompatibleDC( winDC ) ;

   BYTE* memory = 0 ;

   HBITMAP bitmap = CreateDIBSection( winDC , &info

   , DIB_RGB_COLORS , (void**)&memory , 0 , 0 ) ;

   SelectObject( memDC , bitmap ) ;

   BitBlt( memDC , 0 , 0 , bitmap_dx , bitmap_dy

		 , winDC , 0 , 0 , SRCCOPY ) ;

   DeleteDC( memDC ) ;

   ReleaseDC( window , winDC ) ;

   //  save dibsection data

   int bytes = ( ( ( 24 * bitmap_dx + 31 )

   & ( ~31 ) ) / 8 ) * bitmap_dy ;

   file.write( memory , bytes ) ;

   // HA HA, forgot paste in the DeleteObject lol, happy now  ;) ?

  DeleteObject( bitmap ) ;

}

*/

void CCanvas::chord( int x1 , int y1

, int x2 , int y2 , int x3 , int y3 , int x4 , int y4 )

{

  Chord( hdc , x1 , y1 , x2 , y2 , x3 , y3 , x4 , y4 ) ;

}

void CCanvas::ellipse(

int x , int y , int dx , int dy )

{

  Ellipse( hdc , x , y , dx , dy ) ;

}

void CCanvas::pen( int clr , int thick , ePen line )

{

   _pen = CreatePen( line , thick , clr ) ;

}

void CCanvas::polyBlezier( POINT *p , int max)

{

  PolyBezier( hdc , p , max ) ;

}

void CCanvas::polygon( POINT *p , int max)

{

  Polygon( hdc , p , max ) ;

}

int CCanvas::getPixel( int x , int y )

{

   return GetPixel( hdc , x , y ) ;

}

void CCanvas::pset( int x , int y , int clr )

{

  SetPixel( hdc , x , y , clr ) ;

}

void CCanvas::rectangle(

  int x , int y , int dx , int dy )

{

  Rectangle( hdc , x , y , dx , dy ) ;

}

void CCanvas::text( int x , int y

, char* txt

, char* fontname

, int fontsize  , int clr )

{

  LOGFONT logfont = { } ;

  // init all members to 0

  strcpy( logfont.lfFaceName

  , fontname ) ; // max 31+1 chars

  logfont.lfHeight = fontsize ;

  HFONT font =

	CreateFontIndirect( &logfont ) ;

  HFONT oldfont =

	(HFONT)SelectObject( hdc , font ) ;

  SetBkMode( hdc , TRANSPARENT ) ;

  SetTextColor( hdc , clr ) ;

  TextOut( hdc , x , y , txt

  , strlen( txt ) ) ;

  SelectObject( hdc , oldfont ) ;

  DeleteObject( font ) ;

}

Terug naar “Informatica en programmeren”