PrintDocument.PrintPage Zdarzenie

Definicja

Występuje, gdy dane wyjściowe do drukowania dla bieżącej strony są potrzebne.

C#
public event System.Drawing.Printing.PrintPageEventHandler PrintPage;

Typ zdarzenia

Przykłady

W poniższym przykładzie kodu pokazano obsługę PrintPage zdarzenia.

Inny przykład można znaleźć w temacie How to: Print a Multi-Page Text File in Windows Forms (Instrukcje: drukowanie wielostronicowego pliku tekstowego w Windows Forms).

C#
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

public partial class Form1 : System.Windows.Forms.Form
{
    private System.ComponentModel.Container components;
    private System.Windows.Forms.Button printButton;
    private Font printFont;
    private StreamReader streamToPrint;

    public Form1()
    {
        // The Windows Forms Designer requires the following call.
        InitializeComponent();
    }

    // The Click event is raised when the user clicks the Print button.
    private void printButton_Click(object sender, EventArgs e)
    {
        try
        {
            streamToPrint = new StreamReader
               ("C:\\My Documents\\MyFile.txt");
            try
            {
                printFont = new Font("Arial", 10);
                PrintDocument pd = new PrintDocument();
                pd.PrintPage += new PrintPageEventHandler
                   (this.pd_PrintPage);
                pd.Print();
            }
            finally
            {
                streamToPrint.Close();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    // The PrintPage event is raised for each page to be printed.
    private void pd_PrintPage(object sender, PrintPageEventArgs ev)
    {
        float linesPerPage = 0;
        float yPos = 0;
        int count = 0;
        float leftMargin = ev.MarginBounds.Left;
        float topMargin = ev.MarginBounds.Top;
        string line = null;

        // Calculate the number of lines per page.
        linesPerPage = ev.MarginBounds.Height /
           printFont.GetHeight(ev.Graphics);

        // Print each line of the file.
        while (count < linesPerPage &&
           ((line = streamToPrint.ReadLine()) != null))
        {
            yPos = topMargin + (count *
               printFont.GetHeight(ev.Graphics));
            ev.Graphics.DrawString(line, printFont, Brushes.Black,
               leftMargin, yPos, new StringFormat());
            count++;
        }

        // If more lines exist, print another page.
        if (line != null)
            ev.HasMorePages = true;
        else
            ev.HasMorePages = false;
    }

    // The Windows Forms Designer requires the following procedure.
    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.printButton = new System.Windows.Forms.Button();

        this.ClientSize = new System.Drawing.Size(504, 381);
        this.Text = "Print Example";

        printButton.ImageAlign =
           System.Drawing.ContentAlignment.MiddleLeft;
        printButton.Location = new System.Drawing.Point(32, 110);
        printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        printButton.TabIndex = 0;
        printButton.Text = "Print the file.";
        printButton.Size = new System.Drawing.Size(136, 40);
        printButton.Click += new System.EventHandler(printButton_Click);

        this.Controls.Add(printButton);
    }
}

Uwagi

Aby określić dane wyjściowe do wydrukowania, użyj Graphics właściwości PrintPageEventArgs. Aby na przykład określić wiersz tekstu, który ma zostać wydrukowany, narysuj tekst przy użyciu Graphics.DrawString metody .

Oprócz określania danych wyjściowych można wskazać, czy istnieją dodatkowe strony do wydrukowania, ustawiając PrintPageEventArgs.HasMorePages właściwość na truewartość . Wartość domyślna to false, co oznacza, że nie ma więcej stron do wydrukowania. Poszczególne ustawienia strony można również zmodyfikować za pomocą PageSettings polecenia , a zadanie drukowania można anulować, ustawiając PrintPageEventArgs.Cancel właściwość na true. Aby wydrukować każdą stronę dokumentu przy użyciu różnych ustawień strony, obsłuż QueryPageSettings zdarzenie.

Aby skojarzyć zdarzenie z programem obsługi zdarzeń, dodaj wystąpienie PrintPageEventHandler delegata do zdarzenia. Procedura obsługi zdarzeń jest wywoływana za każdym razem, gdy wystąpi zdarzenie. Aby uzyskać więcej informacji na temat obsługi zdarzeń za pomocą delegatów, zobacz Obsługa i podnoszenie zdarzeń.

Dotyczy

Produkt Wersje
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

Zobacz też