Old fashion design for PC-104
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
4.1 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 26/11/2001
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: BackgroundDrawThread.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of BackgroundDrawThread.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "BackgroundDrawThread.h"
  16. #ifdef _DEBUG
  17. #undef THIS_FILE
  18. static char THIS_FILE[]=__FILE__;
  19. #define new DEBUG_NEW
  20. #endif
  21. CBackgroundDrawThread::CBackgroundDrawThread()
  22. {
  23. m_hMutexData = NULL;
  24. m_hMutexDraw = NULL;
  25. m_hEventDraw = NULL;
  26. m_WndRect.SetRectEmpty();
  27. m_DstRect.SetRectEmpty();
  28. m_pBackground = NULL;
  29. m_hWnd = NULL;
  30. }
  31. CBackgroundDrawThread::~CBackgroundDrawThread()
  32. {
  33. Destroy();
  34. }
  35. DWORD CBackgroundDrawThread::ThreadProc()
  36. {
  37. __try
  38. {
  39. HANDLE hEvents[] = { CThread::GetThreadStopEvent(), m_hEventDraw };
  40. CRect NewWndRect(0,0,0,0);
  41. CRect WndRect = NewWndRect;
  42. CRect DstRect = NewWndRect;
  43. while( true )
  44. {
  45. switch( ::WaitForMultipleObjects( 2, hEvents, FALSE, INFINITE ) )
  46. {
  47. case WAIT_OBJECT_0:
  48. return 0;
  49. case WAIT_OBJECT_0+1:
  50. break;
  51. }
  52. ::WaitForSingleObject( m_hMutexData, INFINITE );
  53. NewWndRect = m_WndRect;
  54. DstRect = m_DstRect;
  55. ::ReleaseMutex( m_hMutexData );
  56. ::WaitForSingleObject( m_hMutexDraw, INFINITE );
  57. if( NewWndRect.Width() != WndRect.Width() ||
  58. NewWndRect.Height() != WndRect.Height() )
  59. {
  60. WndRect = NewWndRect;
  61. m_MemoryDC.Create( WndRect.Width(), WndRect.Height() );
  62. }
  63. m_pBackground->Draw( &m_MemoryDC, &DstRect );
  64. CRect rect[4];
  65. rect[0].SetRect( WndRect.left, WndRect.top, DstRect.left, WndRect.bottom );
  66. rect[1].SetRect( DstRect.left, WndRect.top, DstRect.right, DstRect.top );
  67. rect[2].SetRect( DstRect.right, WndRect.top, WndRect.right, WndRect.bottom );
  68. rect[3].SetRect( DstRect.left, DstRect.bottom, DstRect.right, WndRect.bottom );
  69. for( int i = 0; i < 4; i++ )
  70. if( rect[i].Width() > 0 && rect[i].Height() > 0 )
  71. m_MemoryDC.FillSolidRect( rect[i], RGB( 0,0,0 ) );
  72. ::GdiFlush();
  73. ::ReleaseMutex( m_hMutexDraw );
  74. ::PostMessage( m_hWnd, WM_BACKGROUND_DRAW_THREAD, 0, 0 );
  75. }
  76. }
  77. __except(1)
  78. {
  79. TRACE_ERROR( "CBackgroundDrawThread::ThreadProc" );
  80. }
  81. return 0;
  82. }
  83. BOOL CBackgroundDrawThread::InitDraw( LPRECT pWndRect, LPRECT pDstRect )
  84. {
  85. if( ::WaitForSingleObject( m_hMutexData, INFINITE ) != WAIT_OBJECT_0 )
  86. return FALSE;
  87. m_WndRect = pWndRect;
  88. m_DstRect = pDstRect;
  89. ::ReleaseMutex( m_hMutexData );
  90. return ::SetEvent( m_hEventDraw );
  91. }
  92. BOOL CBackgroundDrawThread::Draw( CDC *pDC )
  93. {
  94. if( !pDC )
  95. return FALSE;
  96. if( ::WaitForSingleObject( m_hMutexDraw, 0UL ) != WAIT_OBJECT_0 )
  97. return FALSE;
  98. pDC->BitBlt( 0, 0, m_WndRect.right - m_WndRect.left, m_WndRect.bottom - m_WndRect.top, &m_MemoryDC, 0, 0, SRCCOPY );
  99. return ::ReleaseMutex( m_hMutexDraw );
  100. }
  101. BOOL CBackgroundDrawThread::Create( CWnd *pWnd, CBackground *pBackground )
  102. {
  103. Destroy();
  104. m_pBackground = pBackground;
  105. if( !pBackground )
  106. return FALSE;
  107. if( !pWnd )
  108. return FALSE;
  109. m_hWnd = pWnd->GetSafeHwnd();
  110. if( !m_hWnd )
  111. return FALSE;
  112. m_hMutexData = ::CreateMutex( NULL, FALSE, NULL );
  113. if( !m_hMutexData )
  114. return FALSE;
  115. m_hMutexDraw = ::CreateMutex( NULL, FALSE, NULL );
  116. if( !m_hMutexDraw )
  117. return FALSE;
  118. m_hEventDraw = ::CreateEvent( NULL, FALSE, FALSE, NULL );
  119. if( !m_hEventDraw )
  120. return FALSE;
  121. return CThread::Start( FALSE );
  122. }
  123. void CBackgroundDrawThread::Destroy()
  124. {
  125. CThread::Stop( TRUE );
  126. if( m_hMutexData )
  127. {
  128. ::CloseHandle( m_hMutexData );
  129. m_hMutexData = NULL;
  130. }
  131. if( m_hMutexDraw )
  132. {
  133. ::CloseHandle( m_hMutexDraw );
  134. m_hMutexDraw = NULL;
  135. }
  136. if( m_hEventDraw )
  137. {
  138. ::CloseHandle( m_hEventDraw );
  139. m_hEventDraw = NULL;
  140. }
  141. m_WndRect.SetRectEmpty();
  142. m_DstRect.SetRectEmpty();
  143. m_pBackground = NULL;
  144. m_hWnd = NULL;
  145. }