PrintDocument.QueryPageSettings 事件

定義

PrintPage 事件之前立即發生。

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 

事件類型

範例

如果印表機支援,下列程式代碼範例會列印具有第一頁色彩的檔。 此範例會要求 PrintDocument 已建立名為 printDoc 的變數,並 PrintPage 處理 和 QueryPageSettings 事件。 變數 currentPageNumber 會在事件中 PrintPage 列印每個頁面之後遞增,而不會顯示。

使用此範例的 System.DrawingSystem.Drawing.Printing 命名空間。

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

備註

您可以使用不同的頁面設定來列印檔的每個頁面。 您可以修改 屬性的 QueryPageSettingsEventArgs.PageSettings 個別屬性,或將 屬性設定為 PageSettings來設定頁面設定。 對 PageSettings 所做的變更只會影響目前頁面,而不是文件的默認頁面設定。 您也可以將 的屬性trueQueryPageSettingsEventArgs設定Cancel為 ,以取消列印作業。

若要將事件與您的事件處理程式產生關聯,請將委派的 QueryPageSettingsEventHandler 實例新增至 事件。 每當事件發生時,就會呼叫事件處理程式。 如需使用委派處理事件的詳細資訊,請參閱 處理和引發事件

如果您使用 QueryPageSettings 事件來修改印表機設定,即使已設定優化組態參數,控件的 PrintPreviewDialog 效能也不會改善。 如需詳細資訊,請參閱 PrintPreviewDialog 控制項概觀

適用於

另請參閱