Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Estrazione - offscreen.cpp

offscreen.cpp

Caricato da: AldoBaldo
Scarica il programma completo

  1. /**=============================================================================
  2. CLASSE OFFSCREEN v1.4.5 - agosto 2014 ( corretto get_rect( RectF *r ) mancante )
  3. =============================================================================**/
  4.  
  5. #include "offscreen.h"
  6.  
  7.  
  8. static const char kErrOffscreenNonCreato[]
  9.     = "classe OFFSCREEN: impossibile impostare la grafica offscreen";
  10. static const char kErrNienteDC[]
  11.     = "classe OFFSCREEN: impossibile creare il device context";
  12. static const char kErrNienteCostruttoreDiCopia[]
  13.     = "classe OFFSCREEN: costruttore di copia non implementato!";
  14.  
  15.  
  16. OFFSCREEN::OFFSCREEN( HWND hwnd ) {
  17.     HDC dcOrig;
  18.     RECT rAux;
  19.  
  20.     dcOrig      = NULL;
  21.     dc          = NULL;
  22.     bmp         = NULL;
  23.     dc_sfondo   = NULL;
  24.     bmp_sfondo  = NULL;
  25.     p_sfondo    = NULL;
  26.  
  27.     if( hwnd == NULL ) goto errore_e_uscita;
  28.     finestra = hwnd;
  29.  
  30.     dcOrig = GetDC( finestra );
  31.     if( dcOrig == NULL ) goto errore_e_uscita;
  32.  
  33.     dc = CreateCompatibleDC( dcOrig );
  34.     if( dc == NULL ) goto errore_e_uscita;
  35.  
  36.     GetClientRect( finestra, &rAux );
  37.     w = rAux.right;
  38.     h = rAux.bottom;
  39.     bmp = CreateCompatibleBitmap( dcOrig, w, h );
  40.     if( bmp == NULL ) goto errore_e_uscita;
  41.  
  42.     set_sfondo( RGB(0xFF,0xFF,0xFF) );
  43.  
  44.     SelectObject( dc, bmp );
  45.     cancella();
  46.  
  47.     return;
  48.  
  49.     errore_e_uscita:
  50.     if( dcOrig != NULL ) ReleaseDC( finestra, dcOrig );
  51.     if( dc != NULL ) DeleteDC( dc );
  52.     if( bmp != NULL ) DeleteObject( bmp );
  53.     throw kErrOffscreenNonCreato;
  54. }
  55.  
  56.  
  57. OFFSCREEN::OFFSCREEN(const OFFSCREEN& orig) {
  58.     throw kErrNienteCostruttoreDiCopia;
  59. }
  60.  
  61.  
  62. OFFSCREEN::~OFFSCREEN() {
  63.     if( dc != NULL ) {
  64.         SelectObject( dc, (HBITMAP) NULL );
  65.         SelectObject( dc, (HBRUSH) NULL );
  66.         DeleteDC( dc );
  67.     }
  68.  
  69.         if( bmp != NULL ) DeleteObject( bmp );
  70.  
  71.         if( dc_sfondo != NULL ) {
  72.         SelectObject( dc_sfondo, (HBITMAP) NULL );
  73.         DeleteDC( dc_sfondo );
  74.     }
  75.  
  76.     if( p_sfondo != NULL ) DeleteObject( bmp );
  77. }
  78.  
  79.  
  80. void OFFSCREEN::set_sfondo( COLORREF c_sfondo ) {
  81.     HBRUSH p;
  82.  
  83.     p = CreateSolidBrush( c_sfondo );
  84.  
  85.     if( p != NULL ) {
  86.         if( p_sfondo != NULL )
  87.             DeleteObject( p_sfondo );
  88.         p_sfondo = p;
  89.  
  90.         bmp_sfondo = NULL;
  91.     }
  92. }
  93.  
  94.  
  95. void OFFSCREEN::set_sfondo( HBITMAP b_sfondo ) {
  96.     HDC dcTemp = NULL;
  97.  
  98.     if( b_sfondo == NULL ) return;
  99.  
  100.     dcTemp = CreateCompatibleDC( dc );
  101.     if( dcTemp == NULL ) throw kErrNienteDC;
  102.     if( dc_sfondo != NULL ) DeleteDC( dc_sfondo );
  103.     dc_sfondo = dcTemp;
  104.  
  105.     bmp_sfondo = b_sfondo;
  106.     SelectObject( dc_sfondo, bmp_sfondo );
  107. }
  108.  
  109.  
  110. void OFFSCREEN::cancella( const RECT *r ) {
  111.  
  112.     // se non c'e' un'immagine di sfondo, usiamo il colore compatto
  113.     if( bmp_sfondo == NULL || dc_sfondo == NULL ) {
  114.         RECT rAux;
  115.  
  116.         if( r == NULL )
  117.             SetRect( &rAux, 0, 0, w, h );
  118.         else rAux = *r;
  119.  
  120.         FillRect( dc, &rAux, p_sfondo );
  121.     }
  122.     else { // altrimenti copiamo dall'immagine di sfondo
  123.         if( r == NULL ) {
  124.             BitBlt( dc, 0, 0, w, h, dc_sfondo, 0, 0, SRCCOPY );
  125.         }
  126.         else {
  127.             BitBlt(
  128.                 dc,
  129.                 r->left, r->top, r->right-r->left, r->bottom-r->top,
  130.                 dc_sfondo, r->left, r->top, SRCCOPY
  131.             );
  132.         }
  133.     }
  134. }
  135.  
  136.  
  137. void OFFSCREEN::cancella( HRGN r ) {
  138.  
  139.     // se non c'e' un'immagine di sfondo, usiamo il colore compatto
  140.     if( bmp_sfondo == NULL || dc_sfondo == NULL ) {
  141.         FillRgn( dc, r, p_sfondo );
  142.     }
  143.     else { // altrimenti copiamo dall'immagine di sfondo
  144.         RECT rAux;
  145.  
  146.         SelectClipRgn( dc, r );
  147.         GetClipBox( dc, &rAux );
  148.  
  149.         BitBlt(
  150.             dc,
  151.             rAux.left, rAux.top, rAux.right-rAux.left, rAux.bottom-rAux.top,
  152.             dc_sfondo, rAux.left, rAux.top, SRCCOPY
  153.         );
  154.  
  155.         SelectClipRgn( dc, NULL );
  156.     }
  157. }
  158.  
  159.  
  160. #if kOffscreenUsaGdiPlus == TRUE
  161.  
  162. void OFFSCREEN::cancella( const Rect *r ) {
  163.     static RECT rAux;
  164.  
  165.     SetRect(
  166.         &rAux, (LONG)r->X, (LONG)r->Y,
  167.         (LONG)r->GetRight(), (LONG)r->GetBottom()
  168.     );
  169.  
  170.     cancella( &rAux );
  171. }
  172.  
  173. void OFFSCREEN::cancella( const RectF *r ) {
  174.     static RECT rAux;
  175.  
  176.     SetRect(
  177.         &rAux, (LONG)r->X, (LONG)r->Y,
  178.         (LONG)r->GetRight(), (LONG)r->GetBottom()
  179.     );
  180.  
  181.     cancella( &rAux );
  182. }
  183.  
  184. #endif // kOffscreenUsaGdiPlus
  185.  
  186.  
  187. void OFFSCREEN::invalida( const RECT *r ) {
  188.     RECT rAux;
  189.  
  190.     if( r == NULL )
  191.         SetRect( &rAux, 0, 0, w, h );
  192.     else rAux = *r;
  193.  
  194.     InvalidateRect( finestra, &rAux, FALSE );
  195. }
  196.  
  197.  
  198. void OFFSCREEN::invalida( HRGN r ) {
  199.     InvalidateRgn( finestra, r, FALSE );
  200. }
  201.  
  202.  
  203. #if kOffscreenUsaGdiPlus == TRUE
  204.  
  205. void OFFSCREEN::invalida( const Rect *r ) {
  206.     static RECT rAux;
  207.  
  208.     SetRect(
  209.         &rAux, (LONG)r->X, (LONG)r->Y,
  210.         (LONG)r->GetRight(), (LONG)r->GetBottom()
  211.     );
  212.  
  213.     invalida( &rAux );
  214. }
  215.  
  216. void OFFSCREEN::invalida( const RectF *r ) {
  217.     static RECT rAux;
  218.  
  219.     SetRect(
  220.         &rAux, (LONG)r->X, (LONG)r->Y,
  221.         (LONG)r->GetRight(), (LONG)r->GetBottom()
  222.     );
  223.  
  224.     invalida( &rAux );
  225. }
  226.  
  227. #endif // kOffscreenUsaGdiPlus
  228.  
  229.  
  230. void OFFSCREEN::aggiorna( void ) {
  231.     UpdateWindow( finestra );
  232. }
  233.  
  234.  
  235. void OFFSCREEN::mostra( void ) {
  236.     PAINTSTRUCT ps;
  237.     HDC hdc;
  238.  
  239.     hdc = BeginPaint( finestra, &ps );
  240.  
  241.     BitBlt(
  242.         hdc,
  243.         ps.rcPaint.left, ps.rcPaint.top,
  244.         ps.rcPaint.right, ps.rcPaint.bottom,
  245.         dc,
  246.         ps.rcPaint.left, ps.rcPaint.top,
  247.         SRCCOPY
  248.     );
  249.  
  250.     EndPaint( finestra, &ps );
  251. }
  252.  
  253.  
  254. void OFFSCREEN::mostra( LPDRAWITEMSTRUCT ds ) {
  255.     BitBlt(
  256.         ds->hDC, 0, 0, w, h,
  257.         dc, 0, 0, SRCCOPY
  258.     );
  259. }
  260.  
  261.  
  262. RECT *OFFSCREEN::get_RECT( RECT *r ) {
  263.     SetRect( r, 0, 0, w, h );
  264.     return r;
  265. }
  266.  
  267.  
  268. #if kOffscreenUsaGdiPlus == TRUE
  269.  
  270. Rect *OFFSCREEN::get_Rect( Rect *r ) {
  271.     r->X = 0; r->Y = 0; r->Width = w; r->Height = h;
  272.     return r;
  273. }
  274.  
  275. RectF *OFFSCREEN::get_Rect( RectF *r ) {
  276.     r->X = 0; r->Y = 0; r->Width = w; r->Height = h;
  277.     return r;
  278. }
  279.  
  280. #endif // kOffscreenUsaGdiPlus