Led.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Led.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "StoneU_HC_OCX.h"
  5. #include "Led.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CLed
  13. CLed::CLed()
  14. {
  15. m_LedBitmap.LoadBitmap(IDB_LEDS);
  16. m_nLedColor = LED_COLOR_RED;
  17. m_nLedMode = LED_OFF;
  18. m_nLedShape = LED_ROUND;
  19. }
  20. CLed::~CLed()
  21. {
  22. VERIFY(m_LedBitmap.DeleteObject());
  23. }
  24. BEGIN_MESSAGE_MAP(CLed, CStatic)
  25. //{{AFX_MSG_MAP(CLed)
  26. ON_WM_CREATE()
  27. ON_WM_ERASEBKGND()
  28. ON_WM_PAINT()
  29. ON_WM_TIMER()
  30. //}}AFX_MSG_MAP
  31. END_MESSAGE_MAP()
  32. /////////////////////////////////////////////////////////////////////////////
  33. // CLed message handlers
  34. int CLed::OnCreate(LPCREATESTRUCT lpCreateStruct)
  35. {
  36. if (CStatic::OnCreate(lpCreateStruct) == -1)
  37. return -1;
  38. // TODO: Add your specialized creation code here
  39. return 0;
  40. }
  41. BOOL CLed::OnEraseBkgnd(CDC* pDC)
  42. {
  43. // TODO: Add your message handler code here and/or call default
  44. return TRUE;
  45. }
  46. void CLed::OnPaint()
  47. {
  48. CPaintDC dc(this); // device context for painting
  49. DrawLed(&dc,m_nLedColor,m_nLedMode,m_nLedShape);
  50. // TODO: Add your message handler code here
  51. // Do not call CStatic::OnPaint() for painting messages
  52. }
  53. void CLed::OnTimer(UINT nIDEvent)
  54. {
  55. // TODO: Add your message handler code here and/or call default
  56. if(nIDEvent == PING_TIMER)
  57. {
  58. SetLed(m_nLedColor,CLed::LED_OFF,m_nLedShape);
  59. KillTimer(nIDEvent);
  60. m_bPingEnabled = FALSE;
  61. }
  62. CStatic::OnTimer(nIDEvent);
  63. }
  64. ///////////////////////////////////////////////////////////////////////////////
  65. // Name: SetLed
  66. // Description: This method will draw the LED to the specified DC.
  67. //
  68. // Entry:
  69. // CDC *pDC - DC to draw to
  70. //
  71. // int iLedColor - Where color is defined by:
  72. // LED_COLOR_RED
  73. // LED_COLOR_GREEN
  74. // LED_COLOR_YELLOW
  75. // LED_COLOR_BLUE
  76. //
  77. // int iMode - where mode is defined by:
  78. // LED_ON
  79. // LED_OFF
  80. // LED_DISABLED
  81. //
  82. // int iShape - where shape is defined by:
  83. // LED_ROUND
  84. // LED_SQUARE
  85. ///////////////////////////////////////////////////////////////////////////////
  86. void CLed::DrawLed(CDC *pDC,int nLEDColor, int nMode, int nShape)
  87. {
  88. return;//chn add
  89. CRect rect;
  90. GetClientRect(&rect);
  91. //
  92. // Center led within an oversized window
  93. //
  94. if(rect.Width() >= LED_SIZE && rect.Height() >= LED_SIZE)
  95. {
  96. int nWidth = rect.Width();
  97. int nHeight = rect.Height();
  98. rect.left += (nWidth - LED_SIZE)/2;
  99. rect.right -= (nWidth - LED_SIZE)/2;
  100. rect.top += (nHeight - LED_SIZE)/2;
  101. rect.bottom -= (nHeight - LED_SIZE)/2;
  102. }
  103. //
  104. // Prepare temporary DCs and bitmaps
  105. //
  106. CBitmap TransBitmap;
  107. TransBitmap.CreateBitmap(LED_SIZE,LED_SIZE,1,1,NULL);
  108. CBitmap bitmapTemp;
  109. CBitmap* pBitmap = &m_LedBitmap;
  110. CDC srcDC;
  111. CDC dcMask;
  112. CDC TempDC;
  113. TempDC.CreateCompatibleDC(pDC);
  114. srcDC.CreateCompatibleDC(pDC);
  115. dcMask.CreateCompatibleDC(pDC);
  116. CBitmap* pOldBitmap = srcDC.SelectObject(pBitmap);
  117. CBitmap* pOldMaskbitmap = dcMask.SelectObject(&TransBitmap);
  118. bitmapTemp.CreateCompatibleBitmap(pDC,LED_SIZE,LED_SIZE);
  119. //
  120. // Work with tempDC and bitmapTemp to reduce flickering
  121. //
  122. CBitmap *pOldBitmapTemp = TempDC.SelectObject(&bitmapTemp);
  123. TempDC.BitBlt(0, 0, LED_SIZE, LED_SIZE, pDC, rect.left, rect.top, SRCCOPY);
  124. //
  125. // Create mask
  126. //
  127. COLORREF OldBkColor = srcDC.SetBkColor(RGB(255,0,255));
  128. dcMask.BitBlt(0, 0, LED_SIZE, LED_SIZE,&srcDC, nMode*LED_SIZE, nLEDColor+nShape, SRCCOPY);
  129. TempDC.SetBkColor(OldBkColor);
  130. //
  131. // Using the IDB_LEDS bitmap, index into the bitmap for the appropriate
  132. // LED. By using the mask color (RGB(255,0,255)) a mask has been created
  133. // so the bitmap will appear transparent.
  134. //
  135. TempDC.BitBlt(0, 0, LED_SIZE, LED_SIZE, &srcDC, nMode*LED_SIZE, nLEDColor+nShape, SRCINVERT);
  136. TempDC.BitBlt(0, 0, LED_SIZE, LED_SIZE,&dcMask, 0, 0, SRCAND);
  137. TempDC.BitBlt(0, 0, LED_SIZE, LED_SIZE, &srcDC, nMode*LED_SIZE, nLEDColor+nShape, SRCINVERT);
  138. //
  139. // Since the actual minipulation is done to tempDC so there is minimal
  140. // flicker, it is now time to draw the result to the screen.
  141. //
  142. pDC->BitBlt(rect.left, rect.top, LED_SIZE, LED_SIZE, &TempDC, 0, 0, SRCCOPY);
  143. //
  144. // House cleaning
  145. //
  146. srcDC.SelectObject(pOldBitmap);
  147. dcMask.SelectObject(pOldMaskbitmap);
  148. TempDC.SelectObject(pOldBitmapTemp);
  149. VERIFY(srcDC.DeleteDC());
  150. VERIFY(dcMask.DeleteDC());
  151. VERIFY(TempDC.DeleteDC());
  152. VERIFY(TransBitmap.DeleteObject());
  153. VERIFY(bitmapTemp.DeleteObject());
  154. }
  155. ///////////////////////////////////////////////////////////////////////////////
  156. // Name: SetLed
  157. // Description: This method will draw and set led parameters.
  158. //
  159. // Entry: int iLedColor - Where color is defined by:
  160. // LED_COLOR_RED
  161. // LED_COLOR_GREEN
  162. // LED_COLOR_YELLOW
  163. // LED_COLOR_BLUE
  164. //
  165. // int iMode - where mode is defined by:
  166. // LED_ON
  167. // LED_OFF
  168. // LED_DISABLED
  169. //
  170. // int iShape - where shape is defined by:
  171. // LED_ROUND
  172. // LED_SQUARE
  173. ///////////////////////////////////////////////////////////////////////////////
  174. void CLed::SetLed(int nLedColor, int nMode, int nShape)
  175. {
  176. m_nLedColor = nLedColor;
  177. m_nLedMode = nMode;
  178. m_nLedShape = nShape;
  179. CDC *pDC;
  180. pDC = GetDC();
  181. DrawLed(pDC,m_nLedColor,m_nLedMode,m_nLedShape);
  182. ReleaseDC(pDC);
  183. }
  184. ///////////////////////////////////////////////////////////////////////////////
  185. // Name: Ping
  186. // Description: This method will turn the led on for dwTimeout milliseconds and
  187. // then turn it off.
  188. //
  189. // Entry: DWORD dwTimeout - Time out in milliseconds
  190. ///////////////////////////////////////////////////////////////////////////////
  191. void CLed::Ping(DWORD dwTimeout)
  192. {
  193. // Return if pinging
  194. if(m_bPingEnabled == TRUE)
  195. {
  196. KillTimer(PING_TIMER);
  197. }
  198. m_bPingEnabled = TRUE;
  199. SetLed(m_nLedColor,CLed::LED_ON,m_nLedShape);
  200. SetTimer(PING_TIMER,dwTimeout,NULL);
  201. }