Mega Code Archive

 
Categories / C# Tutorial / 2D Graphics
 

Draw left justified text based on font size

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Windows.Forms;   class Form1 : Form   {     public Form1()     {       SetStyle(ControlStyles.Opaque, true);       Bounds = new Rectangle(0, 0, 500, 300);     }     protected override void OnPaint(PaintEventArgs e)     {       Graphics g = e.Graphics;       int y = 0;       g.FillRectangle(Brushes.White, ClientRectangle);       // draw left justified text       Rectangle rect = new Rectangle(0, y, 400, Font.Height);       g.DrawRectangle(Pens.Blue, rect);       g.DrawString("This text is left justified.", Font,Brushes.Black, rect);       y += Font.Height + 20;     }        [STAThread]     static void Main()     {       Application.EnableVisualStyles();       Application.Run(new Form1());     }   }