c#将文本输出到打印机(WinForm)

heyiwen123 2008-09-23 10:27:40
一个窗体中,一个文本框,一个命令按钮(print),一按命令按钮直接吧文本框里的内容输入到打印机,打印机可以选择.
请付上代码,谢谢.
...全文
1675 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
zsf8764 2008-09-24
  • 打赏
  • 举报
回复
mark
heshanxu000 2008-09-24
  • 打赏
  • 举报
回复
可以直接通过Drawing从新绘图后再打印,如果图片过长还可以将图片旋转后再打印
//调用BITBLT类
[System.Runtime.InteropServices.DllImport("gdi32.dll ")]
public static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
//定义要打印的图片
private Bitmap memoryImage;
private Bitmap newmap;
//将要打印的位置变成图片
private void CaptureScreen()
{
Graphics mygraphics = this.panel1.CreateGraphics();
Size s = this.panel1.Size;
memoryImage = new Bitmap(s.Width,s.Height, mygraphics);
Graphics memoryGraphics =System.Drawing.Graphics.FromImage(memoryImage);
IntPtr dc1 = mygraphics.GetHdc();
IntPtr dc2 = memoryGraphics.GetHdc();
BitBlt(dc2, 0, 0, this.panel1.Width, this.panel1.Height, dc1, 0, 0, 13369376);
mygraphics.ReleaseHdc(dc1);
memoryGraphics.ReleaseHdc(dc2);
}

/// <summary>
/// 打印并预览图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_print_Click(object sender, EventArgs e)
{
button2.Visible = false;
btn_print.Visible = false;
//调用函数
CaptureScreen();
PrintDialog myprint = new PrintDialog();
myprint.Document = printDocument1;
if (myprint.ShowDialog() == DialogResult.OK)
{
try
{
//打印预览
printPreviewDialog1.Show();
//设置打印图片的大小为Letter
System.Drawing.Printing.PaperSize ps = null;
foreach (System.Drawing.Printing.PaperSize p in printDocument1.PrinterSettings.PaperSizes)
{
if (p.PaperName.Equals("Letter"))
ps = p;
}
//打印
printDocument1.Print();
}
tianjinldl 2008-09-24
  • 打赏
  • 举报
回复
上面的 path为文件路径
zt_100094 2008-09-24
  • 打赏
  • 举报
回复
我的BLOG有源码
tianjinldl 2008-09-24
  • 打赏
  • 举报
回复
我现在程序在用的打印代码
方法和打印事件外面声明
//创建一个PrintDocument的实例
System.Drawing.Printing.PrintDocument docToPrint = new System.Drawing.Printing.PrintDocument();

System.IO.Stream streamToPrint;
string streamType = "txt";



//打印按钮里面
streamToPrint = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);

// 创建一个PrintDialog的实例。
System.Windows.Forms.PrintDialog PrintDialog1 = new PrintDialog();
PrintDialog1.AllowSomePages = true;
PrintDialog1.ShowHelp = true;

// 把PrintDialog的Document属性设为上面配置好的PrintDocument的实例
PrintDialog1.Document = docToPrint;
this.docToPrint.PrintPage += new PrintPageEventHandler(docToPrint_PrintPage);

// 调用PrintDialog的ShowDialog函数显示打印对话框
DialogResult result = PrintDialog1.ShowDialog();

if (result == DialogResult.OK)
{
// 开始打印
docToPrint.Print();
}

//设置打印机开始打印的事件处理函数
private void docToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
switch (this.streamType)
{
case "txt":
string text = null;

// 信息头
string strTou = string.Empty;
System.Drawing.Font printFont = new System.Drawing.Font
("Arial", 8, System.Drawing.FontStyle.Regular);
System.Drawing.Font printFont1 = new System.Drawing.Font
("Arial", 11, System.Drawing.FontStyle.Regular);
System.IO.StreamReader streamReader = new StreamReader(this.streamToPrint);
text = streamReader.ReadToEnd();

// 获取信息头
strTou = text.Substring(0, 20);

//信息其他部分
text = text.Substring(20, (text.Length - 20));

// 设置信息打印格式
e.Graphics.DrawString(strTou, printFont1, System.Drawing.Brushes.Black, 5, 5);
e.Graphics.DrawString(text, printFont, System.Drawing.Brushes.Black, 10, 5);
break;
case "image":
System.Drawing.Image image = System.Drawing.Image.FromStream(this.streamToPrint);
int x = e.MarginBounds.X;
int y = e.MarginBounds.Y;
int width = image.Width;
int height = image.Height;
if ((width / e.MarginBounds.Width) > (height / e.MarginBounds.Height))
{
width = e.MarginBounds.Width;
height = image.Height * e.MarginBounds.Width / image.Width;
}
else
{
height = e.MarginBounds.Height;
width = image.Width * e.MarginBounds.Height / image.Height;
}
System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(x, y, width, height);
e.Graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, System.Drawing.GraphicsUnit.Pixel);
break;
default:
break;
}

}
paulin 2008-09-24
  • 打赏
  • 举报
回复
楼主看看下面的文章,应该跟你的要求比较一致
http://industry.ccidnet.com/art/1136/20020328/11405_1.html
gh_li 2008-09-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wuyi8808 的回复:]
C# code// 先把文本框的内容写到文本文件,然后:
System.Diagnostics.Process.Start("notepad", "/p " + 文本文件名);
[/Quote]
学习了,
baby_shark 2008-09-24
  • 打赏
  • 举报
回复
学习学习,谢谢了
cscheat 2008-09-24
  • 打赏
  • 举报
回复
路过,听说佳能又有新品上市了哦,叫CP770来着,如果我要买是不是直接去中关村就可以了?
wuyi8808 2008-09-23
  • 打赏
  • 举报
回复
// 先把文本框的内容写到文本文件,然后:
System.Diagnostics.Process.Start("notepad", "/p " + 文本文件名);
qqlpp 2008-09-23
  • 打赏
  • 举报
回复
LS搜索的东西很有用 说的比较清楚了
销魂的拖拉机 2008-09-23
  • 打赏
  • 举报
回复
在windows应用程序中文档的打印是一项非常重要的功能,在以前一直是一个非常复杂的工作,Microsoft .net Framework的打
印功能都以组件的方式提供,为程序员提供了很大的方便,但是这几个组件的使用还是很复杂的,有必要解释一下。
打印操作通常包括以下四个功能
1 打印设置 设置打印机的一些参数比如更改打印机驱动程序等
2 页面设置 设置页面大小纸张类型等
3 打印预览 类似于word中的打印预览
4 打印

下面我把我编写的记事本(全部源代码可以在http://www.cndot.net中下载)中用到的打印功能的代码进行解释希望能给大家一些帮助
实现打印功能的核心是PrintDocument类这个类属于System.Drawing.Printing名字空间这个类封装了当前的打印设置页面设置以及所
有的与打印有关的事件和方法
这个类包括以下几个属性 事件 和方法
1、PrinterSettings 属性
存放打印机的设置信息这个属性不需要程序员设置因为它是由打印对话框获取的
2、PrintCountroller 属性
控制打印过程
3、DefaultPageSettings 属性
存放页面设置信息 打印纸大小方向等也不需要程序员设置因为它是由页面设置对话框获取的
4、DocumentName 属性
指定文档名称,出现在打印机状态窗口中
1。 BeginPrint事件
在打印之前发出
2. PrintPage事件
每打印一页是发出,事件接受一个PrintPageEventArgs参数该参数封装了打印相关的信息
PrintPageEventArgs参数有很多重要的属性
1 Cancel 取消打印
2 Graphics 页面的绘图对象
3 HasMorePages 是否还有要打印的页面
Print 方法 该方法没有参数 调用它将按照当前设置开始打印
若实现打印功能首先构造PrintDocument对象添加打印事件
PrintDocument printDocument;
private void InitializeComponent()
{
...
printDocument=new PrintDocument();
printDocument.PrintPage += new PrintPageEventHandler (this.printDocument_PrintPage);
...
}
实现打印事件功能
打印和绘图类似都是调用Graphics 类的方法进行画图 不同的是一个在显示器上一个在打印纸上并且打印要进行一些复杂的计算
如换行 分页等。
private void printDocument_PrintPage(object sender,PrintPageEventArgs e)
{
StringReader lineReader = new StringReader(textBox.Text);
Graphics g = e.Graphics; //获得绘图对象
float linesPerPage = 0; //页面的行号
float yPosition = 0; //绘制字符串的纵向位置
int count = 0; //行计数器
float leftMargin = e.MarginBounds.Left; //左边距
float topMargin = e.MarginBounds.Top; //上边距
string line = null; 行字符串
Font printFont = this.textBox.Font; //当前的打印字体
SolidBrush myBrush = new SolidBrush(Color.Black);//刷子
linesPerPage = e.MarginBounds.Height / printFont.GetHeight(g);//每页可打印的行数
//逐行的循环打印一页
while(count < linesPerPage && ((line=lineReader.ReadLine()) != null))
{
yPosition = topMargin + (count * printFont.GetHeight(g));
g.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
count++;
}
如果本页打印完成而line不为空说明还有没完成的页面这将触发下一次的打印事件在下一次的打印中lineReader会
自动读取上次没有打印完的内容因为lineReader是这个打印方法外的类的成员它可以记录当前读取的位置
if(line != null)
e.HasMorePages = true;
else
e.HasMorePages = false;
}
打印设置,构造打印对话框 将对话框中设置的Document属性赋给printDocument这样会将用户的设置自动保存到printDocument
的PrinterSettings属性中
protected void FileMenuItem_PrintSet_Click(object sender,EventArgs e)
{
PrintDialog printDialog = new PrintDialog();
printDialog.Document = printDocument;
printDialog.ShowDialog();
}
页面设置和打印预览与打印设置原理相同都是构造对话框将用户在对话框中的设置保存到相应的类的属性中
protected void FileMenuItem_PageSet_Click(object sender,EventArgs e)
{
PageSetupDialog pageSetupDialog = new PageSetupDialog();
pageSetupDialog.Document = printDocument;
pageSetupDialog.ShowDialog();
}
打印预览
protected void FileMenuItem_PrintView_Click(object sender,EventArgs e)
{
PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
printPreviewDialog.Document = printDocument;
try
{
printPreviewDialog.ShowDialog();
}
catch(Exception excep)
{
MessageBox.Show(excep.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
打印就可以直接调用printDocument的Print()方法因为用户可能在打印之前还要再更改打印设置所以
在这里再次显示打印设置对话框
protected void FileMenuItem_Print_Click(object sender,EventArgs e)
{
PrintDialog printDialog = new PrintDialog();
printDialog.Document = printDocument;
lineReader = new StringReader(textBox.Text);
if (printDialog.ShowDialog() == DialogResult.OK)
{
try
{
printDocument.Print();
}
catch(Exception excep)
{
MessageBox.Show(excep.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
printDocument.PrintController.OnEndPrint(printDocument,new PrintEventArgs());
}
}
}
总结打印的过程是
1 在应用程序窗体初始化时构造PrintDocument对象 添加 printDocument 的 PrintPage 方法
2 实现PrintPage方法 4 在用户的单击事件中调用 printDocument 的 Print方法实现打印功能
在这中间可能要用到 PrintDialog PrintPreviewDialog PageSetupDialog 设置和查看打印效
-----------------------
帮你GOOGLE了一下
heyiwen123 2008-09-23
  • 打赏
  • 举报
回复
自己顶一下
----------Database-------------- 1.DataTable帮助类(DataTableHelper.cs) 2.Access数据库文件操作辅助类(JetAccessUtil.cs) 5.查询条件组合辅助类(SearchCondition.cs) 6.查询信息实体类(SearchInfo.cs) 8.Sql命令操作函数(可用于安装程序的时候数据库脚本执行)(SqlScriptHelper.cs) ----------Device-------------- 声音播放辅助类(AudioHelper.cs) 摄像头操作辅助类,包括开启、关闭、抓图、设置等功能(Camera.cs) 提供用于操作【剪切板】的方法(ClipboardHelper.cs) 获取电脑信息(Computer.cs) 提供用户硬件唯一信息的辅助类(FingerprintHelper.cs) 读取指定盘符的硬盘序列号(HardwareInfoHelper.cs) 提供访问键盘当前状态的属性(KeyboardHelper.cs) 全局键盘钩子。这可以用来在全球范围内捕捉键盘输入。(KeyboardHook.cs) 模拟鼠标点 击(MouseHelper.cs) 全局鼠标钩子。这可以用来在全球范围内捕获鼠标输入。(MouseHook.cs) MP3文件播放操作辅助类(MP3Helper.cs) 关联文件(ExtensionAttachUtil.cs) 注册文件关联的辅助类(FileAssociationsHelper.cs) 打开、保存文件对话框操作辅助类(FileDialogHelper.cs) 常用的文件操作辅助类FileUtil(FileUtil.cs) INI文件操作辅助类(INIFileUtil.cs) 独立存储操作辅助类(IsolatedStorageHelper.cs) 序列号操作辅助类(Serializer.cs) 获取一个对象,它提供用于访问经常引用的目录的属性。(SpecialDirectories.cs) 简单的Word操作对象(WordCombineUtil.cs) 这个类提供了一些实用的方法来转换XML和对象。(XmlConvertor.cs) XML操作类(XmlHelper.cs) ----------Format-------------- 参数验证的通用验证程序。(ArgumentValidation.cs) 这个类提供了实用方法的字节数组和图像之间的转换。(ByteImageConvertor.cs) byte字节数组操作辅助类(BytesTools.cs) 处理数据类型转换,数制转换、编码转换相关的类(ConvertHelper.cs) CRC校验辅助类(CRCUtils.cs) 枚举操作公共类(EnumHelper.cs) 身份证操作辅助类(IDCardHelper.cs) 检测字符编码的类(IdentifyEncoding.cs) RGB颜色操作辅助类(MyColors.cs) 日期操作类(MyDateTime.cs) 转换人民币大小金额辅助类(RMBUtil.cs) 常用的字符串常量(StringConstants.cs) 简要说明TextHelper。(StringUtil.cs) 获取中文字首字拼写,随机发生器,按指定概率随机执行操作(Util.cs) 各种输入格式验证辅助类(ValidateUtil.cs) ----------Network-------------- Cookie操作辅助类(CookieManger.cs) FTP操作辅助类(FTPHelper.cs) HTML操作类(HttpHelper.cs) 网页抓取帮助(HttpWebRequestHelper.cs) Net(NetworkUtil.cs) IE代理设置辅助类(ProxyHelper.cs) ----------Winform-------------- 跨线程的控件安全访问方式(CallCtrlWithThreadSafety.cs) CheckBoxList(CheckBoxListUtil.cs) 窗口管理类(ChildWinManagement.cs) 由马丁·米勒http://msdn.microsoft.com/en-us/library/ms996492.aspx提供一个简单的方法打印工作的一个RichTextBox一个帮手(ExRichTextBoxPrintHelper.cs) 显示,隐藏或关闭动画形式。(FormAnimator.cs) 对窗体进行冻结、解冻操作辅助类(FreezeWindowUtil.cs) 窗体全屏操作辅助类(Ful

110,545

社区成员

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

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

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