欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 霍尼韦尔扫码枪扫码打印

程序员文章站 2022-04-08 15:50:13
程序运行背景条件: 1、将扫码枪调制串口驱动模式 2、将扫码枪所在串口拆分成几个虚拟串口 3、扫码枪扫描条码就打印条码 4、WinForm程序 条码控件使用 DevExpress.XtraEditors.BarCodeControl 控件 BarMa BarMa 控件加载时将文本居中 BarMa.V ......

程序运行背景条件:

1、将扫码枪调制串口驱动模式

2、将扫码枪所在串口拆分成几个虚拟串口

3、扫码枪扫描条码就打印条码

4、winform程序

C# 霍尼韦尔扫码枪扫码打印

条码控件使用 devexpress.xtraeditors.barcodecontrol   控件 barma

C# 霍尼韦尔扫码枪扫码打印

 

 barma 控件加载时将文本居中

barma.verticalalignment = devexpress.utils.vertalignment.center;
barma.horizontalalignment = devexpress.utils.horzalignment.near;
barma.horizontaltextalignment = devexpress.utils.horzalignment.center;
barma.verticaltextalignment = devexpress.utils.vertalignment.center;

barma 添加事件

barma.textchanged += barma_textchanged;
barma.paint += barma_paint;

 

为窗体添加printpreviewdialog和printdocument控件

printpreviewdialog1的document属性设置为printdocument1

printdocument1添加事件  printdocument1.printpage += printdocument1_printpage;

虚拟串口:

serialport spreceive; //spreceive接受数据
private delegate void mydelegate(string indata);
private mydelegate showdelegate = null;

 

spreceive = new serialport(cbreviceserial.selecteditem.tostring(), convert.toint32(cbdatarate.selecteditem.tostring()), parity.none, 8, stopbits.one);
spreceive.datareceived += new serialdatareceivedeventhandler(datareceivedhandler);
spreceive.open();
showdelegate = new mydelegate(show);

//串口数据监听事件

private void datareceivedhandler(object sender, serialdatareceivedeventargs e)
{
serialport sp = (serialport)sender;
this.invoke(showdelegate, new object[] { sp.readexisting() });
}
void show(string indata)
{
txtbarcode.text = indata;
barma.text = indata;
}

private void barma_textchanged(object sender, eventargs e)
{
btncreat_click(sender, e);
}

//生成条码事件

private void btncreat_click(object sender, eventargs e)
{
barma.text = txtbarcode.text.trim();
barma.size = new size(convert.toint32(txtbarw.text), convert.toint32(txtbarh.text));
}

//条码控件重绘事件。在条码重绘事件中打印条码可以有效的将条码重绘之后的条码打印,在其他情况下打印条码会打印之前的条码

private void barma_paint(object sender, painteventargs e)
{
if (iopent)//虚拟串口打开状态下
{
capturescreen(barma); //绘制条形码
 printdocument1.print();
}
}

//打印条码事件

private void btnprint_click(object sender, eventargs e)
{
capturescreen(barma); //绘制条形码
printdocument1.print();
}

 

#region 绘制条形码

/// <summary>
/// 绘制条形码
/// </summary>
private void capturescreen(control control)
{
using (graphics g = control.creategraphics())
{
size s = control.size;
mimage = new bitmap(s.width, s.height, g); //实例化bitmap对象
using (graphics mg = graphics.fromimage(mimage)) //实例化graphics画图对象
{
intptr dc1 = g.gethdc(); //获取panel句柄
intptr dc2 = mg.gethdc(); //获取绘图对象句柄
//绘制panel控件中内容
bitblt(dc2, 0, 0, control.width, control.height, dc1, 0, 0, 13369376);
g.releasehdc(dc1); //释放panel句柄资源
mg.releasehdc(dc2); //释放绘图对象句柄资源
}
}
}

private void printdocument1_printpage(object sender, system.drawing.printing.printpageeventargs e)
{
e.graphics.drawimage(mimage, 0, 0); //img大小
e.hasmorepages = false;
//e.graphics.drawstring(barma.text, ft, system.drawing.brushes.black, 0, 0);
}

#endregion

#region 将一幅位图从一个设备场景复制到另一个
/// <summary>
/// 将一幅位图从一个设备场景复制到另一个
/// </summary>
/// <param name="hdcdest">目标设备场景</param>
/// <param name="nxdest">对目标dc中目标矩形左上角位置进行描述的那个点,用目标dc的逻辑x坐标表示</param>
/// <param name="nydest">对目标dc中目标矩形左上角位置进行描述的那个点,用目标dc的逻辑y坐标表示</param>
/// <param name="nwidth">欲传输图象的宽度</param>
/// <param name="nheight">欲传输图象的高度</param>
/// <param name="hdcsrc">源设备场景,如光栅运算未指定源,则应设为0</param>
/// <param name="nxsrc">对源dc中源矩形左上角位置进行描述的那个点用源dc的逻辑x坐标表示</param>
/// <param name="nysrc">对源dc中源矩形左上角位置进行描述的那个点用源dc的逻辑y坐标表示</param>
/// <param name="dwrop">传输过程要执行的光栅运算</param>
/// <returns>非零表示成功,零表示失败</returns>
[dllimport("gdi32.dll")]
public static extern long bitblt(intptr hdcdest, int nxdest, int nydest, int nwidth,

int nheight, intptr hdcsrc, int nxsrc, int nysrc, int dwrop);

#endregion