如何定制C#中的打印预览 如题,C#中的printpreviewdialog预览时只能显示几个工具和简单按钮,如何对这几个按钮进行修改,或加入几个自己做的新按钮,或减少,如何实现,最好有代码小生先谢谢了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Drawing.Printing; using System.Windows.Forms; using System.IO; namespace MyCCTV { sealed class TextFilePrinter { string fileName; Encoding theEncode; Font theFont; StreamReader srToPrint; int currPage; public TextFilePrinter(string fileName) : this(fileName, Encoding.GetEncoding(936), new Font("MS UI Gothic", 9)) { } public TextFilePrinter(string fileName, Encoding theEncode, Font theFont) { this.fileName = fileName; this.theEncode = theEncode; this.theFont = theFont; } public void Print() { using (srToPrint = new StreamReader(fileName, theEncode)) { PrintDialog dlg = new PrintDialog(); dlg.Document = GetPrintDocument(); dlg.AllowSomePages = true; dlg.AllowPrintToFile = false; if (dlg.ShowDialog() == DialogResult.OK) dlg.Document.Print(); } } public void View() { using (srToPrint = new StreamReader(fileName, theEncode)) { PrintPreviewDialog dlg = new PrintPreviewDialog(); dlg.Document = GetPrintDocument(); dlg.ShowDialog(); } } PrintDocument GetPrintDocument() { currPage = 1; PrintDocument doc = new PrintDocument(); doc.DocumentName = fileName; doc.PrintPage += new PrintPageEventHandler(PrintPageEvent); return doc; } void PrintPageEvent(object sender, PrintPageEventArgs ev) { string line = null; float linesPerPage = ev.MarginBounds.Height / theFont.GetHeight(ev.Graphics); bool isSomePages = ev.PageSettings.PrinterSettings.PrintRange == PrintRange.SomePages; if (isSomePages) { while (currPage < ev.PageSettings.PrinterSettings.FromPage) { for (int count = 0; count < linesPerPage; count++) { line = srToPrint.ReadLine(); if (line == null) break; } if (line == null) return; currPage++; } if (currPage > ev.PageSettings.PrinterSettings.ToPage) return; } for (int count = 0; count < linesPerPage; count++) { line = srToPrint.ReadLine(); if (line == null) break; ev.Graphics.DrawString(line, theFont, Brushes.Black, ev.MarginBounds.Left, ev.MarginBounds.Top + (count * theFont.GetHeight(ev.Graphics)), new StringFormat()); } currPage++; if (isSomePages && currPage > ev.PageSettings.PrinterSettings.ToPage) return; if (line != null) ev.HasMorePages = true; } } } if (textBox1.Text != string.Empty) { TextFilePrinter tfp = new TextFilePrinter(m_strTxtPath); tfp.Print(); } 调用方法。 http://www.jobdll.com/user/gt/log/2274.html 同一命名空间中不同类之间变量的引用问题! c# 大文件上传 请教高手 如何从路径(可能有文件名)中得到路径和文件名? 关于C#数据库附加的 收邮件解码问题?该样解码? 一个关于combobox的与datasource的问题是高手就进来 大家帮我看看,为什么我刚装的.net 用c#建立web项目的时候 项目名那里是灰色的! 如何定做一个数据库公共连接类 请帮我做一道选择题,并说明为什么 请问!在ASP。NET中的网页都是在 Visual Studio 。NET 里完成吗? 上传图片时生成的缩略图变模糊的问题..................... 利用飞信中的虚拟框架打包
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.IO;
namespace MyCCTV
{
sealed class TextFilePrinter
{
string fileName;
Encoding theEncode;
Font theFont;
StreamReader srToPrint;
int currPage;
public TextFilePrinter(string fileName)
: this(fileName, Encoding.GetEncoding(936), new Font("MS UI Gothic", 9))
{
}
public TextFilePrinter(string fileName, Encoding theEncode, Font theFont)
{
this.fileName = fileName;
this.theEncode = theEncode;
this.theFont = theFont;
}
public void Print()
{
using (srToPrint = new StreamReader(fileName, theEncode))
{
PrintDialog dlg = new PrintDialog();
dlg.Document = GetPrintDocument();
dlg.AllowSomePages = true;
dlg.AllowPrintToFile = false;
if (dlg.ShowDialog() == DialogResult.OK) dlg.Document.Print();
}
}
public void View()
{
using (srToPrint = new StreamReader(fileName, theEncode))
{
PrintPreviewDialog dlg = new PrintPreviewDialog();
dlg.Document = GetPrintDocument();
dlg.ShowDialog();
}
}
PrintDocument GetPrintDocument()
{
currPage = 1;
PrintDocument doc = new PrintDocument();
doc.DocumentName = fileName;
doc.PrintPage += new PrintPageEventHandler(PrintPageEvent);
return doc;
}
void PrintPageEvent(object sender, PrintPageEventArgs ev)
{
string line = null;
float linesPerPage = ev.MarginBounds.Height / theFont.GetHeight(ev.Graphics);
bool isSomePages = ev.PageSettings.PrinterSettings.PrintRange == PrintRange.SomePages;
if (isSomePages)
{
while (currPage < ev.PageSettings.PrinterSettings.FromPage)
{
for (int count = 0; count < linesPerPage; count++)
{
line = srToPrint.ReadLine();
if (line == null) break;
}
if (line == null) return;
currPage++;
}
if (currPage > ev.PageSettings.PrinterSettings.ToPage) return;
}
for (int count = 0; count < linesPerPage; count++)
{
line = srToPrint.ReadLine();
if (line == null) break;
ev.Graphics.DrawString(line, theFont, Brushes.Black, ev.MarginBounds.Left,
ev.MarginBounds.Top + (count * theFont.GetHeight(ev.Graphics)), new StringFormat());
}
currPage++;
if (isSomePages && currPage > ev.PageSettings.PrinterSettings.ToPage) return;
if (line != null) ev.HasMorePages = true;
}
}
}
{
TextFilePrinter tfp = new TextFilePrinter(m_strTxtPath);
tfp.Print();
}
调用方法。