Draw multiline text: auto wrap : Text « 2D Graphics « C# / C Sharp






Draw multiline text: auto wrap

Draw multiline text: auto wrap

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class Form1 : System.Windows.Forms.Form{
  public Form1()
  {
        InitializeComponent();
        SetStyle(ControlStyles.Opaque, true);
        Bounds = new Rectangle(0, 0, 500, 300);
  }

    protected override void OnPaint(PaintEventArgs e) {
         Graphics g = e.Graphics;
   
         g.FillRectangle(Brushes.White, ClientRectangle);


         // Draw multiline text
         Font trFont = new Font("Times New Roman", 12);
         Rectangle rect = new Rectangle(0, 0, 400, trFont.Height * 3);
         g.DrawRectangle(Pens.Blue, rect);
         String longString = "Text Text Text Text Text Text Text Text Text Text  ";
         longString += "Text Text Text Text Text Text Text Text Text Text  ";
         longString += "Text Text Text Text Text Text Text Text Text Text  ";
         longString += "Text Text Text Text Text Text Text Text Text .";
         g.DrawString(longString, trFont, Brushes.Black, rect);

      }

    private void InitializeComponent()
    {
      this.Size = new System.Drawing.Size(300,300);
      this.Text = "Form1";
    }
    static void Main() 
    {
      Application.Run(new Form1());
    }
}


           
       








Related examples in the same category

1.Center each line of text horizontally and verticallyCenter each line of text horizontally and vertically
2.Text string's containing rectangleText string's containing rectangle
3.Draw Vertical String TextDraw Vertical String Text
4.Deal with Tab and New Line in Painting StringDeal with Tab and New Line in Painting String
5.String Format based on Tab info dataString Format based on Tab info data
6.Draw the string, using the rectangle as a bounding boxDraw the string, using the rectangle as a bounding box
7.Draw rectangle and string insideDraw rectangle and string inside
8.Tall in the Center
9.Engrave
10.Emboss
11.Clip Text
12.Draw string with new font and solid brushDraw string with new font and solid brush
13.Text RotateText Rotate
14.Text FormatText Format
15.Draw line and stringDraw line and string
16.Text Rotate 2Text Rotate 2