TextRenderer doesn't draw a long string

痴心易碎 提交于 2019-12-02 02:26:03

问题


Look at this sample:

public partial class Form1 : Form
{
    private static string myString = null;

    private const int MAX_TEXT = 5460;

    public Form1()
    {
        InitializeComponent();

        StringBuilder builder = new StringBuilder();

        for (int i = 0; i < MAX_TEXT; i++)
        {
            builder.Append('a');
        }

        myString = builder.ToString();

        this.Paint += Form1_Paint;
    }

    void Form1_Paint(object sender, PaintEventArgs e)
    {
        TextRenderer.DrawText(
            e.Graphics,
            myString,
            this.Font,
            new Point(10, 30),
            Color.Black);
    }
}

When I set the MAX_TEXT to 5461, the string is not drawn. Do you know if the native mechanism has a limit to draw text, if/or I can setup the options to get it working?


回答1:


I think you hit the limitation of the TextRenderer class, which I think is calling the DrawTextEx API function under the hood. If you try to put your builder.ToString() results into a TextBox, it won't show up either.

If for some reason you need to print a string that long, you would have to revert back to the DrawString method:

e.Graphics.DrawString(myString, this.Font, Brushes.Black, new Point(10, 30));


来源:https://stackoverflow.com/questions/17525640/textrenderer-doesnt-draw-a-long-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!