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()); } }
2D Graphics
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()); } }
FromSystemColor
using System; using System.Drawing; using System.Windows.Forms; class XMarksTheSpot : Form { public static void Main() { Application.Run(new XMarksTheSpot()); } public XMarksTheSpot() { Text = "X Marks The Spot"; ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { Graphics grfx = pea.Graphics; Pen pen = SystemPens.FromSystemColor(SystemColors.ActiveBorder); grfx.DrawLine(pen, 0, 0, ClientSize.Width - 1, ClientSize.Height - 1); grfx.DrawLine(pen, 0, ClientSize.Height - 1, ClientSize.Width - 1, 0); } }
Use the color of Window and WindowText from SystemColors
using System; using System.Drawing; using System.Windows.Forms; class HuckleberryFinn: Form { public static void Main() { Application.Run(new HuckleberryFinn()); } public HuckleberryFinn() { Text = ""The Adventures of Huckleberry Finn""; BackColor = SystemColors.Window; ForeColor = SystemColors.WindowText; ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { pea.Graphics.DrawString("some stretchers, as I said before.", Font, new SolidBrush(ForeColor), ClientRectangle); } }
Create System Brush from System Color
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; using System.Windows; class HundredPixelsSquare : Form { public static void Main() { Application.Run(new HundredPixelsSquare()); } public HundredPixelsSquare() { Text = "Pixels Square"; ResizeRedraw = true; } protected override void OnPaint(PaintEventArgs pea) { DoPage(pea.Graphics, ForeColor, ClientSize.Width, ClientSize.Height); } protected void DoPage(Graphics grfx, Color clr, int cx, int cy) { Brush brush = SystemBrushes.FromSystemColor(SystemColors.MenuText); grfx.FillRectangle(brush, 100, 100, 100, 100); } }