C#编程-136:Windows打印技术_彭世瑜_新浪博客_wpf

C#编程-136:Windows打印技术_彭世瑜_新浪博客_c#_02

C#编程-136:Windows打印技术_彭世瑜_新浪博客_面向对象编程_03

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace PrintDialogTest
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         public Form1()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.         private void button1_Click(object sender, EventArgs e)
  20.         {
  21.             try
  22.             {
  23.                 pageSetupDialog1.Document = printDocument1;
  24.                 pageSetupDialog1.ShowDialog();
  25.             }
  26.             catch (Exception ex)
  27.             {
  28.  
  29.                 MessageBox.Show(ex.Message,"打印设置出错",MessageBoxButtons.OK,MessageBoxIcon.Error);
  30.             }
  31.             
  32.  
  33.         }
  34.  
  35.         private void button2_Click(object sender, EventArgs e)
  36.         {
  37.             if (printDialog1.ShowDialog() == DialogResult.OK)
  38.             {
  39.                 MessageBox.Show("打印");
  40.             }
  41.         }
  42.  
  43.         private void button4_Click(object sender, EventArgs e)
  44.         {
  45.             try
  46.             {
  47.                 printPreviewDialog1.Document = printDocument1;
  48.                 printPreviewDialog1.ShowDialog();
  49.             }
  50.             catch (Exception ex)
  51.             {
  52.                 MessageBox.Show(ex.Message,"打印预览出错",MessageBoxButtons.OK,MessageBoxIcon.Error);
  53.                  
  54.             }
  55.              
  56.         }
  57.  
  58.         private void button3_Click(object sender, EventArgs e)
  59.         {
  60.             try
  61.             {
  62.                 printDocument1.Print();
  63.             }
  64.             catch (Exception ex)
  65.             {
  66.                 MessageBox.Show(ex.Message,"打印出错",MessageBoxButtons.OK,MessageBoxIcon.Error);
  67.                  
  68.             }
  69.         }
  70.  
  71.         private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
  72.         {
  73.             //左边距
  74.             int x = e.PageBounds.Left;
  75.             //上边距
  76.             int y = e.PageBounds.Top;
  77.             //获得绘图对象
  78.             Graphics g = e.Graphics;
  79.             Font printFont = new Font("宋体",14);
  80.             SolidBrush brush = new SolidBrush(Color.Black);
  81.             string text="第一次学习打印机设置,输出的文字内容";
  82.             g.DrawString(text,printFont,brush,x,y);
  83.         }
  84.     }
  85. }