打印控件

时间:2021-11-26 04:48:35

绘制如下窗体

一、PrintDocument -打印的基础  首先放置PrintDocument控件,双击事件PrintPage设置要打印的样式(李献策lxc)

  

//第一步 设置打印属性 private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { //设置字体样式 Font f = new System.Drawing.Font("微软雅黑", 16); f = richTextBox1.Font; //设置字体颜色 Brush b = new SolidBrush(richTextBox1.ForeColor); //e.绘制.画字符串(要打印的文本,文本格式,画刷-颜色和纹理,位置坐标) e.Graphics.DrawString(richTextBox1.Text, f, b, 20, 10); } //字体样式设置 private void button4_Click(object sender, EventArgs e) { DialogResult dr = fontDialog1.ShowDialog(); if (dr == DialogResult.OK) { richTextBox1.Font = fontDialog1.Font; richTextBox1.ForeColor = fontDialog1.Color; } }

设置Document

二、PageSetupDialog - 打印页面设置

//第二步 设置打印页面设置 private void button1_Click(object sender, EventArgs e) { pageSetupDialog1.Document = printDocument1; pageSetupDialog1.ShowDialog(); }

打印页面设置

三、1、PrintPreviewContol - 打印预览格式一,在窗体中设置预览区域

//第三步 打印预览一 private void button2_Click(object sender, EventArgs e) { printPreviewControl1.Document = printDocument1; }

打印预览一

2、PrintPreviewDialog - 打印预览格式二,在弹窗中预览

//第三步 打印预览二 private void button2_Click(object sender, EventArgs e) { printPreviewDialog1.Document = printDocument1; printPreviewDialog1.ShowDialog(); }

打印预览二

四、PrintDialog - 开始打印

//第四步 开始打印 private void button3_Click(object sender, EventArgs e) { printDialog1.Document = printDocument1; DialogResult dr = printDialog1.ShowDialog(); if (dr == DialogResult.OK) { printDocument1.Print(); } }

开始打印