C#Form中,有谁知道怎么调用打印。

冷月孤峰 2003-08-20 03:30:26
C#form中,我的菜单中有一个打印菜单,但是不知道怎么实现。就象普通文件菜单中的页面设置、打印、打印预览一样。有哪位高手帮忙。有代码最好。谢谢。
...全文
59 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
asakao 2003-08-22
  • 打赏
  • 举报
回复
wd_318(初学者.net) 不支持网络打茚哦,
xiaodele 2003-08-22
  • 打赏
  • 举报
回复
www.csharp-corner.com/Graphics/DataGridPrinterMG.asp
冷月孤峰 2003-08-22
  • 打赏
  • 举报
回复
to WD-318(初学者)
我是想单击打印按钮时,能把dataGrid中显示的数据打印出来。
Jim3 2003-08-20
  • 打赏
  • 举报
回复
学习
好运 2003-08-20
  • 打赏
  • 举报
回复
mark
wd_318 2003-08-20
  • 打赏
  • 举报
回复
2、TextFilePrintDocument.cs

//-----------------------------------------------------------------------
// This file is part of the Microsoft .NET SDK Code Samples.
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//This source code is intended only as a supplement to Microsoft
//Development Tools and/or on-line documentation. See these other
//materials for detailed information regarding Microsoft code samples.
//
//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.
//-----------------------------------------------------------------------


namespace Microsoft.Samples.WinForms.Cs.PrintingExample5 {
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;

// <doc>
// <desc>
// TextFilePrintDocument prints a stream to a printer
//
// Note: In order to avoid problems closing a file
// if an exception occurs this class simply takes a
// steam and leaves it to the caller to open the file
// to print
//
// </desc>
// </doc>
public class TextFilePrintDocument : PrintDocument {
private Font printFont = null ;
private StreamReader streamToPrint = null ;

public TextFilePrintDocument(StreamReader streamToPrint) : base () {
this.streamToPrint = streamToPrint ;
}

//Override OnBeginPrint to set up the font we are going to use
protected override void OnBeginPrint(PrintEventArgs ev) {
base.OnBeginPrint(ev) ;
printFont = new Font("Arial", 10);
}

//Override the OnPrintPage to provide the printing logic for the document
protected override void OnPrintPage(PrintPageEventArgs ev) {

base.OnPrintPage(ev) ;

float lpp = 0 ;
float yPos = 0 ;
int count = 0 ;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
String line=null;

//Work out the number of lines per page
//Use the MarginBounds on the event to do this
lpp = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics) ;

//Now iterate over the file printing out each line
//NOTE WELL: This assumes that a single line is not wider than the page width
//Check count first so that we don't read line that we won't print
while (count < lpp && ((line=streamToPrint.ReadLine()) != null)) {
yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));

ev.Graphics.DrawString (line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());

count++;
}

//If we have more lines then print another page
if (line != null)
ev.HasMorePages = true ;
else
ev.HasMorePages = false ;
}

}

}


wd_318 2003-08-20
  • 打赏
  • 举报
回复
1、printform.cs


//-----------------------------------------------------------------------
// This file is part of the Microsoft .NET SDK Code Samples.
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//This source code is intended only as a supplement to Microsoft
//Development Tools and/or on-line documentation. See these other
//materials for detailed information regarding Microsoft code samples.
//
//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.
//-----------------------------------------------------------------------


namespace Microsoft.Samples.WinForms.Cs.PrintingExample5 {
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;

public class PrintForm : System.Windows.Forms.Form {
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components;
protected internal System.Windows.Forms.Button printButton;
protected internal System.Windows.Forms.Button pageSetupButton;
protected internal System.Windows.Forms.Button printPreviewButton;

private PageSettings storedPageSettings = null ;

public PrintForm() {
//
// Required for Windows Form Designer support
//
InitializeComponent();

// Wire up event handlers for print buttons
printButton.Click += new System.EventHandler(printButton_Click);
pageSetupButton.Click += new System.EventHandler(pageSetupButton_Click);
printPreviewButton.Click += new System.EventHandler(printPreviewButton_Click);
}

//Event fired when the user presses the page setup button
private void pageSetupButton_Click(object sender, EventArgs e) {

try {
PageSetupDialog psDlg = new PageSetupDialog() ;

if (storedPageSettings == null) {
storedPageSettings = new PageSettings();
}

psDlg.PageSettings = storedPageSettings ;
psDlg.ShowDialog();

} catch(Exception ex) {
MessageBox.Show("An error occurred - " + ex.Message);
}

}


//Event fired when the user presses the print button
private void printButton_Click(object sender, EventArgs e) {
try {

StreamReader streamToPrint = new StreamReader ("PrintMe.Txt");
try {
TextFilePrintDocument pd = new TextFilePrintDocument(streamToPrint); //Assumes the default printer

if (storedPageSettings != null) {
pd.DefaultPageSettings = storedPageSettings ;
}

PrintDialog dlg = new PrintDialog() ;
dlg.Document = pd;
DialogResult result = dlg.ShowDialog();

if (result == DialogResult.OK) {
pd.Print();
}

} finally {
streamToPrint.Close() ;
}

} catch(Exception ex) {
MessageBox.Show("An error occurred printing the file - " + ex.Message);
}
}


//Event fired when the user presses the print preview button
private void printPreviewButton_Click(object sender, EventArgs e) {
try {

StreamReader streamToPrint = new StreamReader ("PrintMe.Txt");
try {
TextFilePrintDocument pd = new TextFilePrintDocument(streamToPrint); //Assumes the default printer

if (storedPageSettings != null) {
pd.DefaultPageSettings = storedPageSettings ;
}

PrintPreviewDialog dlg = new PrintPreviewDialog() ;
dlg.Document = pd;
dlg.ShowDialog();

} finally {
streamToPrint.Close() ;
}

} catch(Exception ex) {
MessageBox.Show("An error occurred attempting to preview the file to print - " + ex.Message);
}

}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container ();
this.printPreviewButton = new System.Windows.Forms.Button ();
this.pageSetupButton = new System.Windows.Forms.Button ();
this.printButton = new System.Windows.Forms.Button ();
this.Text = "Print Example 5";
this.AutoScaleBaseSize = new System.Drawing.Size (5, 13);
this.ClientSize = new System.Drawing.Size (504, 381);
printPreviewButton.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
printPreviewButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
printPreviewButton.Size = new System.Drawing.Size (136, 40);
printPreviewButton.TabIndex = 0;
printPreviewButton.Location = new System.Drawing.Point (32, 210);
printPreviewButton.Text = "Print Preview";
pageSetupButton.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
pageSetupButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
pageSetupButton.Size = new System.Drawing.Size (136, 40);
pageSetupButton.TabIndex = 0;
pageSetupButton.Location = new System.Drawing.Point (32, 160);
pageSetupButton.Text = "Page Settings";
printButton.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
printButton.Size = new System.Drawing.Size (136, 40);
printButton.TabIndex = 0;
printButton.Location = new System.Drawing.Point (32, 112);
printButton.Text = "Print the file";
this.Controls.Add (this.printButton);
this.Controls.Add (this.pageSetupButton);
this.Controls.Add (this.printPreviewButton);
}

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
public static void Main(string[] args) {
Application.Run(new PrintForm());
}

}
}


110,525

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧