PrintDocument.QueryPageSettings 事件
在 PrintPage 事件的紧面前发生。
**命名空间:**System.Drawing.Printing
**程序集:**System.Drawing(在 system.drawing.dll 中)
语法
声明
Public Event QueryPageSettings As QueryPageSettingsEventHandler
用法
Dim instance As PrintDocument
Dim handler As QueryPageSettingsEventHandler
AddHandler instance.QueryPageSettings, handler
public event QueryPageSettingsEventHandler QueryPageSettings
public:
event QueryPageSettingsEventHandler^ QueryPageSettings {
void add (QueryPageSettingsEventHandler^ value);
void remove (QueryPageSettingsEventHandler^ value);
}
/** @event */
public void add_QueryPageSettings (QueryPageSettingsEventHandler value)
/** @event */
public void remove_QueryPageSettings (QueryPageSettingsEventHandler value)
JScript 支持使用事件,但不支持进行新的声明。
备注
用不同的页设置来打印文档的每一页是可能的。通过修改 QueryPageSettingsEventArgs.PageSettings 属性的各个属性,或者将该属性设置为 PageSettings,可以设置页设置。对 PageSettings 所做的更改只影响当前页,对文档的默认页设置没有影响。通过将 Cancel 属性设置为 QueryPageSettingsEventArgs 的 true,还可以取消打印作业。
若要使事件与事件处理程序相关联,请将 QueryPageSettingsEventHandler 委托的一个实例添加到事件。一旦发生该事件,将调用此事件处理程序。有关用委托处理事件的更多信息,请参见 事件和委托。
示例
下面的代码示例打印文档并彩印文档的第一页(如果打印机支持该功能)。该示例要求已创建名为 printDoc
的 PrintDocument 变量,并且处理 PrintPage 和 QueryPageSettings 事件。在 PrintPage 事件中打印每一页后,currentPageNumber
变量会递增,这一情况不会显示。
在此示例中使用 System.Drawing 和 System.Drawing.Printing 命名空间。
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
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:
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.get_PrinterSettings().set_PrinterName("\\mynetworkprinter");
if (printDoc.get_PrinterSettings().get_IsValid()) {
// If the printer supports printing in color, then
// override the printer's default behavior.
if (printDoc.get_PrinterSettings().get_SupportsColor()) {
// Set the page default's to not print in color.
printDoc.get_DefaultPageSettings().set_Color(false);
}
// Provide a friendly name, set the page number, and print
// the document.
printDoc.set_DocumentName("My Presentation");
currentPageNumber = 1;
printDoc.Print();
}
else {
MessageBox.Show("Printer is not valid");
}
} //myButtonPrint_OnClick
private void MyPrintQueryPageSettingsEvent(Object sender,
QueryPageSettingsEventArgs e)
{
// Determines if the printer supports printing in color.
if (printDoc.get_PrinterSettings().get_SupportsColor()) {
// If the printer supports color printing, use color.
if (currentPageNumber == 1) {
e.get_PageSettings().set_Color(true);
}
}
} //MyPrintQueryPageSettingsEvent
平台
Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
请参见
参考
PrintDocument 类
PrintDocument 成员
System.Drawing.Printing 命名空间
QueryPageSettingsEventHandler
PrintPageEventArgs
PrintDocument.BeginPrint 事件
PrintDocument.EndPrint 事件
PrintDocument.PrintPage 事件