200分求个打印示例程序

hgknight 2003-03-17 12:17:03
在页面上用DrawEllipse、DrawString等涂了点鸦,想实现打印
哪位兄弟能给个具备页面设置及打印预览功能的示例程序
...全文
62 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
angel_lee 2003-03-17
  • 打赏
  • 举报
回复
study:)
ssoonzhang 2003-03-17
  • 打赏
  • 举报
回复
很好用的,关键是DrawGraphics(),自己想要打印的东西,怎样画都可以
private System.Drawing.Printing.PrintDocument printDoc;
//部分函数
//打印
public bool Print()
{
try
{
PrintDialog printDlg = new PrintDialog();
//printDlg - 打印对话框
printDlg.Document = printDoc;
DialogResult result = printDlg.ShowDialog();
if (result == DialogResult.OK)
{
printDoc.DocumentName = "单据打印测试";
printDoc.Print();
}
else
return false;
printDlg.Dispose();
printDlg = null;
return true;
}
catch(Exception ep)
{
MessageBox.Show(ep.ToString(),"msg");
return false;
}
}

//事件
private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.PageUnit = GraphicsUnit.Display;
//DrawGraphics - 自己的画图函数
DrawGraphics(0,0,e.Graphics);
}
QQ86087516 2003-03-17
  • 打赏
  • 举报
回复
在.net 光盘上(好像是第二张)上找vc#示例,general示例部分有个Scrible的画图程序
其中有实现打印的代码,你可以参考一下
cnnbsun 2003-03-17
  • 打赏
  • 举报
回复
namespace Microsoft.Samples.WinForms.Cs.PrintingExample1 {
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;

public class PrintingExample1 : System.Windows.Forms.Form {
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components;
protected internal System.Windows.Forms.Button printButton;

private Font printFont;
private StreamReader streamToPrint;


public PrintingExample1() {

//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

// 启动打印按钮的事件处理程序
printButton.Click += new System.EventHandler(printButton_Click);
}

/// <summary>
/// 清理正在使用的所有资源。
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}

//在用户按下打印按钮时激发的事件
private void printButton_Click(object sender, EventArgs e) {
try {

streamToPrint = new StreamReader ("PrintMe.Txt");
try {
printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument(); //假定为默认打印机
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.Print();
} finally {
streamToPrint.Close() ;
}

} catch(Exception ex) {
MessageBox.Show("打印文件时发生错误 - " + ex.Message);
}
}

//每个要打印的页所激发的事件
private void pd_PrintPage(object sender, PrintPageEventArgs ev) {
float lpp = 0 ;
float yPos = 0 ;
int count = 0 ;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
String line=null;

//算出每页的行数
//在事件上使用 MarginBounds 以达到此目的
lpp = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics) ;

//现在,在文件上重复此操作以输出每行
//注意:假设单行比页宽窄
//首先检查行数,以便看不到不打印的行
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 (line != null)
ev.HasMorePages = true ;
else
ev.HasMorePages = false ;
}


/// <summary>
/// 设计器支持所需的方法 - 不要使用
/// 代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container ();
this.printButton = new System.Windows.Forms.Button ();
this.Text = "打印示例 1";
this.AutoScaleBaseSize = new System.Drawing.Size (5, 13);
this.ClientSize = new System.Drawing.Size (504, 381);
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 = "打印文件";
this.Controls.Add (this.printButton);
}

/// <summary>
/// 应用程序的主要入口点。
/// </summary>
[STAThread]
public static void Main(string[] args) {
Application.Run(new PrintingExample1());
}

}
}

110,499

社区成员

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

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

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