PrintDocument.QueryPageSettings イベント

定義

PrintPage イベントの直前に発生します。Occurs immediately before each PrintPage event.

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 

次のコード例では、プリンターでサポートされている場合、最初のページを色で印刷してドキュメントを印刷します。The following code example prints a document with the first page in color, if the printer supports it. この例では、printDoc という名前の PrintDocument 変数が作成され、PrintPage イベントと QueryPageSettings イベントが処理される必要があります。The example requires that a PrintDocument variable named printDoc has been created, and the PrintPage and QueryPageSettings events are handled. currentPageNumber 変数は、PrintPage イベントですべてのページが印刷された後にインクリメントされます。これは表示されません。The currentPageNumber variable is incremented after every page is printed in the PrintPage event, which is not shown.

この例では、System.DrawingSystem.Drawing.Printing 名前空間を使用します。Use the System.Drawing and System.Drawing.Printing namespaces for this example.

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

注釈

異なるページ設定を使用して、ドキュメントの各ページを印刷することができます。It is possible to print each page of a document using different page settings. ページ設定を設定するには、QueryPageSettingsEventArgs.PageSettings プロパティの個々のプロパティを変更するか、プロパティを PageSettingsに設定します。You set page settings by modifying individual properties of the QueryPageSettingsEventArgs.PageSettings property or by setting the property to a PageSettings. PageSettings に加えられた変更は、ドキュメントの既定のページ設定ではなく、現在のページにのみ影響します。Changes made to the PageSettings affect only the current page, not the document's default page settings. また、Cancel プロパティを QueryPageSettingsEventArgstrue に設定して、印刷ジョブをキャンセルすることもできます。The print job can also be canceled by setting the Cancel property to true for the QueryPageSettingsEventArgs.

イベントをイベントハンドラーに関連付けるには、イベントに QueryPageSettingsEventHandler デリゲートのインスタンスを追加します。To associate the event with your event handler, add an instance of the QueryPageSettingsEventHandler delegate to the event. イベントハンドラーは、イベントが発生するたびに呼び出されます。The event handler is called whenever the event occurs. デリゲートを使用したイベントの処理の詳細については、「イベントの処理と発生」を参照してください。For more information about handling events with delegates, see Handling and Raising Events.

QueryPageSettings イベントを使用してプリンターの設定を変更した場合、最適化構成スイッチが設定されていても、PrintPreviewDialog コントロールのパフォーマンスは向上しません。If you use the QueryPageSettings event to modify printer settings, the performance of the PrintPreviewDialog control will not improve even if an optimization configuration switch is set. 詳細については、「 Printプレビューダイアログコントロールの概要」を参照してください。For more information, see PrintPreviewDialog control overview.

適用対象

こちらもご覧ください