C# 逐行驱动打印

windows打印以page方式驱动打印机,行方式驱动需调用API或用种变通的方式。

方法一

简单的写到一个文件里然后

System.Diagnostics.Process.Start("cmd"," /c copy d:\1.txt  prn")

输出重定向简单易行打个receipt啥的足够用,就是感觉很业余,呵呵。

方法二
这个就要用几个API 了

Class  RawPrinterHelper

None.gif using  System;
None.gif
using  System.IO; 
None.gif
using  System.Drawing.Printing; 
None.gif
using  System.Runtime.InteropServices; 
None.gif
None.gif
None.gif
public   class  RawPrinterHelper 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif    [StructLayout(LayoutKind.Sequential, CharSet
=CharSet.Unicode)] 
InBlock.gif        
public struct DOCINFOW 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif        [MarshalAs(UnmanagedType.LPWStr)] 
InBlock.gif        
public string pDocName; 
InBlock.gif        [MarshalAs(UnmanagedType.LPWStr)] 
InBlock.gif        
public string pOutputFile; 
InBlock.gif        [MarshalAs(UnmanagedType.LPWStr)] 
InBlock.gif        
public string pDataType; 
ExpandedSubBlockEnd.gif    }
 
InBlock.gif
InBlock.gif    [DllImport(
"winspool.Drv", EntryPoint="OpenPrinterW", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode, SetLastError=true, ExactSpelling=true)]
InBlock.gif    
public static extern bool OpenPrinter(string src, ref IntPtr hPrinter, long pd);
InBlock.gif 
InBlock.gif
InBlock.gif
InBlock.gif    [DllImport(
"winspool.Drv", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode, SetLastError=true, ExactSpelling=true)]
InBlock.gif    
public static extern bool ClosePrinter(IntPtr hPrinter);
InBlock.gif 
InBlock.gif
InBlock.gif
InBlock.gif    [DllImport(
"winspool.Drv", EntryPoint="StartDocPrinterW", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode, SetLastError=true, ExactSpelling=true)]
InBlock.gif    
public static extern bool StartDocPrinter(IntPtr hPrinter, int level, ref RawPrinterHelper.DOCINFOW pDI);
InBlock.gif 
InBlock.gif
InBlock.gif
InBlock.gif    [DllImport(
"winspool.Drv", EntryPoint="EndDocPrinter", SetLastError=true, CharSet=CharSet.Unicode, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)] 
InBlock.gif    
public static extern bool EndDocPrinter(IntPtr hPrinter); 
InBlock.gif    
InBlock.gif
InBlock.gif    [DllImport(
"winspool.Drv", EntryPoint="StartPagePrinter", SetLastError=true, CharSet=CharSet.Unicode, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)] 
InBlock.gif    
public static extern bool StartPagePrinter(IntPtr hPrinter) ;
InBlock.gif    
InBlock.gif
InBlock.gif    [DllImport(
"winspool.Drv", EntryPoint="EndPagePrinter", SetLastError=true, CharSet=CharSet.Unicode, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)] 
InBlock.gif    
public static extern bool EndPagePrinter(IntPtr hPrinter) ;
InBlock.gif    
InBlock.gif
InBlock.gif    [DllImport(
"winspool.Drv", CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Unicode, SetLastError=true, ExactSpelling=true)]
InBlock.gif    
public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, int dwCount, ref int dwWritten);
InBlock.gif 
InBlock.gif
InBlock.gif
InBlock.gif    
public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount) 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif        IntPtr hPrinter 
=  System.IntPtr.Zero; 
InBlock.gif        Int32 dwError; 
InBlock.gif        DOCINFOW di 
= new DOCINFOW(); 
InBlock.gif        Int32 dwWritten 
= 0
InBlock.gif        
bool bSuccess; 
InBlock.gif        di.pDocName 
= "My Document"
InBlock.gif        di.pDataType 
= "RAW"
InBlock.gif        bSuccess 
= false
InBlock.gif        
if (OpenPrinter(szPrinterName,ref hPrinter, 0)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            
if (StartDocPrinter(hPrinter, 1,ref di)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                
if (StartPagePrinter(hPrinter)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif
InBlock.gif                    bSuccess 
= WritePrinter(hPrinter, pBytes, dwCount, ref dwWritten); 
InBlock.gif                    EndPagePrinter(hPrinter); 
ExpandedSubBlockEnd.gif                }
 
InBlock.gif                EndDocPrinter(hPrinter); 
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            ClosePrinter(hPrinter); 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif        
if (bSuccess == false
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif
InBlock.gif            dwError 
= Marshal.GetLastWin32Error(); 
ExpandedSubBlockEnd.gif        }
 
InBlock.gif        
return bSuccess; 
InBlock.gif
ExpandedSubBlockEnd.gif    }
 
InBlock.gif
InBlock.gif
InBlock.gif    
public static bool SendFileToPrinter(string szPrinterName, string szFileName) 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif        FileStream stream1 
= new FileStream(szFileName, FileMode.Open);
InBlock.gif        BinaryReader reader1 
= new BinaryReader(stream1);
InBlock.gif        
byte[] buffer1 = new byte[((int) stream1.Length) + 1];
InBlock.gif        buffer1 
= reader1.ReadBytes((int) stream1.Length);
InBlock.gif        IntPtr ptr1 
= Marshal.AllocCoTaskMem((int) stream1.Length);
InBlock.gif        Marshal.Copy(buffer1, 
0, ptr1, (int) stream1.Length);
InBlock.gif        
bool flag1 = RawPrinterHelper.SendBytesToPrinter(szPrinterName, ptr1, (int) stream1.Length);
InBlock.gif        Marshal.FreeCoTaskMem(ptr1);
InBlock.gif        
return flag1;
InBlock.gif
ExpandedSubBlockEnd.gif    }
 
InBlock.gif
InBlock.gif    
public static void SendStringToPrinter(string szPrinterName, string szString) 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif
InBlock.gif        IntPtr pBytes; 
InBlock.gif        Int32 dwCount; 
InBlock.gif        dwCount 
= szString.Length; 
InBlock.gif        pBytes 
= Marshal.StringToCoTaskMemAnsi(szString); 
InBlock.gif        SendBytesToPrinter(szPrinterName, pBytes, dwCount); 
InBlock.gif        Marshal.FreeCoTaskMem(pBytes); 
ExpandedSubBlockEnd.gif    }
 
ExpandedBlockEnd.gif}



调用

None.gif
None.gif    sPrintStr   
=   "  test "
None.gif
None.gif    PrintDialog pd 
=   new  PrintDialog(); 
None.gif    pd.PrinterSettings 
=   new  PrinterSettings(); 
None.gif    
// if (pd.ShowDialog(this) > DialogResult.None)
None.gif    
// {
None.gif
    RawPrinterHelper.SendStringToPrinter(pd.PrinterSettings.PrinterName, sPrintStr); 
None.gif    
// }

 

有段以前VB的懒的改了找了段VB.net  http://support.microsoft.com/default.aspx?scid=KB;EN-US;322090 的翻过来,结果搞了一个多小时才调通还不如自己写呢。
原代码语法翻译工具 VB.Net  To C# 编译时候会遇到n多默认名空间、类型转换、修饰符、返回值、初始化、方法的()等问题,SendFileToPrinter是编译后用Reflector反编译回来的也有些莫名其妙的问题。
我语法翻译了一份,反编译一份对照这改才算调试通过了。
VB.net的代码还是编译了再 DllImport方便些。

转载于:https://www.cnblogs.com/kilnt/archive/2005/09/08/232288.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值