RationalCodabar.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // RationalCodabar.cpp: implementation of the CRationalCodabar class.
  2. //
  3. // Copyright 2002 Neil Van Eps
  4. //
  5. //////////////////////////////////////////////////////////////////////
  6. #include "stdafx.h"
  7. #include "RationalCodabar.h"
  8. //////////////////////////////////////////////////////////////////////
  9. // Construction/Destruction
  10. //////////////////////////////////////////////////////////////////////
  11. CRationalCodabar::CRationalCodabar()
  12. {
  13. // rational codabar
  14. m_nSymbology = RATIONALCODABAR;
  15. }
  16. CRationalCodabar::~CRationalCodabar()
  17. {
  18. }
  19. ////////////////////////////////////////////////////////////////////////////////////
  20. //
  21. // Name:
  22. // DrawBitmap()
  23. //
  24. // Description:
  25. // draws a barcode using the previously loaded data
  26. //
  27. // Arguments:
  28. // none
  29. //
  30. // Return:
  31. // void
  32. //
  33. // Called by:
  34. // public class interface
  35. //
  36. ////////////////////////////////////////////////////////////////////////////////////
  37. void CRationalCodabar::DrawBitmap()
  38. {
  39. int i;
  40. // draw each character in the message
  41. for (i=0;i<m_csMessage.GetLength();i++)
  42. DrawPattern(RetrievePattern(m_csMessage.GetAt(i)));
  43. return;
  44. }
  45. ////////////////////////////////////////////////////////////////////////////////////
  46. //
  47. // Name:
  48. // DrawPattern()
  49. //
  50. // Description:
  51. // draws the passed character pattern at the end of the barcode
  52. //
  53. // Arguments:
  54. // CString csPattern - the bar pattern to draw
  55. //
  56. // Return:
  57. // void
  58. //
  59. // Called by:
  60. // CRationalCodabar::DrawBitmap()
  61. //
  62. ////////////////////////////////////////////////////////////////////////////////////
  63. void CRationalCodabar::DrawPattern(CString csPattern)
  64. {
  65. int i,nXPixel,nYPixel,nTempWidth;
  66. CDC oDC;
  67. // attach to the device context
  68. oDC.Attach(m_hDC);
  69. // initialize X pixel value
  70. nXPixel = m_nStartingXPixel;
  71. for (i=0;i<csPattern.GetLength();i++)
  72. {
  73. // decide if narrow or wide bar
  74. if (csPattern.GetAt(i)=='n')
  75. nTempWidth = m_nNarrowBarPixelWidth;
  76. else
  77. nTempWidth = m_nWideBarPixelWidth;
  78. // X value for loop
  79. for (nXPixel=m_nStartingXPixel;nXPixel<m_nStartingXPixel+nTempWidth;nXPixel++)
  80. {
  81. // Y value for loop
  82. for (nYPixel=m_nStartingYPixel;nYPixel<m_nStartingYPixel+m_nPixelHeight;nYPixel++)
  83. {
  84. // if this is a bar
  85. if (i%2==0)
  86. oDC.SetPixelV(nXPixel,nYPixel,COLORBLACK);
  87. else
  88. oDC.SetPixelV(nXPixel,nYPixel,COLORWHITE);
  89. }
  90. }
  91. // advance the starting position
  92. m_nStartingXPixel+= nTempWidth;
  93. }
  94. // detach from the device context
  95. oDC.Detach();
  96. return;
  97. }
  98. ////////////////////////////////////////////////////////////////////////////////////
  99. //
  100. // Name:
  101. // RetrievePattern()
  102. //
  103. // Description:
  104. // retrieves the bar pattern for a given character
  105. //
  106. // Arguments:
  107. // char cInputCharacter - the input character to get the bar pattern for
  108. //
  109. // Return:
  110. // CString - the bar pattern for the input character
  111. //
  112. // Called by:
  113. // CRationalCodabar::DrawBitmap()
  114. //
  115. ////////////////////////////////////////////////////////////////////////////////////
  116. CString CRationalCodabar::RetrievePattern(char c)
  117. {
  118. CString csCharPattern;
  119. if (c == '0')
  120. csCharPattern = "nnnnnwwn";
  121. else if (c == '1')
  122. csCharPattern = "nnnnwwnn";
  123. else if (c == '2')
  124. csCharPattern = "nnnwnnwn";
  125. else if (c == '3')
  126. csCharPattern = "wwnnnnnn";
  127. else if (c == '4')
  128. csCharPattern = "nnwnnwnn";
  129. else if (c == '5')
  130. csCharPattern = "wnnnnwnn";
  131. else if (c == '6')
  132. csCharPattern = "nwnnnnwn";
  133. else if (c == '7')
  134. csCharPattern = "nwnnwnnn";
  135. else if (c == '8')
  136. csCharPattern = "nwwnnnnn";
  137. else if (c == '9')
  138. csCharPattern = "wnnwnnnn";
  139. else if (c == '-')
  140. csCharPattern = "nnnwwnnn";
  141. else if (c == '$')
  142. csCharPattern = "nnwwnnnn";
  143. else if (c == ':')
  144. csCharPattern = "wnnnwnwn";
  145. else if (c == '/')
  146. csCharPattern = "wnwnnnwn";
  147. else if (c == '.')
  148. csCharPattern = "wnwnwnnn";
  149. else if (c == '+')
  150. csCharPattern = "nnwnwnwn";
  151. else if (c == 'A')
  152. csCharPattern = "nnwwnwnn";
  153. else if (c == 'B')
  154. csCharPattern = "nwnwnnwn";
  155. else if (c == 'C')
  156. csCharPattern = "nnnwnwwn";
  157. else if (c == 'D')
  158. csCharPattern = "nnnwwwnn";
  159. return csCharPattern;
  160. }
  161. ////////////////////////////////////////////////////////////////////////////////////
  162. //
  163. // Name:
  164. // BitmapToClipboard()
  165. //
  166. // Description:
  167. // puts the specified bitmap on the clipboard
  168. //
  169. // Arguments:
  170. // none
  171. //
  172. // Return:
  173. // void
  174. //
  175. // Called by:
  176. // public class interface - called by users of the class
  177. //
  178. ////////////////////////////////////////////////////////////////////////////////////
  179. void CRationalCodabar::BitmapToClipboard()
  180. {
  181. CDC memDC;
  182. CBitmap oBitmap;
  183. memDC.CreateCompatibleDC(NULL);
  184. m_hDC = memDC.GetSafeHdc();
  185. // create compatible, correctly sized bitmap
  186. oBitmap.CreateCompatibleBitmap(&memDC,m_nFinalBarcodePixelWidth,m_nPixelHeight);
  187. // select our bitmap into the device context
  188. CBitmap * oldbm = memDC.SelectObject(&oBitmap);
  189. // turn area white - stock black bitmap is selected
  190. memDC.BitBlt(0,0,m_nFinalBarcodePixelWidth,m_nPixelHeight,NULL,0,0,WHITENESS);
  191. // draw bitmap into memory device context
  192. DrawBitmap();
  193. // put bitmap on clipboard
  194. ::OpenClipboard(NULL);
  195. ::EmptyClipboard();
  196. ::SetClipboardData(CF_BITMAP, oBitmap.m_hObject);
  197. ::CloseClipboard();
  198. // deselect object out of device context
  199. memDC.SelectObject(oldbm);
  200. // make sure bitmap not deleted with CBitmap object
  201. oBitmap.Detach();
  202. return;
  203. }