2010-03-02 17:56:21 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
|
|
namespace Tanshu.Accounts.Print
|
|
|
|
|
{
|
2011-07-12 07:00:48 +00:00
|
|
|
|
internal class PrinterWindows : PlatformPrinter
|
2010-03-02 17:56:21 +00:00
|
|
|
|
{
|
2018-05-17 10:22:27 +00:00
|
|
|
|
private string location;
|
2010-03-02 17:56:21 +00:00
|
|
|
|
// Cut Code for the printer Alt-29, Alt-86, Alt-49
|
|
|
|
|
#region API Declarations
|
|
|
|
|
// Structure and API declarions:
|
|
|
|
|
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
|
|
|
|
|
internal class DOCINFOA
|
|
|
|
|
{
|
|
|
|
|
[MarshalAs(UnmanagedType.LPStr)]
|
|
|
|
|
public string pDocName;
|
|
|
|
|
[MarshalAs(UnmanagedType.LPStr)]
|
|
|
|
|
public string pOutputFile;
|
|
|
|
|
[MarshalAs(UnmanagedType.LPStr)]
|
|
|
|
|
public string pDataType;
|
|
|
|
|
}
|
2011-04-11 12:55:45 +00:00
|
|
|
|
[DllImport("winspool.drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
|
2010-03-02 17:56:21 +00:00
|
|
|
|
internal static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
|
|
|
|
|
|
2011-04-11 12:55:45 +00:00
|
|
|
|
[DllImport("winspool.drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
|
2010-03-02 17:56:21 +00:00
|
|
|
|
internal static extern bool ClosePrinter(IntPtr hPrinter);
|
|
|
|
|
|
2011-04-11 12:55:45 +00:00
|
|
|
|
[DllImport("winspool.drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
|
2010-03-02 17:56:21 +00:00
|
|
|
|
internal static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
|
|
|
|
|
|
2011-04-11 12:55:45 +00:00
|
|
|
|
[DllImport("winspool.drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
|
2010-03-02 17:56:21 +00:00
|
|
|
|
internal static extern bool EndDocPrinter(IntPtr hPrinter);
|
|
|
|
|
|
2011-04-11 12:55:45 +00:00
|
|
|
|
[DllImport("winspool.drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
|
2010-03-02 17:56:21 +00:00
|
|
|
|
internal static extern bool StartPagePrinter(IntPtr hPrinter);
|
|
|
|
|
|
2011-04-11 12:55:45 +00:00
|
|
|
|
[DllImport("winspool.drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
|
2010-03-02 17:56:21 +00:00
|
|
|
|
internal static extern bool EndPagePrinter(IntPtr hPrinter);
|
|
|
|
|
|
2011-04-11 12:55:45 +00:00
|
|
|
|
[DllImport("winspool.drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
|
2010-03-02 17:56:21 +00:00
|
|
|
|
internal static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);
|
|
|
|
|
#endregion
|
2018-05-17 10:22:27 +00:00
|
|
|
|
internal PrinterWindows (string location)
|
|
|
|
|
{
|
|
|
|
|
this.location = location.Substring(4).Normalize();
|
|
|
|
|
}
|
|
|
|
|
internal override bool Print(string documentName, string printingText)
|
2010-03-02 17:56:21 +00:00
|
|
|
|
{
|
|
|
|
|
IntPtr hPrinter = new IntPtr(0);
|
|
|
|
|
// The printer handle.
|
|
|
|
|
// Last error -incase there was trouble.
|
|
|
|
|
DOCINFOA di = new DOCINFOA();
|
|
|
|
|
// Describes your document(name, port, data type).
|
2011-07-12 07:00:48 +00:00
|
|
|
|
var bSuccess = false; // Your success code.
|
2010-03-02 17:56:21 +00:00
|
|
|
|
|
|
|
|
|
// Set up the DOCINFO structure.
|
|
|
|
|
di.pDocName = documentName;
|
|
|
|
|
di.pDataType = "RAW";
|
|
|
|
|
|
2018-05-17 10:22:27 +00:00
|
|
|
|
if (OpenPrinter(location, out hPrinter, IntPtr.Zero))
|
2010-03-02 17:56:21 +00:00
|
|
|
|
{
|
|
|
|
|
if (StartDocPrinter(hPrinter, 1, di))
|
|
|
|
|
{
|
2011-07-12 07:00:48 +00:00
|
|
|
|
if (!StartPagePrinter(hPrinter))
|
|
|
|
|
return false;
|
|
|
|
|
//Write your printer - specific bytes to the printer.
|
|
|
|
|
bSuccess = SendLine(printingText, hPrinter);
|
|
|
|
|
EndPagePrinter(hPrinter);
|
2010-03-02 17:56:21 +00:00
|
|
|
|
EndDocPrinter(hPrinter);
|
|
|
|
|
}
|
|
|
|
|
ClosePrinter(hPrinter);
|
|
|
|
|
}
|
|
|
|
|
// If you did not succeed, GetLastError may give more information about why not.
|
|
|
|
|
if (bSuccess == false)
|
|
|
|
|
{
|
2011-07-12 07:00:48 +00:00
|
|
|
|
Marshal.GetLastWin32Error();
|
2010-03-02 17:56:21 +00:00
|
|
|
|
}
|
|
|
|
|
return bSuccess;
|
|
|
|
|
}// SendBytesToPrinter()
|
2011-07-12 07:00:48 +00:00
|
|
|
|
|
2010-03-02 17:56:21 +00:00
|
|
|
|
internal static bool SendLine(string szString, IntPtr hPrinter)
|
|
|
|
|
{
|
|
|
|
|
Int32 dwWritten = 0;
|
|
|
|
|
Int32 dwCount = szString.Length; // How many characters are in the string?
|
|
|
|
|
|
|
|
|
|
IntPtr pBytes = Marshal.StringToCoTaskMemAnsi(szString);
|
|
|
|
|
// Assume that the printer is expecting ANSI text, and then convert
|
|
|
|
|
// the string to ANSI text.
|
|
|
|
|
|
|
|
|
|
// Write your bytes.
|
2011-07-12 07:00:48 +00:00
|
|
|
|
return WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
|
2010-03-02 17:56:21 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|