有一个程序应该只写出论文中的页数 假设计数= 50
但是当我编写代码时,它会迅速而且永远不会停止。
这是代码:
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
RectangleF yek1;
while(count >= 0)
{
e.Graphics.DrawString("12345678901234567890", TitleFont, BlackBrush,yek,TitleStingFormat);
yek1 = new Rectangle(10, 10, drawWidth, drawHeight);
e.Graphics.DrawString(count.toString(), TitleFont, BlackBrush, yek1, TitleStingFormat);
count --;
e.HasMorePages = true;
}
e.HasMorePages = false;
}
谢谢,如果有人可以帮助我。
我改变了上面的代码。但它只是打印" 50"这是反击的第一个值。
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
// Page Size
int pageWidth = 830;
int pageHeight = 1170;
int BorderWidth, BorderHeight, BorderX, BorderY, drawHeight, drawWidth, drawY1, drawY2, drawX1, drawX2;
Color BorderColor;
float BorderLineWidth;
Font TitleFont;
Brush BlackBrush, whiteBrush;
StringFormat TitleStingFormat = new StringFormat();
// Border
BorderWidth = pageWidth - 40;
BorderHeight = pageHeight - 20;
BorderX = 30;
BorderY = 25;
BorderColor = Color.Gray;
BorderLineWidth = 2;
drawHeight = 30;
drawWidth = 400;
drawY1 = 400;
drawY2 = 985;
drawX1 = 100;
drawX2 = 513;
int xc = 0;
int counter = 10;
TitleFont = textBox1.Font;
BlackBrush = Brushes.Black;
whiteBrush = Brushes.White;
TitleStingFormat = new StringFormat();
TitleStingFormat.Alignment = StringAlignment.Near;
// draw border
Pen BorderPen = new Pen(BorderColor, BorderLineWidth);
Pen linePen = new Pen(BorderColor, 1);
linePen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
//Rectangles for clearing the paper
/* RectangleF one = new Rectangle(BorderX, BorderY, BorderWidth, drawY * 9 + 8);
RectangleF two = new Rectangle(BorderX, BorderY + 270, BorderWidth, drawY * 9 + 8);
RectangleF three = new Rectangle(BorderX, BorderY + 540, BorderWidth, drawY * 9 + 8);
RectangleF four = new Rectangle(BorderX, BorderY + 810, BorderWidth, drawY * 9 + 5);
RectangleF yek = new Rectangle(drawX1, drawY1, drawWidth, drawHeight);
RectangleF dow = new Rectangle(drawX2, drawY1, drawWidth, drawHeight);
RectangleF se = new Rectangle(drawX1, drawY2, drawWidth, drawHeight);
RectangleF chehar = new Rectangle(drawX2, drawY2, drawWidth, drawHeight);
*/
//Draw
RectangleF yek1;
RectangleF dow1;
RectangleF se1;
RectangleF chehar1;
while(counter >= 0)
{
/* e.Graphics.DrawString("12345678901234567890", TitleFont, BlackBrush, yek, TitleStingFormat);
e.Graphics.DrawString("12345678901234567890", TitleFont, BlackBrush, dow, TitleStingFormat);
e.Graphics.DrawString("12345678901234567890", TitleFont, BlackBrush, se, TitleStingFormat);
e.Graphics.DrawString("12345678901234567890", TitleFont, BlackBrush, chehar, TitleStingFormat);
*/
yek1 = new Rectangle(drawX1, 10 +(xc * 830), drawWidth, drawHeight);
// dow1 = new Rectangle(drawX2, drawY1+ (xc * 830), drawWidth, drawHeight);
// se1 = new Rectangle(drawX1, drawY2+(xc * 830), drawWidth, drawHeight);
// chehar1 = new Rectangle(drawX2, drawY2+(xc * 830), drawWidth, drawHeight);
e.Graphics.DrawString(counter.ToString(), TitleFont, BlackBrush, yek1, TitleStingFormat);
// e.Graphics.DrawString("12345678901234567890", TitleFont, BlackBrush, dow1, TitleStingFormat);
// e.Graphics.DrawString("12345678901234567890", TitleFont, BlackBrush, se1, TitleStingFormat);
// e.Graphics.DrawString("12345678901234567890", TitleFont, BlackBrush, chehar1, TitleStingFormat);
// counter--;
counter--;
xc++;
e.HasMorePages = true;
// MessageBox.Show(xc.ToString());
}
e.HasMorePages = false;
}
答案 0 :(得分:1)
你做错了。它正在发生的是打印50页,然后打破你的循环。在它退出循环后,e.HasMorePages
仍设置为true
。那么你的应用程序就像“哦!还有更多的页面。让我们打印那些”。所以它再次运行PrintPage
事件,它会冲洗并重复。
在您突破e.HasMorePages
循环后将false
属性设置为while
,它应该可以解决您的问题。
答案 1 :(得分:0)
此代码看起来会在单个页面上编写循环的所有迭代,然后再次为下一页执行此操作。你可能想要取出循环:
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
RectangleF yek1;
// Change "while" to "if" here
if(count >= 0)
{
e.Graphics.DrawString("12345678901234567890", TitleFont, BlackBrush,yek,TitleStingFormat);
yek1 = new Rectangle(drawX1, drawY1 + (xc * 830), drawWidth, drawHeight);
e.Graphics.DrawString(xc.ToString(), TitleFont, BlackBrush, yek1, TitleStingFormat);
count --;
// Set HasMorePages depending on the value of count
e.HasMorePages = (count >= 0);
return;
}
// No more pages
e.HasMorePages = false;
}