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.

255 lines
6.8 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: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: ImageWnd.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of ImageWnd.cpp
  13. //----------------------------------------------------------------------------
  14. // ImageWnd.cpp : implementation file
  15. //
  16. #include "stdafx.h"
  17. #include "ImageWnd.h"
  18. #ifdef _DEBUG
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CImageWnd
  25. CImageWnd::CImageWnd()
  26. {
  27. }
  28. CImageWnd::~CImageWnd()
  29. {
  30. }
  31. BEGIN_MESSAGE_MAP(CImageWnd, CWnd)
  32. //{{AFX_MSG_MAP(CImageWnd)
  33. ON_WM_PAINT()
  34. ON_WM_SIZE()
  35. ON_WM_LBUTTONDOWN()
  36. ON_WM_LBUTTONUP()
  37. ON_WM_MOUSEMOVE()
  38. //}}AFX_MSG_MAP
  39. END_MESSAGE_MAP()
  40. /////////////////////////////////////////////////////////////////////////////
  41. // CImageWnd message handlers
  42. BOOL CImageWnd::Create(CWnd *pParentWnd, int x, int y, int cx, int cy)
  43. {
  44. CString strClassName = ::AfxRegisterWndClass( CS_HREDRAW | CS_VREDRAW, ::LoadCursor( NULL, IDC_ARROW ), NULL, NULL );
  45. CRect rect( x, y, x + cx, y + cy );
  46. if( CWnd::Create( strClassName, NULL, WS_CHILD|WS_VISIBLE, rect, pParentWnd, 0 ) )
  47. {
  48. m_MemDC.Create( cx, cy );
  49. CClientDC dc( this );
  50. m_CopyDC.CreateCompatibleDC( &dc );
  51. return TRUE;
  52. }
  53. return FALSE;
  54. }
  55. BOOL CImageWnd::Create( CWnd *pParentWnd, CRect *pRect )
  56. {
  57. return CImageWnd::Create( pParentWnd, pRect->left, pRect->top, pRect->Width(), pRect->Height() );
  58. }
  59. void CImageWnd::PostNcDestroy()
  60. {
  61. m_CopyDC.DeleteDC();
  62. CWnd::PostNcDestroy();
  63. }
  64. void CImageWnd::OnPaint()
  65. {
  66. CPaintDC dc(this); // device context for painting
  67. CRect rect;
  68. GetClientRect( &rect );
  69. CSize size = m_MemDC.GetSize();
  70. CLgMemoryDC memDC;
  71. memDC.Create( size.cx, size.cy );
  72. memDC.BitBlt( 0, 0, size.cx, size.cy, &m_MemDC, 0, 0, SRCCOPY );
  73. POSITION pos = m_List.GetHeadPosition();
  74. while( pos )
  75. {
  76. COFTemplateDCBit *pBit = m_List.GetNext( pos );
  77. CLgDPoint dp = pBit->GetPos();
  78. CPoint p( (int)(dp.x*size.cx), (int)(dp.y*size.cy) );
  79. CRect r( p.x - 10, p.y - 10, p.x + 10, p.y + 10 );
  80. memDC.FillSolidRect( &r, RGB( 0x3F,0x3F,0x3F ) );
  81. memDC.Draw3dRect( &r, RGB( 0xFF,0xFF,0xFF), RGB( 0x7F,0x7F,0x7F) );
  82. COLORREF oldColor = memDC.SetTextColor( RGB(0xFF,0xFF,0xFF) );
  83. int oldBkMode = memDC.SetBkMode( TRANSPARENT );
  84. memDC.DrawText( pBit->GetName(), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
  85. memDC.SetBkMode( oldBkMode );
  86. memDC.SetTextColor( oldColor );
  87. }
  88. dc.BitBlt( 0, 0, rect.Width(), rect.Height(), &memDC, 0, 0, SRCCOPY );
  89. }
  90. void CImageWnd::SetImage( LPBITMAPINFO pBmi, LPBYTE pBuffer )
  91. {
  92. if( pBmi && pBuffer )
  93. {
  94. // create DDB bitmap indirect
  95. LPBYTE pBits = NULL;
  96. HBITMAP hBitmap = ::CreateDIBSection( m_CopyDC.GetSafeHdc(), pBmi, DIB_RGB_COLORS, (void**)&pBits, NULL, 0UL );
  97. if( pBits )
  98. {
  99. // copy data to image bits
  100. ::CopyMemory( pBits, pBuffer, pBmi->bmiHeader.biSizeImage );
  101. // create temp CBitmap object and attach it to DDB bitmap
  102. CBitmap bmp;
  103. bmp.Attach( hBitmap );
  104. // select temp bitmap into m_CopyDC
  105. CBitmap *pBitmapOld = m_CopyDC.SelectObject( &bmp );
  106. // copy from m_CopyDC to m_MemDC
  107. int nOldStretchMode = m_MemDC.SetStretchBltMode( COLORONCOLOR );
  108. m_MemDC.StretchBlt( 0, 0, m_MemDC.GetSize().cx, m_MemDC.GetSize().cy, &m_CopyDC,
  109. 0, 0, pBmi->bmiHeader.biWidth, pBmi->bmiHeader.biHeight, SRCCOPY );
  110. m_MemDC.SetStretchBltMode( nOldStretchMode );
  111. // restore selected bitmap in m_CopyDC
  112. m_CopyDC.SelectObject( pBitmapOld );
  113. // detach and delete DDB bitmap
  114. bmp.Detach();
  115. ::DeleteObject( hBitmap );
  116. }
  117. }
  118. else
  119. {
  120. // clear bitmap (filling with black)
  121. CRect rect( 0, 0, m_MemDC.GetSize().cx, m_MemDC.GetSize().cy );
  122. m_MemDC.FillSolidRect( &rect, RGB(0,0,0) );
  123. COLORREF nOldTextColor = m_MemDC.SetTextColor( RGB(0x7F,0x7F,0x7F) );
  124. m_MemDC.DrawText( CString( _T("No image") ), &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
  125. m_MemDC.SetTextColor( nOldTextColor );
  126. }
  127. if( GetSafeHwnd() )
  128. {
  129. InvalidateRect( NULL );
  130. RedrawWindow();
  131. }
  132. }
  133. void CImageWnd::OnSize(UINT nType, int cx, int cy)
  134. {
  135. CWnd::OnSize(nType, cx, cy);
  136. if( cx && cy )
  137. m_MemDC.Create( cx, cy );
  138. }
  139. void CImageWnd::SetImage( CString &file )
  140. {
  141. HINSTANCE hInst = (HINSTANCE)::GetWindowLong( GetSafeHwnd(), GWL_HINSTANCE );
  142. // load bitmap from file
  143. HBITMAP hBitmap = (HBITMAP)::LoadImage( hInst, file, IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE|LR_LOADFROMFILE );
  144. SetImage( hBitmap );
  145. if( hBitmap )
  146. ::DeleteObject( hBitmap );
  147. }
  148. void CImageWnd::SetImage( CBitmap *pBitmap )
  149. {
  150. if( pBitmap )
  151. SetImage( (HBITMAP)pBitmap->GetSafeHandle() );
  152. else
  153. SetImage( (HBITMAP)NULL );
  154. }
  155. void CImageWnd::SetImage( HBITMAP hBitmap )
  156. {
  157. if( hBitmap )
  158. {
  159. // create temp CBitmap object and attach it to bitmap
  160. CBitmap bmp;
  161. bmp.Attach( hBitmap );
  162. // retrieve bitmap parameters
  163. BITMAPINFO bmi;
  164. ::ZeroMemory( &bmi, sizeof( bmi ) );
  165. bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  166. ::GetDIBits( m_CopyDC.GetSafeHdc(), hBitmap, 0, 0, NULL, &bmi, DIB_RGB_COLORS );
  167. // select temp bitmap into m_CopyDC
  168. CBitmap *pBitmapOld = m_CopyDC.SelectObject( &bmp );
  169. // copy from m_CopyDC to m_MemDC
  170. int nOldStretchMode = m_MemDC.SetStretchBltMode( COLORONCOLOR );
  171. m_MemDC.StretchBlt( 0, 0, m_MemDC.GetSize().cx, m_MemDC.GetSize().cy, &m_CopyDC,
  172. 0, 0, bmi.bmiHeader.biWidth, bmi.bmiHeader.biHeight, SRCCOPY );
  173. m_MemDC.SetStretchBltMode( nOldStretchMode );
  174. // restore selected bitmap in m_CopyDC
  175. m_CopyDC.SelectObject( pBitmapOld );
  176. // detach bitmap
  177. bmp.Detach();
  178. }
  179. else
  180. {
  181. // clear bitmap (filling with black)
  182. CRect rect( 0, 0, m_MemDC.GetSize().cx, m_MemDC.GetSize().cy );
  183. m_MemDC.FillSolidRect( &rect, RGB(0,0,0) );
  184. COLORREF nOldTextColor = m_MemDC.SetTextColor( RGB(0x7F,0x7F,0x7F) );
  185. m_MemDC.DrawText( CString( _T("No image") ), &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE );
  186. m_MemDC.SetTextColor( nOldTextColor );
  187. }
  188. if( GetSafeHwnd() )
  189. {
  190. InvalidateRect( NULL );
  191. RedrawWindow();
  192. }
  193. }
  194. void CImageWnd::OnLButtonDown(UINT nFlags, CPoint point)
  195. {
  196. GetParent()->SendMessage( WM_IMAGE_LBUTTONDOWN, nFlags, (LPARAM)&point );
  197. CWnd::OnLButtonDown(nFlags, point);
  198. }
  199. void CImageWnd::OnLButtonUp(UINT nFlags, CPoint point)
  200. {
  201. GetParent()->SendMessage( WM_IMAGE_LBUTTONUP, nFlags, (LPARAM)&point );
  202. CWnd::OnLButtonUp(nFlags, point);
  203. }
  204. void CImageWnd::OnMouseMove(UINT nFlags, CPoint point)
  205. {
  206. GetParent()->SendMessage( WM_IMAGE_MOUSEMOVE, nFlags, (LPARAM)&point );
  207. CWnd::OnMouseMove(nFlags, point);
  208. }