110,552
社区成员
发帖
与我相关
我的任务
分享
string pk;
for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
{
pk = PrinterSettings.InstalledPrinters[i];
comboBox1.Items.Add(pk);
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Printing;
using System.Data.SQLite;
using OpenNETCF.Desktop.Communication;
using Cobainsoft.Windows.Forms;
using System.Xml;
namespace TVTowerK3Print
{
public partial class Form1 : Form
{
BarcodeControl bc = new BarcodeControl();
DataView dDV = null;
int column = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dDV="";//自己把datagridview赋上值
PrinterSettings ps = new PrinterSettings();
comboBox1.Text = ps.PrinterName;//获取打印机名称
}
private void btnPrint_Click(object sender, EventArgs e)
{
short copies = 1;//打印份数
try
{
copies = Convert.ToInt16(this.txtCount.Text.Trim());
if (copies < 1)
{
MessageBox.Show("打印数量必须大于0!");
return;
}
else
{
column = 0;
printDocument1.PrinterSettings.Copies = copies;
PaperSize ps = new PaperSize("a", 275, 157);
printDocument1.DefaultPageSettings.PaperSize = ps;
printDocument1.PrinterSettings.PrinterName = this.comboBox1.Text;//打印机名称
if (dDV != null)
{
foreach (DataRow dr in dDV.Table.Rows)
{
printDocument1.Print();
column += 1;
}
}
else
{
MessageBox.Show("请您先查询出需要打印的数据!");
}
//打印预览对话框
//PrintPreviewDialog ppd = new PrintPreviewDialog();
//ppd.Document = printDocument1;
//ppd.ShowDialog();
//printDocument1.Print();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics g = e.Graphics;
Rectangle rect = bc.ClientRectangle;
rect = new Rectangle(5, 45, this.bc.Width, this.bc.Height);
bc.Draw(g, rect, GraphicsUnit.Pixel, 1, 0, null);
g.DrawString("广州新电视塔建设有限公司", new Font("黑体", 12), Brushes.Black, new PointF(35, 5));
g.DrawString("经营管理分公司", new Font("黑体", 12), Brushes.Black, new PointF(75, 23));
g.DrawString(dDV.Table.Rows[column]["固定资产名称"].ToString(), new Font("黑体", 10), Brushes.Black, new PointF(30, 120));
g.DrawString("部门:" + dDV.Table.Rows[column]["使用部门"].ToString(), new Font("黑体", 10), Brushes.Black, new PointF(30, 140));
g.Dispose();
}
}
}