天天看點

C#程式設計-136:Windows列印技術

C#程式設計-136:Windows列印技術
C#程式設計-136:Windows列印技術
C#程式設計-136:Windows列印技術
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace PrintDialogTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                pageSetupDialog1.Document = printDocument1;
                pageSetupDialog1.ShowDialog();
            }
            catch (Exception ex)
            {
 
                MessageBox.Show(ex.Message,"列印設定出錯",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
            
 
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            if (printDialog1.ShowDialog() == DialogResult.OK)
            {
                MessageBox.Show("列印");
            }
        }
 
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                printPreviewDialog1.Document = printDocument1;
                printPreviewDialog1.ShowDialog();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"列印預覽出錯",MessageBoxButtons.OK,MessageBoxIcon.Error);
                 
            }
             
        }
 
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                printDocument1.Print();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"列印出錯",MessageBoxButtons.OK,MessageBoxIcon.Error);
                 
            }
        }
 
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            //左邊距
            int x = e.PageBounds.Left;
            //上邊距
            int y = e.PageBounds.Top;
            //獲得繪圖對象
            Graphics g = e.Graphics;
            Font printFont = new Font("宋體",14);
            SolidBrush brush = new SolidBrush(Color.Black);
            string text="第一次學習列印機設定,輸出的文字内容";
            g.DrawString(text,printFont,brush,x,y);
        }
    }
}      

繼續閱讀