程序運行背景條件:
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