码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2019-04-09 13:55:19      阅读:375      评论:0      收藏:0      [点我收藏+]

标签:its   运行   虚拟   执行   添加   phi   逻辑   review   width   

程序运行背景条件:

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

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

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

4、WinForm程序

技术图片

条码控件使用 DevExpress.XtraEditors.BarCodeControl   控件 BarMa

技术图片

 

 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

 

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

标签:its   运行   虚拟   执行   添加   phi   逻辑   review   width   

原文地址:https://www.cnblogs.com/liujianshe1990-/p/10676364.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!