2024年4月25日发(作者:)

C# Winform打印小票

(1)自定义纸张设置

控制面板->打印机和传真->右键->服务器属性->创建新的格式

(2)自定义纸张使用

ize = new

ize("NewPrint", iWidth, iHeight);

NewPrint:制定一纸张名称。iWidth:纸张使用宽度。iHeight:纸张使用高度。

iWidth,iHeight 可以在使用过程中调整。

例如:iWidth=923,iHeight=480

(3)ESC/P指令使用

using System;

pServices;

;

using ;

;

namespacePrintDome

{

classClsPrintLPT

{

privateIntPtriHandle;

privateFileStreamfs;

privateStreamWritersw;

private string prnPort = "LPT1"; //打印机端口

publicClsPrintLPT()

{

}

privateconstuint GENERIC_READ = 0x80000000;

privateconstuint GENERIC_WRITE = 0x40000000;

privateconstint OPEN_EXISTING = 3;

///

/// 打开一个vxd(设备)

///

[DllImport("", EntryPoint = "CreateFile", CharSet = )]

private static extern IntPtrCreateFile(string lpFileName, uintdwDesiredAccess, intdwShareMode,

intlpSecurityAttributes,

intdwCreationDisposition, intdwFlagsAndAttributes, inthTemplateFile);

///

/// 开始连接打印机

///

privateboolPrintOpen()

{

iHandle = CreateFile(prnPort, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);

if (32() == -1)

{

("没有连接打印机或者打印机端口不是LPT1!", "提示",

, ation);

return false;

}

else

{

fs = new FileStream(iHandle, ite);

sw = new StreamWriter(fs, t); //写数据

return true;

}

}

///

/// 打印字符串

///

/// 要打印的字符串

private void PrintLine(string str)

{

ine(str); ;

}

///

/// 关闭打印连接

///

private void PrintEnd()

{