C # An example of bar code printing

Source: Internet
Author: User

C # the bar code is usually printed using commands or images. The format of the picture is fastreport, but vs is not found to call it. It is only successfully used in Delphi 7. In fact, the vast majority of bar code printer manufacturers have their own printing command language. With this language, they can directly print without a driver, and the operation is very simple, just send the command to the printer.

VS has a comport control, but there is no ready-made LPT port control. Compared with the comport, The LPT speed is faster. Therefore, when printing, the customer generally chooses the LPT communication mode, after some consultation on the Internet, the LPT port printing is finally realized, and the printer is Zebra, which is written to share with you.

C # example of bar code printing:

 
 
  1. UsingSystem;
  2. UsingSystem. Collections. Generic;
  3. UsingSystem. componentmodel;
  4. UsingSystem. Data;
  5. UsingSystem. drawing;
  6. UsingSystem. text;
  7. UsingSystem. Windows. forms;
  8. // C # instance for bar code printing
  9. NamespacePrintdemo
  10. {
  11. PublicPartialClassForm1: Form
  12. {
  13. PublicForm1 ()
  14. {
  15. Initializecomponent ();
  16. }
  17.  
  18. Private VoidForm1_load (ObjectSender, eventargs E)
  19. {
  20. Tbbarcode. Focus ();
  21. }
  22. // C # instance for bar code printing
  23. Private VoidTbbarcode_keydown (ObjectSender,
  24.  
  25. Keyeventargs E)
  26. {
  27. Switch(E. keycode)
  28. {
  29. CaseKeys. Enter:
  30. Printbarcode (tbbarcode. Text. Trim ());
  31. Tbbarcode. Text = "";
  32. Tbbarcode. Focus ();
  33. Break;
  34. Default:
  35. Break;
  36. }
  37. }
  38. Private VoidPrintbarcode (StringBarcode)
  39. {
  40. Barcode = "^ XA ^ fo48, 12 ^ by4 ^ BCN, 152, N, N ^ FD>;" +
  41. // C # instance for bar code printing
  42. Barcode. Trim () + "^ FS ^ ft62, 220 ^ ci0 ^ ABN, 44,28 ^ FD" +
  43.  
  44. Barcode. Trim () + "^ FS ^ pq1, 0, 1, y ^ xz ";
  45. Printdemo. posprinter PRN =New
  46.  
  47. Printdemo. posprinter ("LPT1 ");
  48. StringStrmsg = PRN. printline (barcode );
  49. If(Strmsg! = "")
  50. {
  51. MessageBox. Show (strmsg );
  52. }
  53. }
  54. }
  55. }

C # define posprinter for bar code printing as follows

 
 
  1. NamespacePrintdemo
  2. {
  3. ClassPosprinter
  4. {
  5. Const IntOpen_existing = 3;
  6. StringPrnport = "LPT1 ";
  7. [Dllimport ("kernel32.dll", charset = charset. Auto)]
  8. Private Static ExternIntptr createfile (String
  9.  
  10. Lpfilename,
  11. IntDwdesiredaccess,
  12. IntDww.mode,
  13. IntLpsecurityattributes,
  14. IntDwcreationdisposition,
  15. IntDwflagsandattributes,
  16. IntHtemplatefile );
  17.  
  18. PublicPosprinter ()
  19. {
  20. //
  21. // Todo: add the constructor logic here
  22. //
  23. }
  24.  
  25. PublicPosprinter (StringPrnport)
  26. {
  27. This. Prnport = prnport; // printer port
  28. }
  29.  
  30. Public StringPrintline (StringStr)
  31. {
  32.  
  33. Intptr ihandle = createfile (prnport, 0x40000000,
  34.  
  35. 0, 0, open_existing, 0, 0 );
  36. If(Ihandle. toint32 () =-1)
  37. {
  38. Return"LPT1 port open failed ";
  39. }
  40. Else
  41. {
  42.  
  43. Filestream FS =NewFilestream (ihandle,
  44.  
  45. Fileaccess. readwrite );
  46. Streamwriter Sw =NewStreamwriter (FS,
  47.  
  48. System. Text. encoding. Default); // C # print the written data of the bar code operation
  49. Sw. writeline (STR );
  50. Sw. Close ();
  51. FS. Close ();
  52. Return"";
  53. }
  54. }
  55. }
  56. }

I will introduce you to the example of C # print bar code operation. I hope you can understand and learn about C # print bar code operation.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.