C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Graphics »

Draw the rectangle around the text


Posted Date: 05 Aug 2009    Resource Type: Code Snippets    Category: Graphics
Author: Shunmuganathan MMember Level: Diamond    
Rating: 1 out of 5Points: 9



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



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Draw the rectangle  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Take the screen shot of your window
Previous Resource: Show your Label in different color in Runtime C#
Return to Discussion Resource Index
Post New Resource
Category: Graphics


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use