212 lines
9.2 KiB
C#
212 lines
9.2 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Text;
|
||
using System.IO;
|
||
using System.Drawing;
|
||
using System.Drawing.Printing;
|
||
using System.Windows.Forms;
|
||
using System.Runtime.InteropServices;
|
||
|
||
namespace CommonClassLib
|
||
{
|
||
public class RawPrinterHelper
|
||
{
|
||
// Structure and API declarions:
|
||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
|
||
public class DOCINFOA
|
||
{
|
||
[MarshalAs(UnmanagedType.LPStr)]
|
||
public string pDocName;
|
||
[MarshalAs(UnmanagedType.LPStr)]
|
||
public string pOutputFile;
|
||
[MarshalAs(UnmanagedType.LPStr)]
|
||
public string pDataType;
|
||
}
|
||
[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
|
||
public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
|
||
|
||
[DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
|
||
public static extern bool ClosePrinter(IntPtr hPrinter);
|
||
|
||
[DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
|
||
public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
|
||
|
||
[DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
|
||
public static extern bool EndDocPrinter(IntPtr hPrinter);
|
||
|
||
[DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
|
||
public static extern bool StartPagePrinter(IntPtr hPrinter);
|
||
|
||
[DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
|
||
public static extern bool EndPagePrinter(IntPtr hPrinter);
|
||
|
||
[DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
|
||
public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);
|
||
|
||
// SendBytesToPrinter()
|
||
// When the function is given a printer name and an unmanaged array
|
||
// of bytes, the function sends those bytes to the print queue.
|
||
// Returns true on success, false on failure.
|
||
//<2F><><EFBFBD><EFBFBD><EFBFBD>ֽڵ<D6BD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1>
|
||
public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
|
||
{
|
||
Int32 dwError = 0, dwWritten = 0;
|
||
IntPtr hPrinter = new IntPtr(0);
|
||
DOCINFOA di = new DOCINFOA();
|
||
bool bSuccess = false; // Assume failure unless you specifically succeed.
|
||
|
||
di.pDocName = "My C#.NET RAW Document";
|
||
di.pDataType = "RAW";
|
||
|
||
// Open the printer.
|
||
if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
|
||
{
|
||
// Start a document.
|
||
if (StartDocPrinter(hPrinter, 1, di))
|
||
{
|
||
// Start a page.
|
||
if (StartPagePrinter(hPrinter))
|
||
{
|
||
// Write your bytes.
|
||
bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
|
||
EndPagePrinter(hPrinter);
|
||
}
|
||
EndDocPrinter(hPrinter);
|
||
}
|
||
ClosePrinter(hPrinter);
|
||
}
|
||
// If you did not succeed, GetLastError may give more information
|
||
// about why not.
|
||
if (bSuccess == false)
|
||
{
|
||
dwError = Marshal.GetLastWin32Error();
|
||
}
|
||
return bSuccess;
|
||
}
|
||
//<2F><><EFBFBD><EFBFBD>prn<72>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1>
|
||
public static bool SendFileToPrinter(string szPrinterName, string szFileName)
|
||
{
|
||
// Open the file.
|
||
FileStream fs = new FileStream(szFileName, FileMode.Open);
|
||
// Create a BinaryReader on the file.
|
||
BinaryReader br = new BinaryReader(fs);
|
||
// Dim an array of bytes big enough to hold the file's contents.
|
||
Byte[] bytes = new Byte[fs.Length];
|
||
bool bSuccess = false;
|
||
// Your unmanaged pointer.
|
||
IntPtr pUnmanagedBytes = new IntPtr(0);
|
||
int nLength;
|
||
|
||
nLength = Convert.ToInt32(fs.Length);
|
||
// Read the contents of the file into the array.
|
||
bytes = br.ReadBytes(nLength);
|
||
// Allocate some unmanaged memory for those bytes.
|
||
pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
|
||
// Copy the managed byte array into the unmanaged array.
|
||
Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
|
||
// Send the unmanaged bytes to the printer.
|
||
bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
|
||
// Free the unmanaged memory that you allocated earlier.
|
||
Marshal.FreeCoTaskMem(pUnmanagedBytes);
|
||
return bSuccess;
|
||
}
|
||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>뵽<EFBFBD><EBB5BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1>
|
||
public static bool SendStringToPrinter(string szPrinterName, string szString)
|
||
{
|
||
IntPtr pBytes;
|
||
Int32 dwCount;
|
||
// How many characters are in the string?
|
||
dwCount = szString.Length;
|
||
// Assume that the printer is expecting ANSI text, and then convert
|
||
// the string to ANSI text.
|
||
pBytes = Marshal.StringToCoTaskMemAnsi(szString);
|
||
// Send the converted ANSI string to the printer.
|
||
SendBytesToPrinter(szPrinterName, pBytes, dwCount);
|
||
Marshal.FreeCoTaskMem(pBytes);
|
||
return true;
|
||
}
|
||
|
||
|
||
public static void startPrint()
|
||
{
|
||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1>ZLPָ<50>ʼ
|
||
StringBuilder ZPL = new StringBuilder("");
|
||
ZPL.Append("\r\n");
|
||
//<2F><><EFBFBD><EFBFBD>
|
||
Font f0 = new Font("<22><><EFBFBD><EFBFBD>", 50, FontStyle.Bold);
|
||
Bitmap imgA = BarcodeUtils.CreateImage("<22><><EFBFBD><EFBFBD>ʡ<EFBFBD><CAA1><EFBFBD>繫˾<E7B9AB><CBBE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", f0);
|
||
ZPL.Append(BarcodeUtils.CreateBmpZLP(imgA, BarcodeUtils.ConvertImageToCode(imgA), "100"));
|
||
//caption1
|
||
Font f1 = new Font("<22><><EFBFBD><EFBFBD>", 30, FontStyle.Bold);
|
||
Bitmap img1 = BarcodeUtils.CreateImage("<22><> <20><> <20>룺", f1);
|
||
ZPL.Append(BarcodeUtils.CreateBmpZLP(img1, BarcodeUtils.ConvertImageToCode(img1), "101"));
|
||
//caption2
|
||
Font f2 = new Font("<22><><EFBFBD><EFBFBD>", 30, FontStyle.Bold);
|
||
Bitmap img2 = BarcodeUtils.CreateImage("<22>ּ<EFBFBD><D6BC><EFBFBD><EFBFBD>ۣ<EFBFBD>", f2);
|
||
ZPL.Append(BarcodeUtils.CreateBmpZLP(img2, BarcodeUtils.ConvertImageToCode(img2), "102"));
|
||
//caption3
|
||
Font f3 = new Font("<22><><EFBFBD><EFBFBD>", 30, FontStyle.Bold);
|
||
Bitmap img3 = BarcodeUtils.CreateImage("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>", f3);
|
||
ZPL.Append(BarcodeUtils.CreateBmpZLP(img3, BarcodeUtils.ConvertImageToCode(img3), "103"));
|
||
|
||
//data1
|
||
Font df1 = new Font("<22><><EFBFBD><EFBFBD>", 30, FontStyle.Underline);
|
||
Bitmap dimg1 = BarcodeUtils.CreateImage("barcode", df1);
|
||
ZPL.Append(BarcodeUtils.CreateBmpZLP(dimg1, BarcodeUtils.ConvertImageToCode(dimg1), "201"));
|
||
//data2
|
||
Bitmap dimg2 = BarcodeUtils.CreateImage("sort_conclusion", df1);
|
||
ZPL.Append(BarcodeUtils.CreateBmpZLP(dimg2, BarcodeUtils.ConvertImageToCode(dimg2), "202"));
|
||
//data3
|
||
|
||
List<int> imgIDS = new List<int>();
|
||
|
||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
ZPL.Append("^XA");
|
||
ZPL.Append("\r\n");
|
||
ZPL.Append("^MMP");
|
||
ZPL.Append("\r\n");
|
||
ZPL.Append("^POI");
|
||
ZPL.Append("\r\n");
|
||
//<2F><>ʼ<EFBFBD><CABC>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD>
|
||
//<2F><>һ<EFBFBD><D2BB>
|
||
ZPL.Append("^FT30,30^XG100.GRF,1,1^FS");
|
||
ZPL.Append("\r\n");
|
||
ZPL.Append("^FT30,140^XG101.GRF,1,1^FS");
|
||
ZPL.Append("\r\n");
|
||
ZPL.Append("^FT30,190^XG102.GRF,1,1^FS");
|
||
ZPL.Append("\r\n");
|
||
ZPL.Append("^FT30,240^XG103.GRF,1,1^FS");
|
||
ZPL.Append("\r\n");
|
||
ZPL.Append("^FT240,140^XG201.GRF,1,1^FS");
|
||
ZPL.Append("\r\n");
|
||
ZPL.Append("^FT240,190^XG202.GRF,1,1^FS");
|
||
ZPL.Append("\r\n");
|
||
|
||
|
||
//<2F>ͷ<EFBFBD><CDB7><EFBFBD>Դ
|
||
ZPL.Append("^PQ1,0,1,Y^XZ");
|
||
ZPL.Append("\r\n");
|
||
|
||
ZPL.Append("^XA^ID100.GRF^FS^XZ");
|
||
ZPL.Append("\r\n");
|
||
ZPL.Append("^XA^ID101.GRF^FS^XZ");
|
||
ZPL.Append("\r\n");
|
||
ZPL.Append("^XA^ID102.GRF^FS^XZ");
|
||
ZPL.Append("\r\n");
|
||
ZPL.Append("^XA^ID103.GRF^FS^XZ");
|
||
ZPL.Append("\r\n");
|
||
ZPL.Append("^XA^ID201.GRF^FS^XZ");
|
||
ZPL.Append("\r\n");
|
||
ZPL.Append("^XA^ID202.GRF^FS^XZ");
|
||
ZPL.Append("\r\n");
|
||
|
||
|
||
|
||
string printer = "ZDesigner ZT230-200dpi ZPL";
|
||
bool isPrintOK = RawPrinterHelper.SendStringToPrinter(printer, ZPL.ToString());
|
||
|
||
}
|
||
|
||
}
|
||
}
|