简体   繁体   English

如何将溢出的文本写到图片框的下一行?

[英]How can I write overflowed text to the next line in picturebox?

I am painting to PictureBox but the problem is my painting (text) overflow from picture box. 我正在向PictureBox绘画,但问题是我的绘画(文本)从图片框溢出。 How can I write to the next line? 如何写到下一行?

    private string idbul(string gelenid)
    {
        string id = gelenid;
        string[] malzeme = id.Split(' ');
        string mal_id = malzeme[0];
        mal_id = mal_id.Replace(" ", "");
        return mal_id;
    }
    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        using (Font myFont = new Font("Arial", 8))
        {
            string id = idbul(comboBox1.Text);
            string tanim = tanimbul(comboBox1.Text);
            DateTime now = DateTime.Now;
            string tarih = now.ToString("dd/MM/yyyy");
            e.Graphics.DrawString("SKYLAB TEKNOLOJİ", myFont, Brushes.Black, new Point(2, 145));
            e.Graphics.DrawString("ÜRÜN KODU: " + id, myFont, Brushes.Black, new Point(2, 160));
            e.Graphics.DrawString("Tanım : " + tanim, myFont, Brushes.Black, new Point(2, 175));
            e.Graphics.DrawString("Tarih : "+tarih, myFont, Brushes.Black, new Point(2, 190));

        }
    }
    private string tanimbul(string p)
    {
        string id = p;
        string[] malzeme = id.Split(' ');
        malzeme[0] = "";
        string mal_id = String.Join(" ", malzeme);
        return mal_id;
    }

The string variable "tanim" can be long text so it is overflowing. 字符串变量“ tanim”可以是长文本,因此会溢出。 From the screenshot, you can see the problem. 从屏幕截图中,您可以看到问题。

Screenshot: 截图:

在此处输入图片说明

In DrawString you can specify the bounding rectangle (kind of like margins) see here 在DrawString中,您可以指定边界矩形(类似边距),请参见此处

edit* I looked into it more, the word wrapping only happens for actual words, as in it won't wrap in the middle of a word, only at the end of a word and before the start of the next word. 编辑*我研究了更多的内容,自动换行仅针对实际的单词,因为它不会自动换行到单词的中间,仅在单词的末尾和下一个单词的开始之前。

You can also try getting the length of the tanim (tanim.Length) and writing a separate DrawString if its longer than what will fit into your box. 您还可以尝试获取tanim的长度(tanim.Length),并编写一个单独的DrawString(如果它的长度超过您的盒子的长度)。

something like this: 像这样的东西:

if(tanim.Length>x)
{
    drawstring("tanim : "+tanim.substring(0,x),font,brush,firstlinestart);
    drawstring(tanim.substring(x),font,brush,secondlinestart);
}

where x is the number of characters you can have fit onto the first line. 其中x是您可以在第一行中容纳的字符数。

1.Create a dummy label with visible = false`; 1,创建一个带有visible = false`的虚拟标签;

  1. use the function to resize the font 使用功能调整字体大小

     private float scaleFont(Label lab, string txt) { lab.Text = txt; var width = TextRenderer.MeasureText(lab.Text, lab.Font).Width; while (this.Bounds.Width < width) { using (var font = new Font(lab.Font.FontFamily, lab.Font.Size - 0.5f, lab.Font.Style)) { lab.Font = font; width = TextRenderer.MeasureText(lab.Text, font).Width; } } return lab.Font.Size; } 
  2. use it like so 像这样使用

    String drawString = "your string here"; String drawString =“您的字符串在这里”;
    var fontSize = scaleFont(label1, drawString); var fontSize = scaleFont(label1,drawString); using(Font drawFont = new Font("Arial", fontSize )) { //rest of your code } using(Font drawFont = new Font(“ Arial”,fontSize)){//代码的其余部分}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM