计算机打印机用户,如何:在 Windows 窗体中选择连接到用户计算机的打印机

使用 PrintDialog 组件选择要使用的打印机。

在下面的代码示例中,有两个要处理的事件。第一个事件是 Button 控件的 Click 事件,在该事件中,PrintDialog 类被实例化,并且用户选择的打印机在 DialogResult 属性中捕获。

第二个事件是 PrintDocument**** 组件的 PrintPage 事件,在该事件中,将一个示例文档打印到指定的打印机。

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim PrintDialog1 As New PrintDialog()

PrintDialog1.Document = PrintDocument1

Dim result As DialogResult = PrintDialog1.ShowDialog()

If (result = DialogResult.OK) Then

PrintDocument1.Print()

End If

End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

e.Graphics.FillRectangle(Brushes.Red, New Rectangle(500, 500, 500, 500))

End Sub

private void button1_Click(object sender, System.EventArgs e)

{

PrintDialog printDialog1 = new PrintDialog();

printDialog1.Document = printDocument1;

DialogResult result = printDialog1.ShowDialog();

if (result == DialogResult.OK)

{

printDocument1.Print();

}

}

private void printDocument1_PrintPage(object sender,

System.Drawing.Printing.PrintPageEventArgs e)

{

e.Graphics.FillRectangle(Brushes.Red,

new Rectangle(500, 500, 500, 500));

}

private void button1_Click(Object sender, System.EventArgs e)

{

PrintDialog printDialog1 = new PrintDialog();

printDialog1.set_Document(printDocument1);

DialogResult result = printDialog1.ShowDialog();

if (result == DialogResult.OK)

{

printDocument1.Print();

}

}

private void printDocument1_PrintPage(Object sender,

System.Drawing.Printing.PrintPageEventArgs e)

{

e.get_Graphics().FillRectangle(Brushes.get_Red(),

new Rectangle(500, 500, 500, 500));

}

private:

void button1_Click(System::Object ^ sender,

System::EventArgs ^ e)

{

PrintDialog ^ printDialog1 = gcnew PrintDialog();

printDialog1->Document = printDocument1;

System::Windows::Forms::DialogResult result =

printDialog1->ShowDialog();

if (result == DialogResult::OK)

{

printDocument1->Print();

}

}

private:

void printDocument1_PrintPage(System::Object ^ sender,

System::Drawing::Printing::PrintPageEventArgs ^ e)

{

e->Graphics->FillRectangle(Brushes::Red,

Rectangle(500, 500, 500, 500));

}

((Visual C#、Visual J# 和 Visual C++)在窗体的构造函数中放入以下代码以注册事件处理程序。

this.printDocument1.PrintPage += new

System.Drawing.Printing.PrintPageEventHandler

(this.printDocument1_PrintPage);

this.button1.Click += new System.EventHandler(this.button1_Click);

this.printDocument1.add_PrintPage(new

System.Drawing.Printing.PrintPageEventHandler

(this.printDocument1_PrintPage));

this.button1.add_Click(new System.EventHandler(this.button1_Click));

this->printDocument1->PrintPage += gcnew

System::Drawing::Printing::PrintPageEventHandler

(this, &Form1::printDocument1_PrintPage);

this->button1->Click += gcnew

System::EventHandler(this, &Form1::button1_Click);


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部