主题:[讨论]C#中打印一行内容过长时有错误?
本人最近学习C#中的打印功能,使用PrintDocument对象的PintPage事件这样写的。
C# code
void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
//StringReader lineReader = new StringReader(textBox.Text);
Graphics graphic = e.Graphics;//获取绘图对象
float linesPerPage = 0;//页面行号
float yPosition = 0;//绘制字符串的纵向位置
float leftMargin = e.MarginBounds.Left;//左边距
float topMargin = e.MarginBounds.Top;//上边距
string line = string.Empty;//读取的行字符串
int currentPageLine=0;//当前页读取的行数
Font charFont = richTextBox1.Font;//获取打印字体
SolidBrush brush = new SolidBrush(Color.Black);//刷子
linesPerPage = e.MarginBounds.Height / charFont.GetHeight(graphic);//每页可打印的行数
//MessageBox.Show(richTextBox1.Lines[0].ToString());
//if (richTextBox1.Lines.Length == 0)
// MessageBox.Show("无任何内容!");
//else
//{
while (countNum < richTextBox1.Lines.Length)
{
if (currentPageLine < linesPerPage)
{
line = richTextBox1.Lines[countNum].ToString();
yPosition = topMargin + (currentPageLine * charFont.GetHeight(graphic));
//绘制当前行
graphic.DrawString(line, charFont, brush, leftMargin, yPosition, new StringFormat());
countNum++;
currentPageLine++;
}
else
{
line = null;
break;
}
}
//}
if (line == null)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
if (countNum >= richTextBox1.Lines.Length)
{
countNum = 0;
}
}
打印预览的代码如下:
C# code
PrintPreviewDialog printPreview = new PrintPreviewDialog();
this.PrintDocument();
printPreview.Document = printDocument;
try
{
printPreview.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
countNum是一个静态整形变量
但是发现有个问题,就是在读文本文件的时候如果一行内容过长的时候在打印预览里会丢失长出来的文字,请问怎么解决?
------------
问题2:
我在solution中加了一个aboutbox的form
我希望每次我点击菜单项中的"关于"后, 弹出自己修改过的aboutbox
但是总是弹出系统默认的那个, 怎么回事?
C# code
void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
//StringReader lineReader = new StringReader(textBox.Text);
Graphics graphic = e.Graphics;//获取绘图对象
float linesPerPage = 0;//页面行号
float yPosition = 0;//绘制字符串的纵向位置
float leftMargin = e.MarginBounds.Left;//左边距
float topMargin = e.MarginBounds.Top;//上边距
string line = string.Empty;//读取的行字符串
int currentPageLine=0;//当前页读取的行数
Font charFont = richTextBox1.Font;//获取打印字体
SolidBrush brush = new SolidBrush(Color.Black);//刷子
linesPerPage = e.MarginBounds.Height / charFont.GetHeight(graphic);//每页可打印的行数
//MessageBox.Show(richTextBox1.Lines[0].ToString());
//if (richTextBox1.Lines.Length == 0)
// MessageBox.Show("无任何内容!");
//else
//{
while (countNum < richTextBox1.Lines.Length)
{
if (currentPageLine < linesPerPage)
{
line = richTextBox1.Lines[countNum].ToString();
yPosition = topMargin + (currentPageLine * charFont.GetHeight(graphic));
//绘制当前行
graphic.DrawString(line, charFont, brush, leftMargin, yPosition, new StringFormat());
countNum++;
currentPageLine++;
}
else
{
line = null;
break;
}
}
//}
if (line == null)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
if (countNum >= richTextBox1.Lines.Length)
{
countNum = 0;
}
}
打印预览的代码如下:
C# code
PrintPreviewDialog printPreview = new PrintPreviewDialog();
this.PrintDocument();
printPreview.Document = printDocument;
try
{
printPreview.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
countNum是一个静态整形变量
但是发现有个问题,就是在读文本文件的时候如果一行内容过长的时候在打印预览里会丢失长出来的文字,请问怎么解决?
------------
问题2:
我在solution中加了一个aboutbox的form
我希望每次我点击菜单项中的"关于"后, 弹出自己修改过的aboutbox
但是总是弹出系统默认的那个, 怎么回事?