c#新行输出字符串

本文关键字:字符串 输出 新行 | 更新日期: 2023-09-27 18:07:16

如何打印出整个字符串?我想知道我如何做一个新的行打印出这个字符串?

谢谢你的回答

    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);
    }

c#新行输出字符串

将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)