Code39.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // Code39.cpp: implementation of the CCode39 class.
  2. //
  3. // Copyright 2002 Neil Van Eps
  4. //
  5. //////////////////////////////////////////////////////////////////////
  6. #include "stdafx.h"
  7. #include "Code39.h"
  8. //////////////////////////////////////////////////////////////////////
  9. // Construction/Destruction
  10. //////////////////////////////////////////////////////////////////////
  11. CCode39::CCode39()
  12. {
  13. // code 39
  14. m_nSymbology = CODE39;
  15. }
  16. CCode39::~CCode39()
  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 CCode39::DrawBitmap()
  38. {
  39. int i;
  40. CString csCurrentPattern;
  41. // draw start character, an asterisk
  42. DrawPattern(RetrievePattern('*'));
  43. // draw each character in the message
  44. for (i=0;i<m_csMessage.GetLength();i++)
  45. DrawPattern(RetrievePattern(m_csMessage.GetAt(i)));
  46. // draw stop character, also an asterisk
  47. DrawPattern(RetrievePattern('*'));
  48. return;
  49. }
  50. ////////////////////////////////////////////////////////////////////////////////////
  51. //
  52. // Name:
  53. // DrawPattern()
  54. //
  55. // Description:
  56. // draws the passed character pattern at the end of the barcode
  57. //
  58. // Arguments:
  59. // CString csPattern - the bar pattern to draw
  60. //
  61. // Return:
  62. // void
  63. //
  64. // Called by:
  65. // CRationalCodabar::DrawBitmap()
  66. //
  67. ////////////////////////////////////////////////////////////////////////////////////
  68. void CCode39::DrawPattern( CString csPattern )
  69. {
  70. int i,nXPixel,nYPixel,nTempWidth;
  71. CDC oDC;
  72. // attach to the device context
  73. oDC.Attach(m_hDC);
  74. // initialize X pixel value
  75. nXPixel = m_nStartingXPixel;
  76. for (i=0;i<csPattern.GetLength();i++)
  77. {
  78. // decide if narrow or wide bar
  79. if (csPattern.GetAt(i)=='n')
  80. nTempWidth = m_nNarrowBarPixelWidth;
  81. else
  82. nTempWidth = m_nWideBarPixelWidth;
  83. // X value for loop
  84. for (nXPixel=m_nStartingXPixel;nXPixel<m_nStartingXPixel+nTempWidth;nXPixel++)
  85. {
  86. // Y value for loop
  87. for (nYPixel=m_nStartingYPixel;nYPixel<m_nStartingYPixel+m_nPixelHeight;nYPixel++)
  88. {
  89. // if this is a bar
  90. if (i%2==0)
  91. oDC.SetPixelV(nXPixel,nYPixel,COLORBLACK);
  92. else
  93. oDC.SetPixelV(nXPixel,nYPixel,COLORWHITE);
  94. }
  95. }
  96. // advance the starting position
  97. m_nStartingXPixel+= nTempWidth;
  98. }
  99. // detach from the device context
  100. oDC.Detach();
  101. return;
  102. }
  103. ////////////////////////////////////////////////////////////////////////////////////
  104. //
  105. // Name:
  106. // RetrievePattern()
  107. //
  108. // Description:
  109. // retrieves the bar pattern for a given character
  110. //
  111. // Arguments:
  112. // char cInputCharacter - the input character to get the bar pattern for
  113. //
  114. // Return:
  115. // CString - the bar pattern for the input character
  116. //
  117. // Called by:
  118. // CRationalCodabar::DrawBitmap()
  119. //
  120. ////////////////////////////////////////////////////////////////////////////////////
  121. CString CCode39::RetrievePattern(char c)
  122. {
  123. CString csCharPattern;
  124. switch (c)
  125. {
  126. case '1':
  127. csCharPattern = "wnnwnnnnwn";
  128. break;
  129. case '2':
  130. csCharPattern = "nnwwnnnnwn";
  131. break;
  132. case '3':
  133. csCharPattern = "wnwwnnnnnn";
  134. break;
  135. case '4':
  136. csCharPattern = "nnnwwnnnwn";
  137. break;
  138. case '5':
  139. csCharPattern = "wnnwwnnnnn";
  140. break;
  141. case '6':
  142. csCharPattern = "nnwwwnnnnn";
  143. break;
  144. case '7':
  145. csCharPattern = "nnnwnnwnwn";
  146. break;
  147. case '8':
  148. csCharPattern = "wnnwnnwnnn";
  149. break;
  150. case '9':
  151. csCharPattern = "nnwwnnwnnn";
  152. break;
  153. case '0':
  154. csCharPattern = "nnnwwnwnnn";
  155. break;
  156. case 'A':
  157. csCharPattern = "wnnnnwnnwn";
  158. break;
  159. case 'B':
  160. csCharPattern = "nnwnnwnnwn";
  161. break;
  162. case 'C':
  163. csCharPattern = "wnwnnwnnnn";
  164. break;
  165. case 'D':
  166. csCharPattern = "nnnnwwnnwn";
  167. break;
  168. case 'E':
  169. csCharPattern = "wnnnwwnnnn";
  170. break;
  171. case 'F':
  172. csCharPattern = "nnwnwwnnnn";
  173. break;
  174. case 'G':
  175. csCharPattern = "nnnnnwwnwn";
  176. break;
  177. case 'H':
  178. csCharPattern = "wnnnnwwnnn";
  179. break;
  180. case 'I':
  181. csCharPattern = "nnwnnwwnnn";
  182. break;
  183. case 'J':
  184. csCharPattern = "nnnnwwwnnn";
  185. break;
  186. case 'K':
  187. csCharPattern = "wnnnnnnwwn";
  188. break;
  189. case 'L':
  190. csCharPattern = "nnwnnnnwwn";
  191. break;
  192. case 'M':
  193. csCharPattern = "wnwnnnnwnn";
  194. break;
  195. case 'N':
  196. csCharPattern = "nnnnwnnwwn";
  197. break;
  198. case 'O':
  199. csCharPattern = "wnnnwnnwnn";
  200. break;
  201. case 'P':
  202. csCharPattern = "nnwnwnnwnn";
  203. break;
  204. case 'Q':
  205. csCharPattern = "nnnnnnwwwn";
  206. break;
  207. case 'R':
  208. csCharPattern = "wnnnnnwwnn";
  209. break;
  210. case 'S':
  211. csCharPattern = "nnwnnnwwnn";
  212. break;
  213. case 'T':
  214. csCharPattern = "nnnnwnwwnn";
  215. break;
  216. case 'U':
  217. csCharPattern = "wwnnnnnnwn";
  218. break;
  219. case 'V':
  220. csCharPattern = "nwwnnnnnwn";
  221. break;
  222. case 'W':
  223. csCharPattern = "wwwnnnnnnn";
  224. break;
  225. case 'X':
  226. csCharPattern = "nwnnwnnnwn";
  227. break;
  228. case 'Y':
  229. csCharPattern = "wwnnwnnnnn";
  230. break;
  231. case 'Z':
  232. csCharPattern = "nwwnwnnnnn";
  233. break;
  234. case '-':
  235. csCharPattern = "nwnnnnwnwn";
  236. break;
  237. case '.':
  238. csCharPattern = "wwnnnnwnnn";
  239. break;
  240. case ' ':
  241. csCharPattern = "nwwnnnwnnn";
  242. break;
  243. case '*':
  244. csCharPattern = "nwnnwnwnnn";
  245. break;
  246. case '$':
  247. csCharPattern = "nwnwnwnnnn";
  248. break;
  249. case '/':
  250. csCharPattern = "nwnwnnnwnn";
  251. break;
  252. case '+':
  253. csCharPattern = "nwnnnwnwnn";
  254. break;
  255. case '%':
  256. csCharPattern = "nnnwnwnwnn";
  257. break;
  258. }
  259. return csCharPattern;
  260. }
  261. ////////////////////////////////////////////////////////////////////////////////////
  262. //
  263. // Name:
  264. // BitmapToClipboard()
  265. //
  266. // Description:
  267. // puts the specified bitmap on the clipboard
  268. //
  269. // Arguments:
  270. // none
  271. //
  272. // Return:
  273. // void
  274. //
  275. // Called by:
  276. // public class interface - called by users of the class
  277. //
  278. ////////////////////////////////////////////////////////////////////////////////////
  279. void CCode39::BitmapToClipboard()
  280. {
  281. CDC memDC;
  282. CBitmap oBitmap;
  283. memDC.CreateCompatibleDC(NULL);
  284. m_hDC = memDC.GetSafeHdc();
  285. // create compatible, correctly sized bitmap
  286. oBitmap.CreateCompatibleBitmap(&memDC,m_nFinalBarcodePixelWidth,m_nPixelHeight);
  287. // select our bitmap into the device context
  288. CBitmap * oldbm = memDC.SelectObject(&oBitmap);
  289. // turn area white - stock black bitmap is selected
  290. memDC.BitBlt(0,0,m_nFinalBarcodePixelWidth,m_nPixelHeight,NULL,0,0,WHITENESS);
  291. // draw bitmap into memory device context
  292. DrawBitmap();
  293. // put bitmap on clipboard
  294. ::OpenClipboard(NULL);
  295. ::EmptyClipboard();
  296. ::SetClipboardData(CF_BITMAP, oBitmap.m_hObject);
  297. ::CloseClipboard();
  298. // deselect object out of device context
  299. memDC.SelectObject(oldbm);
  300. // make sure bitmap not deleted with CBitmap object
  301. oBitmap.Detach();
  302. return;
  303. }