This text is centered and underlined

image_pdfimage_print


   

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());
    }
}


           
          


This entry was posted in 2D Graphics. Bookmark the permalink.