[英]c# new line printing out a String
我如何打印出整個字符串? 我想知道如何進行換行打印此字符串?
謝謝你的回答
private void drucken_Click(object sender, EventArgs e)
{
PrintDialog printDialog = new PrintDialog();
PrintDocument printDocument = new PrintDocument();
printDialog.Document = printDocument;
printDocument.PrintPage += new PrintPageEventHandler (PrintReceiptPage);
DialogResult result = printDialog.ShowDialog();
if (result == DialogResult.OK)
{
printDocument.Print();
}
}
private void PrintReceiptPage(object sender, PrintPageEventArgs e)
{
string message = "Haalloodhsak dfsdfsfdsfsdfdssdddddddddddddddsf Tetsn dfgdfgdfgdfg dfgdfgdfgdfg dfgdfgdfgdfggdfdfg gdgdgdffgddfgdfgdfggdf dfgdfgdfgdfggdf";
int y;
// Print receipt
Font myFont = new Font("Times New Roman", 15, FontStyle.Bold);
y = e.MarginBounds.Y;
e.Graphics.DrawString(message , myFont, Brushes.Red, e.MarginBounds.X, y);
}
將您的PrintReceiptPage方法更改為如下所示:
private void PrintReceiptPage(object sender, PrintPageEventArgs e)
{
string message = "Haalloodhsak dfsdfsfdsfsdfdssdddddddddddddddsf Tetsn dfgdfgdfgdfg dfgdfgdfgdfg dfgdfgdfgdfggdfdfg gdgdgdffgddfgdfgdfggdf dfgdfgdfgdfggdf";
int yMargin;
// Print receipt
Font myFont = new Font("Times New Roman", 15, FontStyle.Bold);
yMargin = e.MarginBounds.Y;
// Create rectangle for drawing.
float x = 150.0F;
float y = 150.0F;
float width = 200.0F;
float height = 50.0F;
RectangleF drawRect = new RectangleF(x, yMargin, width, height);
e.Graphics.DrawString(message, myFont, Brushes.Red, drawRect);
}
我認為沒有任何方法可以自動包裝文本。 您可以做的一件事是測量字符串的一部分,如果有足夠的空間,則添加字符。 如果沒有足夠的空間,請打印並向下移動一點。
要測量字符串,請使用e.Graphics.MeasureString(string,Font)
。 希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.