Draw the rectangle around the text

Draw the rectangle around the text


We can draw the rectangle around the text.

Step 1
Measure the font height

Setp 2
Draw the rectangle around the text.

Following namespaces are used


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




Following is the code for drawing the rectangle around the text.

public class Form1 : System.Windows.Forms.Form
{
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Text = "Pen Cap App";
this.Resize += new System.EventHandler(this.Form1_Resize);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

}
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics MyGraphics = e.Graphics;
MyGraphics.FillRectangle(Brushes.White, this.ClientRectangle);

FontFamily MyFontFamily = new FontFamily("Times New Roman");
Font MyFont = new Font(MyFontFamily, 30);
String MyString = "Shunmuganathan " + MyFont.Height;
SizeF Mysizef = MyGraphics.MeasureString(MyString, MyFont, Int32.MaxValue, StringFormat.GenericTypographic);
MyRectangleF = new RectangleF(0, 0, Mysizef.Width, MyFont.Height);

MyGraphics.DrawRectangle(Pens.Black,MyRectangleF.Left, MyRectangleF.Top, MyRectangleF.Width, MyRectangleF.Height);
MyGraphics.DrawString(MyString, MyFont, Brushes.Black, , StringFormat.GenericTypographic);

MyFont.Dispose();

}

private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}



By
Nathan


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: