using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class Form1 : Form { protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); String s = "This string is long enough to wrap. "; Font f = new Font("Arial", 12); Rectangle r = new Rectangle(20, 20, 200, f.Height * 6); g.DrawRectangle(Pens.Black, r); g.DrawString(s, f, Brushes.Black, r); f.Dispose(); } public static void Main() { Application.Run(new Form1()); } }
use a 12pt font, and assume the text string must fit into a width of 150 pixels
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class Form1 : Form { protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); String s = "This string is long enough to wrap. "; Font f = new Font("Arial", 12); SizeF sf = g.MeasureString(s, f, 150); RectangleF rf = new RectangleF(20, 20, sf.Width, sf.Height); g.DrawRectangle(Pens.Black, rf.Left, rf.Top, rf.Width, rf.Height); g.DrawString(s, f, Brushes.Black, rf); f.Dispose(); } public static void Main() { Application.Run(new Form1()); } }
Draw the rectangle and draw the string, using the rectangle as a bounding box
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class Form1 : Form { protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); String s = "This string is long enough to wrap. "; Font f = new Font("Arial", 12); Rectangle r = new Rectangle(20, 20, 150, f.Height * 4); StringFormat sf = new StringFormat(); sf.FormatFlags = StringFormatFlags.NoWrap; g.DrawRectangle(Pens.Black, r); g.DrawString(s, f, Brushes.Black, r, sf); f.Dispose(); } public static void Main() { Application.Run(new Form1()); } }
a long string that will wrap and centered both vertically and horizontally
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class Form1 : Form { protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); String s = "This is a long string that will wrap. "; s += "It will be centered both vertically and horizontally."; Font f = new Font("Arial", 12); StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; Rectangle r = new Rectangle(10, 10, 300, f.Height * 4); g.DrawRectangle(Pens.Black, r); g.DrawString(s, f, Brushes.Black, r, sf); f.Dispose(); } public static void Main() { Application.Run(new Form1()); } }
String Rectangle
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class Form1 : Form { protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); String s = "AAAAAAAAAA"; StringFormat sf = new StringFormat(StringFormatFlags.DirectionVertical); Font f = new Font("Times New Roman", 14); SizeF sizef = g.MeasureString(s, f, Int32.MaxValue, sf); RectangleF rf = new RectangleF(20, 20, sizef.Width, sizef.Height); g.DrawRectangle(Pens.Black, rf.Left, rf.Top, rf.Width, rf.Height); g.DrawString(s, f, Brushes.Black, rf, sf); f.Dispose(); } public static void Main() { Application.Run(new Form1()); } }
Use tab to control the DrawString
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class Form1 : Form { protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; g.FillRectangle(Brushes.White, this.ClientRectangle); Font f = new Font("Times New Roman", 12); Font bf = new Font(f, FontStyle.Bold); StringFormat sf = new StringFormat(); float[] ts = { 10.0f, 70.0f, 100.0f, 90.0f }; sf.SetTabStops(0.0f, ts); string s1 = " A AAA AAAAA AAAAAAAAAA"; string s2 = " AAAAAAA AAAAA AA AAA"; g.DrawString(s1, bf, Brushes.Black, 20, 20, sf); g.DrawString(s2, f, Brushes.Blue, 20, 20 + bf.Height, sf); f.Dispose(); bf.Dispose(); } public static void Main() { Application.Run(new Form1()); } }
This text is centered and underlined
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 centered text Font cFont = new Font("Courier New", 12, FontStyle.Underline); Rectangle rect = new Rectangle(0, 0, 400, cFont.Height); g.DrawRectangle(Pens.Blue, rect); StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; g.DrawString("This text is centered and underlined.", cFont, Brushes.Red, rect, sf); } private void InitializeComponent() { this.Size = new System.Drawing.Size(300,300); this.Text = "Form1"; } static void Main() { Application.Run(new Form1()); } }