Bagikan melalui


PrintDocument.QueryPageSettings Kejadian

Definisi

Terjadi segera sebelum setiap PrintPage peristiwa.

public:
 event System::Drawing::Printing::QueryPageSettingsEventHandler ^ QueryPageSettings;
public event System.Drawing.Printing.QueryPageSettingsEventHandler QueryPageSettings;
member this.QueryPageSettings : System.Drawing.Printing.QueryPageSettingsEventHandler 
Public Custom Event QueryPageSettings As QueryPageSettingsEventHandler 

Jenis Acara

Contoh

Contoh kode berikut mencetak dokumen dengan halaman pertama berwarna, jika printer mendukungnya. Contohnya mengharuskan PrintDocument variabel bernama printDoc telah dibuat, dan peristiwa dan QueryPageSettings ditanganiPrintPage. Variabel currentPageNumber dinaikkan setelah setiap halaman dicetak dalam PrintPage peristiwa, yang tidak ditampilkan.

System.Drawing Gunakan namespace layanan dan System.Drawing.Printing untuk contoh ini.

private:
   void MyButtonPrint_OnClick( Object^ sender, System::EventArgs^ e )
   {
      // Set the printer name and ensure it is valid. If not, provide a message to the user.
      printDoc->PrinterSettings->PrinterName = "\\mynetworkprinter";
      if ( printDoc->PrinterSettings->IsValid )
      {
         // If the printer supports printing in color, then override the printer's default behavior.
         if ( printDoc->PrinterSettings->SupportsColor )
         {
            // Set the page default's to not print in color.
            printDoc->DefaultPageSettings->Color = false;
         }

         // Provide a friendly name, set the page number, and print the document.
         printDoc->DocumentName = "My Presentation";
         currentPageNumber = 1;
         printDoc->Print();
      }
      else
      {
         MessageBox::Show( "Printer is not valid" );
      }
   }

   void MyPrintQueryPageSettingsEvent( Object^ sender, QueryPageSettingsEventArgs^ e )
   {
      // Determines if the printer supports printing in color.
      if ( printDoc->PrinterSettings->SupportsColor )
      {
         // If the printer supports color printing, use color.
         if ( currentPageNumber == 1 )
         {
            e->PageSettings->Color = true;
         }
      }
   }

private void MyButtonPrint_OnClick(object sender, System.EventArgs e)
{
    
    // Set the printer name and ensure it is valid. If not, provide a message to the user.
    printDoc.PrinterSettings.PrinterName = "\\mynetworkprinter";

    if (printDoc.PrinterSettings.IsValid) {
    
        // If the printer supports printing in color, then override the printer's default behavior.
        if (printDoc.PrinterSettings.SupportsColor) {

            // Set the page default's to not print in color.
            printDoc.DefaultPageSettings.Color = false;
        }

        // Provide a friendly name, set the page number, and print the document.
        printDoc.DocumentName = "My Presentation";
        currentPageNumber = 1;
        printDoc.Print();
    }
    else {
        MessageBox.Show("Printer is not valid");
    }
}

private void MyPrintQueryPageSettingsEvent(object sender, QueryPageSettingsEventArgs e)
{
    // Determines if the printer supports printing in color.
    if (printDoc.PrinterSettings.SupportsColor) {

        // If the printer supports color printing, use color.
        if (currentPageNumber == 1 ) {

            e.PageSettings.Color = true;
        }
    }    
}

Private Sub MyButtonPrint_OnClick(ByVal sender As Object, ByVal e As System.EventArgs)

    ' Set the printer name and ensure it is valid. If not, provide a message to the user.
    printDoc.PrinterSettings.PrinterName = "\\mynetworkprinter"

    If printDoc.PrinterSettings.IsValid Then

        ' If the printer supports printing in color, then override the printer's default behavior.
        if printDoc.PrinterSettings.SupportsColor then

            ' Set the page default's to not print in color.
            printDoc.DefaultPageSettings.Color = False
        End If

        ' Provide a friendly name, set the page number, and print the document.
        printDoc.DocumentName = "My Presentation"
        currentPageNumber = 1
        printDoc.Print()
    Else
        MessageBox.Show("Printer is not valid")
    End If
End Sub

Private Sub MyPrintQueryPageSettingsEvent(ByVal sender As Object, ByVal e As QueryPageSettingsEventArgs)

    ' Determines if the printer supports printing in color.
    If printDoc.PrinterSettings.SupportsColor Then

        ' If the printer supports color printing, use color.
        If currentPageNumber = 1 Then

            e.PageSettings.Color = True
        End If

    End If
End Sub

Keterangan

Dimungkinkan untuk mencetak setiap halaman dokumen menggunakan pengaturan halaman yang berbeda. Anda mengatur pengaturan halaman dengan mengubah properti QueryPageSettingsEventArgs.PageSettings properti individual atau dengan mengatur properti ke PageSettings. Perubahan yang dibuat pada PageSettings hanya mempengaruhi halaman saat ini, bukan pengaturan halaman default dokumen. Tugas cetak juga dapat dibatalkan dengan mengatur Cancel properti ke true untuk QueryPageSettingsEventArgs.

Untuk mengaitkan peristiwa dengan penanganan aktivitas Anda, tambahkan instans QueryPageSettingsEventHandler delegasi ke peristiwa. Penanganan aktivitas dipanggil setiap kali peristiwa terjadi. Untuk informasi selengkapnya tentang menangani peristiwa dengan delegasi, lihat Menangani dan Meningkatkan Peristiwa.

Jika Anda menggunakan kejadian untuk QueryPageSettings mengubah pengaturan printer, performa PrintPreviewDialog kontrol tidak akan meningkat meskipun sakelar konfigurasi pengoptimalan diatur. Untuk informasi selengkapnya lihat Gambaran umum kontrol PrintPreviewDialog.

Berlaku untuk

Lihat juga