100分求C#逐行打印代码!!!!!!!!!!!!

trytobegood 2006-11-09 12:45:29
求C#逐行打印代码,实现逐行不定长打印.打印机是EPSON K3...在线等,急..............谢谢!!!!
...全文
262 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
trytobegood 2006-11-10
  • 打赏
  • 举报
回复
using System;
using System.IO;
using System.Drawing.Printing;
using System.Runtime.InteropServices;


public class RawPrinterHelper
{
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
public struct DOCINFOW
{
[MarshalAs(UnmanagedType.LPWStr)]
public string pDocName;
[MarshalAs(UnmanagedType.LPWStr)]
public string pOutputFile;
[MarshalAs(UnmanagedType.LPWStr)]
public string pDataType;
}

[DllImport("winspool.Drv", EntryPoint="OpenPrinterW", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode, SetLastError=true, ExactSpelling=true)]
public static extern bool OpenPrinter(string src, ref IntPtr hPrinter, long pd);



[DllImport("winspool.Drv", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode, SetLastError=true, ExactSpelling=true)]
public static extern bool ClosePrinter(IntPtr hPrinter);



[DllImport("winspool.Drv", EntryPoint="StartDocPrinterW", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode, SetLastError=true, ExactSpelling=true)]
public static extern bool StartDocPrinter(IntPtr hPrinter, int level, ref RawPrinterHelper.DOCINFOW pDI);



[DllImport("winspool.Drv", EntryPoint="EndDocPrinter", SetLastError=true, CharSet=CharSet.Unicode, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
public static extern bool EndDocPrinter(IntPtr hPrinter);


[DllImport("winspool.Drv", EntryPoint="StartPagePrinter", SetLastError=true, CharSet=CharSet.Unicode, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
public static extern bool StartPagePrinter(IntPtr hPrinter) ;


[DllImport("winspool.Drv", EntryPoint="EndPagePrinter", SetLastError=true, CharSet=CharSet.Unicode, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
public static extern bool EndPagePrinter(IntPtr hPrinter) ;


[DllImport("winspool.Drv", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode, SetLastError=true, ExactSpelling=true)]
public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, int dwCount, ref int dwWritten);



public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
{
IntPtr hPrinter = System.IntPtr.Zero;
Int32 dwError;
DOCINFOW di = new DOCINFOW();
Int32 dwWritten = 0;
bool bSuccess;
di.pDocName = "My Document";
di.pDataType = "RAW";
bSuccess = false;
if (OpenPrinter(szPrinterName,ref hPrinter, 0))
{
if (StartDocPrinter(hPrinter, 1,ref di))
{
if (StartPagePrinter(hPrinter))
{
bSuccess = WritePrinter(hPrinter, pBytes, dwCount, ref dwWritten);
EndPagePrinter(hPrinter);
}
EndDocPrinter(hPrinter);
}
ClosePrinter(hPrinter);
}
if (bSuccess == false)
{
dwError = Marshal.GetLastWin32Error();
}
return bSuccess;

}


public static bool SendFileToPrinter(string szPrinterName, string szFileName)
{
FileStream stream1 = new FileStream(szFileName, FileMode.Open);
BinaryReader reader1 = new BinaryReader(stream1);
byte[] buffer1 = new byte[((int) stream1.Length) + 1];
buffer1 = reader1.ReadBytes((int) stream1.Length);
IntPtr ptr1 = Marshal.AllocCoTaskMem((int) stream1.Length);
Marshal.Copy(buffer1, 0, ptr1, (int) stream1.Length);
bool flag1 = RawPrinterHelper.SendBytesToPrinter(szPrinterName, ptr1, (int) stream1.Length);
Marshal.FreeCoTaskMem(ptr1);
return flag1;

}

public static void SendStringToPrinter(string szPrinterName, string szString)
{
IntPtr pBytes;
Int32 dwCount;
dwCount = szString.Length;
pBytes = Marshal.StringToCoTaskMemAnsi(szString);
SendBytesToPrinter(szPrinterName, pBytes, dwCount);
Marshal.FreeCoTaskMem(pBytes);
}
}
jedliu 2006-11-09
  • 打赏
  • 举报
回复
哦,了解了!
那你可以找一个逐行打印接口套件,调用一下就可以了。一般这样的控件都是直接往打印机端口写数据。
trytobegood 2006-11-09
  • 打赏
  • 举报
回复
TO:jedliu(21世纪什么最贵? 人才!)
比如POS系统啦.服装工厂的条形码打印啦
trytobegood 2006-11-09
  • 打赏
  • 举报
回复
TO:lytone52(给狼伴舞)
我先测试,行了就给你分.非常感谢!!!!!!
jedliu 2006-11-09
  • 打赏
  • 举报
回复
自带的不行啊,自带的会分页的
----------------
你用什么东西打印它不会分页?
trytobegood 2006-11-09
  • 打赏
  • 举报
回复
自带的不行啊,自带的会分页的
lizhizhe2000 2006-11-09
  • 打赏
  • 举报
回复
用它自带的控件不能满足需求吗?
lytone52 2006-11-09
  • 打赏
  • 举报
回复
http://www.cnblogs.com/zhanghl/archive/2006/07/11/232798.html
Qim 2006-11-09
  • 打赏
  • 举报
回复
up + study

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧