winform做的一个小的标签打印,主要用DrawRectangle于DrawString循环遍历窗体需要显示的组件,实现套打。
效果图-->
PrintDocument printDocument = new PrintDocument();
private int _PrintPage = 0;//当前打印页数
private int _TotalPage = 1;//总页数
public void DoPrint()
{try
{
PrepareData();
if (_TotalPage <= 0)
return;
PrinterSetup();
printDocument.Print();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void PrepareData()
{
_TotalPage = Convert.ToInt32(comboBox1.Text.Trim());
}
private void PrinterSetup()
{
printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);
}
void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics g = e.Graphics;
//绘制标签头,并返回一个rectangle,得到标签头的坐标绘制标签品名厂商的坐标
Rectangle rectangle = DrawPanel(g);
// 循环绘制窗体上的组件
foreach (Control ctr in tabControl1.SelectedTab.Controls)
{
Drawyijia(ctr, g,rectangle);
}
_PrintPage++;//页号
if (_PrintPage < _TotalPage)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
}
private Rectangle DrawPanel(Graphics g)
{
int left = 0,top=0;
string File = "XML.xml"; //从xml中动态读取坐标数据left和top
XmlDocument doc = new XmlDocument();
doc.Load(File);
left =Convert.ToInt32(doc.GetElementsByTagName("left")[1].ChildNodes[0].Value);
top = Convert.ToInt32(doc.GetElementsByTagName("top")[1].ChildNodes[0].Value);
Pen blackPen = new Pen(Color.Black);
blackPen.Width = 1;
Font printFont = new Font("宋体", 12, FontStyle.Regular);
Rectangle r = new Rectangle(left, top, 360, 45);
Rectangle r1 = new Rectangle(left+265, top+45, 95, 280);
g.DrawRectangle(blackPen, r);
g.DrawRectangle(blackPen, r1);
g.DrawString("毅嘉电子(苏州)有限公司识别标签", printFont, new SolidBrush(Color.Black), left+40, top+15, new StringFormat());
g.DrawString("担当用印", printFont, new SolidBrush(Color.Black), left+270, top+50, new StringFormat());
return r;
}
private void Drawyijia(Control l, Graphics g,Rectangle rectangle)
{
Pen blackPen = new Pen(Color.Black);
blackPen.Width = 1;
//当组件是label时绘制
if(l is Label)
{
Rectangle r = GetControlRectLabel(l,rectangle);
g.DrawRectangle(blackPen,r);//绘制边框
Drawing(l,r,g);//绘制边框中的内容
}
//当组件是textbox或者datetimepick时绘制
else if (l is TextBox|| l is DateTimePicker)
{
Rectangle r = GetControlRectText(l,rectangle);
g.DrawRectangle(blackPen,r);//绘制边框
Drawing(l,r,g);
}
}
//绘制组件中的内容,条形码
void Drawing(Control l,Rectangle r,Graphics g)
{
Font printFont = new Font("宋体", 12, FontStyle.Regular);
Font ft2 = new System.Drawing.Font("C39HrP24DhTt", 24, FontStyle.Regular);//条形码字体
if (l.Name.Substring(0,2)!="id") //根据空间的name,如果是包含id则打印条形码否则直接打印空间中的内容
{
g.DrawString(l.Text, printFont, new SolidBrush(l.ForeColor), r.Left + 10, r.Top + 10, new StringFormat());
}
else
{ //打印条形码
g.DrawString("*" + l.Text + "*", ft2, new SolidBrush(l.ForeColor), r.Left, r.Top+5 , new StringFormat());
}
}
private Rectangle GetControlRectText(Control c,Rectangle rectangle)
{
int t = 0, l = 0, w = 0, h = 0;
if(c.Parent!=this)
{
l = rectangle.Left+95;
t = c.Top + rectangle.Top+23;
w = 170;
h = 40;
}
return new Rectangle(l,t,w,h);
}
private Rectangle GetControlRectLabel(Control c,Rectangle rectangle)
{
int t = 0, l = 0, w = 0, h = 0;
if (c.Parent != this)
{
l = rectangle.Left;
t = c.Top+rectangle.Top+23;
w = 95;
h = 40;
}
return new Rectangle(l, t, w, h);
}