Key.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include "StdAfx.h"
  2. #include "Key.h"
  3. #include <cassert>
  4. using namespace std;
  5. // COLORREF Key::clrBord[] = {
  6. // RGB(255,255,255),
  7. // RGB(255, 0, 0)
  8. // };
  9. // COLORREF Key::clrFill[] = {
  10. // RGB( 0, 0, 0),
  11. // RGB(236,233,216)
  12. // };
  13. // COLORREF Key::clrText[] = {
  14. // RGB(255,255,255),
  15. // RGB( 0, 0, 0)
  16. // };
  17. COLORREF Key::clrBord[] = {
  18. RGB( 0, 0, 0),
  19. RGB(255, 0, 0)
  20. };
  21. COLORREF Key::clrFill[] = {
  22. RGB(236,233,216),
  23. RGB( 0, 0,255)
  24. };
  25. COLORREF Key::clrText[] = {
  26. RGB( 0, 0, 0),
  27. RGB(255,255,255)
  28. };
  29. void Key::draw( CDC* pDc ) const
  30. {
  31. int i = getState() >> 7;
  32. CBrush br;
  33. CPen pen;
  34. br.CreateSolidBrush(clrFill[i]);
  35. pen.CreatePen(PS_SOLID,2,clrBord[i]);
  36. pDc->SaveDC();
  37. pDc->SelectObject(&br);
  38. pDc->SelectObject(&pen);
  39. pDc->SetTextColor(clrText[i]);
  40. CString txt = getLabel();
  41. CRect rt = getRect();
  42. CSize sz = pDc->GetTextExtent(txt);
  43. pDc->BitBlt(rt.left,rt.top,rt.Width(),rt.Height(),pDc,rt.left,rt.top,SRCINVERT);
  44. pDc->Rectangle(rt);
  45. pDc->SetBkMode(TRANSPARENT);
  46. pDc->TextOut( (rt.right + rt.left - sz.cx) / 2,
  47. (rt.top + rt.bottom - sz.cy) / 2,
  48. txt );
  49. pDc->RestoreDC(-1);
  50. }
  51. SimpleKey::SimpleKey( LabelSetEx* _pls,const KeyConfig& _kc,int _unit,BYTE _state )
  52. {
  53. assert(_pls != NULL);
  54. pls = _pls;
  55. id = _kc.id;
  56. offset = _kc.offset;
  57. vk = _kc.vk;
  58. state = _state;
  59. rt.left = _kc.rt.left * _unit;
  60. rt.top = _kc.rt.top * _unit;
  61. rt.right = _kc.rt.right * _unit;
  62. rt.bottom = _kc.rt.bottom * _unit;
  63. }
  64. SimpleKey::SimpleKey()
  65. {
  66. pls = NULL;
  67. }
  68. bool SimpleKey::hitTest( const CPoint& pt ) const
  69. {
  70. return rt.PtInRect(pt) == TRUE;
  71. }
  72. BYTE SimpleKey::getState() const
  73. {
  74. return state;
  75. }
  76. BYTE SimpleKey::getVkey() const
  77. {
  78. return vk;
  79. }
  80. LPCTSTR SimpleKey::getLabel() const
  81. {
  82. return pls->getLable(id,offset);
  83. }
  84. const CRect& SimpleKey::getRect() const
  85. {
  86. return rt;
  87. }
  88. void SimpleKey::press()
  89. {
  90. keybd_event(vk,0,0,0);
  91. state ^= 0x81;
  92. }
  93. void SimpleKey::release()
  94. {
  95. keybd_event(vk,0,KEYEVENTF_KEYUP,0);
  96. state ^= 0x80;
  97. }
  98. SimpleKey::~SimpleKey()
  99. {
  100. }
  101. const LabelSetEx& SimpleKey::getLabelSet() const
  102. {
  103. return *pls;
  104. }
  105. KeyDecorator::KeyDecorator( Key* _pkey )
  106. : pkey(_pkey)
  107. {
  108. }
  109. bool KeyDecorator::hitTest( const CPoint& pt ) const
  110. {
  111. return pkey->hitTest(pt);
  112. }
  113. BYTE KeyDecorator::getState() const
  114. {
  115. return pkey->getState();
  116. }
  117. BYTE KeyDecorator::getVkey() const
  118. {
  119. return pkey->getVkey();
  120. }
  121. LPCTSTR KeyDecorator::getLabel() const
  122. {
  123. return pkey->getLabel();
  124. }
  125. const CRect& KeyDecorator::getRect() const
  126. {
  127. return pkey->getRect();
  128. }
  129. void KeyDecorator::press()
  130. {
  131. return pkey->press();
  132. }
  133. void KeyDecorator::release()
  134. {
  135. return pkey->release();
  136. }
  137. const LabelSetEx& KeyDecorator::getLabelSet() const
  138. {
  139. return pkey->getLabelSet();
  140. }
  141. ShiftKey::ShiftKey( Key* _pkey,short _id1,short _id2 )
  142. : KeyDecorator(_pkey)
  143. {
  144. id1 = _id1;
  145. id2 = _id2;
  146. }
  147. void ShiftKey::press()
  148. {
  149. KeyDecorator::press();
  150. LabelSetEx& ls = const_cast<LabelSetEx&>(getLabelSet());
  151. ls.turn(id1);
  152. ls.turn(id2);
  153. }
  154. void ShiftKey::release()
  155. {
  156. KeyDecorator::release();
  157. LabelSetEx& ls = const_cast<LabelSetEx&>(getLabelSet());
  158. ls.turn(id1);
  159. ls.turn(id2);
  160. }
  161. LockKey::LockKey( Key* _pkey,short _id )
  162. : KeyDecorator(_pkey)
  163. {
  164. id = _id;
  165. }
  166. void LockKey::press()
  167. {
  168. KeyDecorator::press();
  169. LabelSetEx& ls = const_cast<LabelSetEx&>(getLabelSet());
  170. ls.turn(id);
  171. }
  172. void LockKey::release()
  173. {
  174. KeyDecorator::release();
  175. }