如何在C# WinFrom中定义一个二维或者三维数组,然后进行分页打印和预览,主要实现功能就是打印和预览的具体功能?
解决方案 »
- winform点击按钮弹出网页
- Excel替换字符串问题
- C# 两个窗体传值,不管From2是否打开,窗体From1必须传值给From2,怎么实现呢?
- winform子窗体无法移动
- [Winform]截取鼠标附近的图片
- asp.net怎么才能在服务器上面读取其他网站的text文件,并取得文件中的数据呢
- richtextbox与Dategridview的结合
- 各人高人来看看,确实很急!!!!!!!关于主界面和DLL窗体的问题
- Remoting 问题!
- 有如下提示是什么原因:当我打开一个解决方案时提示:web服务器运行的不是asp.net1.1版本.
- 分不多,先小散50分(别嫌少),顺便帮忙回答一个非技术的问题吧.
- 送50分,请教有实际工作经验的高手如何解决水晶报表初始化速度慢的问题?
{
private System.ComponentModel.Container components;
private System.Windows.Forms.Button printButton;
private Font printFont;
private StreamReader streamToPrint; public PrintingExample() : base()
{
// The Windows Forms Designer requires the following call.
InitializeComponent();
} // The Click event is raised when the user clicks the Print button.
private void printButton_Click(object sender, EventArgs e)
{
try
{
streamToPrint = new StreamReader
("C:\\My Documents\\MyFile.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);
}
} // The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = null; // Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics); // Print each line of the file.
while(count < linesPerPage &&
((line=streamToPrint.ReadLine()) != null))
{
yPos = topMargin + (count *
printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(line, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
} // If more lines exist, print another page.
if(line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
// The Windows Forms Designer requires the following procedure.
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.printButton = new System.Windows.Forms.Button(); this.ClientSize = new System.Drawing.Size(504, 381);
this.Text = "Print Example"; printButton.ImageAlign =
System.Drawing.ContentAlignment.MiddleLeft;
printButton.Location = new System.Drawing.Point(32, 110);
printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
printButton.TabIndex = 0;
printButton.Text = "Print the file.";
printButton.Size = new System.Drawing.Size(136, 40);
printButton.Click += new System.EventHandler(printButton_Click); this.Controls.Add(printButton);
} // This is the main entry point for the application.
public static void Main(string[] args)
{
Application.Run(new PrintingExample());
}
}