您的位置:首页 > 编程语言 > C#

C# winform打印总结

2014-11-26 16:58 176 查看
针对BEIYANG收据打印机 BTP-R580测试通过。

操作说明:http://www.docin.com/p-395110672.html

1、一般的打印

static Font printFont; //打印使用的字体

public static void PrintEventPage(object sender, PrintPageEventArgs e)

{

float yPos = 0;

int count = 0;

float leftMargin = e.MarginBounds.Left;

float topMargin = e.MarginBounds.Top;

//string line = null;

//string subs = " ";

string context = null;

//打印字体

printFont = new Font("宋体", 14F, FontStyle.Bold | FontStyle.Underline);

//打印内容

context = "收银小票";

//打印的位置

yPos = topMargin + (count * printFont.GetHeight(e.Graphics));

e.Graphics.DrawString(context, printFont, Brushes.Black,

50, 10, new StringFormat());

//换行

count++;

e.HasMorePages = false;

}

调用处:

System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();

System.Drawing.Printing.PrintDocument docToPrint = new System.Drawing.Printing.PrintDocument();

docToPrint.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintEventPage);

PrintDialog1.AllowSomePages = true;

PrintDialog1.ShowHelp = true;

PrintDialog1.Document = docToPrint;//把PrintDialog的Document属性设为上面配置好的PrintDocument的实例

PrintController printController = new StandardPrintController();

docToPrint.PrintController = printController;

DialogResult result = PrintDialog1.ShowDialog();//调用PrintDialog的ShowDialog函数显示打印对话框

//If the result is OK then print the document.

if (result == DialogResult.OK)

{

docToPrint.Print();//开始打印

}

效果:直接调出设置打印机的窗口。

2、USB打印

思路:

1、先找到USB 打印机。SetupDiGetClassDevs、SetupDiEnumDeviceInfo、SetupDiGetDeviceRegistryProperty,找到“USB 支持设备”;

2、然后就是老路,获得该USB打印机的路径,SetupDiEnumDeviceInterfaces、SetupDiGetInterfaceDeviceDetail,获得路径。(SetupDiGetClassDevs不用了,因为1中已经获得句柄集)

3、再根据路径CreateFile,后面的内容网上一搜一大堆。

步骤:

1、CreateFile

2、SetupComm

3、SetCommTimeouts

4、GetCommState

5、SetCommState

6、PurgeComm

7、WriteFile

不知道是不是绕远路了,呵呵。

参考资料:http://www.cnblogs.com/SunYu/archive/2010/04/29/1723977.html

参考代码:http://www.codeproject.com/Articles/14500/Detecting-Hardware-Insertion-and-or-Removal

USB下直接用程序访问不太方便,于是利用下面这个连接里的方法进行LPT并口转换:

http://www.yt-pos.com/ask/question.php?id=263

然后就可以连接上打印机了。

参考其他网站:

http://zhidao.baidu.com/link?url=3hhQOUVhcJdaV9tNgxNnD_SqEUgA-cesSciNvhV42a4G6Pu8u5r17m1J3HmHTud_6ZAtyQquX9gHzVoIYgaLqa

http://bbs.csdn.net/topics/390085304?page=1#post-398581498

/article/5806164.html

打印输出并切纸操作:

打印 自动切纸 走纸

来自:http://blog.163.com/lvjiwei789789@126/blog/static/38442411200891694155488/

第一点:

const uint GENERIC_READ =0x80000000;

const uint GENERIC_WRITE = 0x40000000;

const int OPEN_EXISTING = 3;

private FileStream fs=null;

IntPtr iHandle;

[DllImport("kernel32.dll")]

public static extern IntPtr CreateFileA(string lpFileName, uint dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplateFile);

[DllImport("kernel32.dll")]

private static extern bool WriteFile(

int hFile,

byte[] lpBuffer,

int nNumberOfBytesToWrite,

ref int lpNumberOfBytesWritten,

ref int i

);

[DllImport("kernel32.dll")]

private static extern bool CloseHandle(IntPtr hObject);

第二点:

protected void imageButton1_Click(object sender, ImageClickEventArgs e)

{

ShopCart mycart = CartCtroller.GetCart();

iHandle = CreateFileA("LPT1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);

if (iHandle.ToInt32() == -1)

{

Jscript.AjaxRunJs(this.Page, "alert('没有连接打印机或者打印机端口不是LPT1!')");

return;

}

else

{

fs = new FileStream(iHandle, FileAccess.ReadWrite);

//for (int i = 0; i < 5; i++)

//{

// fs.WriteByte(0x0A); //走纸(忘记了可能是这个)

//}

//StreamReader sr = new StreamReader(fs); //读数据;

//StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default); //些数据;

if (mycart.Count == 0)

{

Jscript.AjaxRunJs(this.Page, "alert('请先购买产品!')");

return;

}

byte[] buf = new byte[500];

char ESC = (char)(27);

char GS = (char)(29);

char LF = (char)(10);

//读出数据

StringBuilder SEND = new StringBuilder();

//SEND.Append(LF.ToString());

char c1 = (char)60;

char c2 = (char)0;

SEND.Append(GS.ToString() + "L" + c1.ToString() + c2.ToString());

SEND.Append(ESC + "D" + Convert.ToChar(25) + Convert.ToChar(30) + Convert.ToChar(0));

string dt = DateTime.Now.ToString();

string mingcheng = "产品名称:";

string miaoshu = "产品描述:";

string shuliang = "购买数量:";

string zhi = "支";

SEND.Append(LF.ToString());

SEND.Append("欢 迎 光 临 - 上海金福");

SEND.Append("\r\n");

SEND.Append("-------------------------------");

SEND.Append(LF.ToString());

SEND.Append("购买时间:"+dt);

SEND.Append("\r\n");

SEND.Append("-------------------------------");

SEND.Append(LF.ToString());

SEND.Append(LF.ToString());

for (int i = 0; i < mycart.Count; i++)

{

modelArrayProduct temp = (modelArrayProduct)mycart[i];

SEND.Append(mingcheng + temp.T_name);

SEND.Append("\r\n");

SEND.Append(miaoshu + temp.T_miaoshu);

SEND.Append("\r\n");

SEND.Append(shuliang + temp.T_shuliang.ToString() + zhi);

SEND.Append("\r\n");

SEND.Append("-------------------------------");

SEND.Append(LF.ToString());

}

//走纸

SEND.Append(LF.ToString());

SEND.Append(LF.ToString());

SEND.Append(LF.ToString());

SEND.Append(LF.ToString());

SEND.Append(GS + "V" + Convert.ToChar(1));

//字符类型转换

Encoding utf8 = Encoding.GetEncoding(65001);

Encoding gb2312 = Encoding.GetEncoding("gb2312");//Encoding.Default ,936

byte[] tempa = utf8.GetBytes(SEND.ToString());

buf = Encoding.Convert(utf8, gb2312, tempa);

fs.Write(buf, 0, buf.Length);

fs.Close();

}

CloseHandle(iHandle);

}

C#执行批处理命令

using System.Diagnostics ;

using System.IO;

private void btnRun_Click(object sender, EventArgs e)

{

txtResult.Text = "";

processCommand("Ping.exe", this.txtAddress.Text);

processCommand("Ping.exe", this.txtAddress.Text);

}

public void processCommand(string commandName, string argument)

{

ProcessStartInfo start = new ProcessStartInfo(commandName);//设置运行的命令行文件问ping.exe文件,这个文件系统会自己找到

//如果是其它exe文件,则有可能需要指定详细路径,如运行winRar.exe

start.WorkingDirectory = "d:\\360Downloads\\";

start.Arguments = argument;//设置命令参数

start.CreateNoWindow = true;//不显示dos命令行窗口

start.RedirectStandardOutput = true;//

start.RedirectStandardInput = true;//

start.UseShellExecute = false;//是否指定操作系统外壳进程启动程序

txtResult.AppendText(start.WorkingDirectory + "\n");

Process p = Process.Start(start);

StreamReader reader = p.StandardOutput;//截取输出流

string line = reader.ReadLine();//每次读取一行

while (!reader.EndOfStream)

{

txtResult.AppendText(line + "\n");

line = reader.ReadLine();

}

p.WaitForExit();//等待程序执行完退出进程

p.Close();//关闭进程

reader.Close();//关闭流

}

以上均测试成功。

补充BEIYANG BTP-R580相关参数:

主要参数
打印方式热敏打印分辨率203×203dpi
最高打印速度230mm/s打印宽度(mm)80
基本参数
打印方式热敏打印分辨率203×203dpi
最高打印速度230mm/s
高级参数
打印宽度(mm)80打印方向双向打印
纸张种类连续纸, 标记纸, 标签纸纸张厚度0.06mm-0.1mm
字体ASCII(12×24), 压缩 ASCII(9×17), 国标宋体I, Ⅱ级(24×24), 国际字符供纸方式自动
接口类型并行接口(IEEE 1284), RS-232, RS-485/422串行接口, USB接口, Ethernet接口, WLAN接口可选
电源参数
电源电压(V)220±10%电源频率(Hz)50-60
外观参数
颜色黑色重量(Kg)1.3
高度(mm)147宽度(mm)147
长度(mm)205
测试打印机二:

ESPON TM TM58 票据打印机

爱普生 T58 基本参数
产品类型:微型热敏打印机
打印方式:热敏式
打印宽度:32/16
缓冲区:2KB
接口类型:RS-232,Bi-directionalparallel
爱普生 T58 打印性能
打印速度:最大100mm/sec
打印分辨率:360dpi
字符集:95Alphanumeric,37 International,128×1Graphic
字体:12×24(ANK)/24×24(Chinese)
爱普生 T58 介质规格
介质宽度:57.5±0.5mm
介质厚度:0.06-0.07mm
爱普生 T58 其它参数
平均无故障时间:160000小时
产品尺寸:116×204×137mm
产品重量:1.3kg
系统平台:Windows2000/XP
电源电压:DC24V(±7%),1.1A
其它特性:字符尺寸:1.25×3.0mm/2.5×3.0mm

每英寸字符数:16.9cpi/10.2cpi

D.K.D.功能:2drives

外壳颜色:ECW

安全标准:CCC

电源:Exclusive AC adapter(included)

配件:AC adapter,I/F cable,User’s manual

安全标准:UL/CSA/T+V(E N60950)
另参考:
/article/9329143.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: