|
|
//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright � 2000-2002. All Rights Reserved.
// Version 1.0 01/01/2000
// Last modification: 26/11/2001
// PROJECT:
// SUBSYSTEM:
// FILE: BackgroundDrawThread.cpp
// AUTHOR: Sergey Chizhevskiy
// OVERVIEW
// ~~~~~~~~
// Source file of BackgroundDrawThread.cpp
//----------------------------------------------------------------------------
#include "stdafx.h"
#include "BackgroundDrawThread.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;#define new DEBUG_NEW
#endif
CBackgroundDrawThread::CBackgroundDrawThread(){ m_hMutexData = NULL; m_hMutexDraw = NULL; m_hEventDraw = NULL; m_WndRect.SetRectEmpty(); m_DstRect.SetRectEmpty(); m_pBackground = NULL; m_hWnd = NULL;}
CBackgroundDrawThread::~CBackgroundDrawThread(){ Destroy();}
DWORD CBackgroundDrawThread::ThreadProc(){ __try { HANDLE hEvents[] = { CThread::GetThreadStopEvent(), m_hEventDraw };
CRect NewWndRect(0,0,0,0); CRect WndRect = NewWndRect; CRect DstRect = NewWndRect;
while( true ) { switch( ::WaitForMultipleObjects( 2, hEvents, FALSE, INFINITE ) ) { case WAIT_OBJECT_0: return 0;
case WAIT_OBJECT_0+1: break; }
::WaitForSingleObject( m_hMutexData, INFINITE ); NewWndRect = m_WndRect; DstRect = m_DstRect; ::ReleaseMutex( m_hMutexData );
::WaitForSingleObject( m_hMutexDraw, INFINITE );
if( NewWndRect.Width() != WndRect.Width() || NewWndRect.Height() != WndRect.Height() ) { WndRect = NewWndRect; m_MemoryDC.Create( WndRect.Width(), WndRect.Height() ); }
m_pBackground->Draw( &m_MemoryDC, &DstRect );
CRect rect[4]; rect[0].SetRect( WndRect.left, WndRect.top, DstRect.left, WndRect.bottom ); rect[1].SetRect( DstRect.left, WndRect.top, DstRect.right, DstRect.top ); rect[2].SetRect( DstRect.right, WndRect.top, WndRect.right, WndRect.bottom ); rect[3].SetRect( DstRect.left, DstRect.bottom, DstRect.right, WndRect.bottom ); for( int i = 0; i < 4; i++ ) if( rect[i].Width() > 0 && rect[i].Height() > 0 ) m_MemoryDC.FillSolidRect( rect[i], RGB( 0,0,0 ) );
::GdiFlush();
::ReleaseMutex( m_hMutexDraw );
::PostMessage( m_hWnd, WM_BACKGROUND_DRAW_THREAD, 0, 0 ); } } __except(1) { TRACE_ERROR( "CBackgroundDrawThread::ThreadProc" ); } return 0;}
BOOL CBackgroundDrawThread::InitDraw( LPRECT pWndRect, LPRECT pDstRect ){ if( ::WaitForSingleObject( m_hMutexData, INFINITE ) != WAIT_OBJECT_0 ) return FALSE;
m_WndRect = pWndRect; m_DstRect = pDstRect;
::ReleaseMutex( m_hMutexData );
return ::SetEvent( m_hEventDraw );}
BOOL CBackgroundDrawThread::Draw( CDC *pDC ){ if( !pDC ) return FALSE;
if( ::WaitForSingleObject( m_hMutexDraw, 0UL ) != WAIT_OBJECT_0 ) return FALSE;
pDC->BitBlt( 0, 0, m_WndRect.right - m_WndRect.left, m_WndRect.bottom - m_WndRect.top, &m_MemoryDC, 0, 0, SRCCOPY ); return ::ReleaseMutex( m_hMutexDraw );}
BOOL CBackgroundDrawThread::Create( CWnd *pWnd, CBackground *pBackground ){ Destroy();
m_pBackground = pBackground; if( !pBackground ) return FALSE;
if( !pWnd ) return FALSE;
m_hWnd = pWnd->GetSafeHwnd(); if( !m_hWnd ) return FALSE;
m_hMutexData = ::CreateMutex( NULL, FALSE, NULL ); if( !m_hMutexData ) return FALSE;
m_hMutexDraw = ::CreateMutex( NULL, FALSE, NULL ); if( !m_hMutexDraw ) return FALSE;
m_hEventDraw = ::CreateEvent( NULL, FALSE, FALSE, NULL ); if( !m_hEventDraw ) return FALSE;
return CThread::Start( FALSE );}
void CBackgroundDrawThread::Destroy(){ CThread::Stop( TRUE );
if( m_hMutexData ) { ::CloseHandle( m_hMutexData ); m_hMutexData = NULL; }
if( m_hMutexDraw ) { ::CloseHandle( m_hMutexDraw ); m_hMutexDraw = NULL; }
if( m_hEventDraw ) { ::CloseHandle( m_hEventDraw ); m_hEventDraw = NULL; }
m_WndRect.SetRectEmpty(); m_DstRect.SetRectEmpty(); m_pBackground = NULL; m_hWnd = NULL;}
|