using System;
using System.Drawing;
using System.Windows.Forms;
class FontMenuForm: Form
{
protected string strText = "Emboss";
protected Font font = new Font("Times New Roman", 24);
public static void Main()
{
Application.Run(new FontMenuForm());
}
public FontMenuForm()
{
ResizeRedraw = true;
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
int iOffset = 2;
SizeF sizef = grfx.MeasureString(strText, font);
float x = (cx - sizef.Width) / 2;
float y = (cy - sizef.Height) / 2;
grfx.Clear(Color.White);
grfx.DrawString(strText, font, Brushes.Gray, x, y);
grfx.DrawString(strText, font, Brushes.White, x - iOffset,
y - iOffset);
}
public float GetAscent(Graphics grfx, Font font)
{
return font.GetHeight(grfx) *
font.FontFamily.GetCellAscent(font.Style) /
font.FontFamily.GetLineSpacing(font.Style);
}
public float GetDescent(Graphics grfx, Font font)
{
return font.GetHeight(grfx) *
font.FontFamily.GetCellDescent(font.Style) /
font.FontFamily.GetLineSpacing(font.Style);
}
public float PointsToPageUnits(Graphics grfx, Font font)
{
float fFontSize;
if (grfx.PageUnit == GraphicsUnit.Display)
fFontSize = 100 * font.SizeInPoints / 72;
else
fFontSize = grfx.DpiX * font.SizeInPoints / 72;
return fFontSize;
}
}
2D Graphics
Engrave
using System;
using System.Drawing;
using System.Windows.Forms;
class FontMenuForm: Form
{
protected string strText = "Engrave";
protected Font font = new Font("Times New Roman", 24);
public static void Main()
{
Application.Run(new FontMenuForm());
}
public FontMenuForm()
{
ResizeRedraw = true;
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
int iOffset = -2;
SizeF sizef = grfx.MeasureString(strText, font);
float x = (cx - sizef.Width) / 2;
float y = (cy - sizef.Height) / 2;
grfx.Clear(Color.White);
grfx.DrawString(strText, font, Brushes.Gray, x, y);
grfx.DrawString(strText, font, Brushes.White, x - iOffset,
y - iOffset);
}
public float GetAscent(Graphics grfx, Font font)
{
return font.GetHeight(grfx) *
font.FontFamily.GetCellAscent(font.Style) /
font.FontFamily.GetLineSpacing(font.Style);
}
public float GetDescent(Graphics grfx, Font font)
{
return font.GetHeight(grfx) *
font.FontFamily.GetCellDescent(font.Style) /
font.FontFamily.GetLineSpacing(font.Style);
}
public float PointsToPageUnits(Graphics grfx, Font font)
{
float fFontSize;
if (grfx.PageUnit == GraphicsUnit.Display)
fFontSize = 100 * font.SizeInPoints / 72;
else
fFontSize = grfx.DpiX * font.SizeInPoints / 72;
return fFontSize;
}
}
Tall in the Center
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
class FontMenuForm: Form
{
protected string strText = “Tall in the Center”;
protected Font font = new Font(“Times New Roman”, 24);
public static void Main()
{
Application.Run(new FontMenuForm());
}
public FontMenuForm()
{
ResizeRedraw = true;
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy)
{
GraphicsPath path = new GraphicsPath();
float fFontSize = PointsToPageUnits(grfx, font);
path.AddString(strText, font.FontFamily, (int) font.Style,
fFontSize, new PointF(0, 0), new StringFormat());
RectangleF rectf = path.GetBounds();
path.Transform(new Matrix(1, 0, 0, 1,
-(rectf.Left + rectf.Right) / 2,
-(rectf.Top + rectf.Bottom) / 2));
rectf = path.GetBounds();
PointF[] aptf = path.PathPoints;
for (int i = 0; i < aptf.Length; i++) aptf[i].Y *= 2 * (rectf.Width - Math.Abs(aptf[i].X)) / rectf.Width; path = new GraphicsPath(aptf, path.PathTypes); grfx.TranslateTransform(cx / 2, cy / 2); grfx.FillPath(new SolidBrush(clr), path); } public float GetAscent(Graphics grfx, Font font) { return font.GetHeight(grfx) * font.FontFamily.GetCellAscent(font.Style) / font.FontFamily.GetLineSpacing(font.Style); } public float GetDescent(Graphics grfx, Font font) { return font.GetHeight(grfx) * font.FontFamily.GetCellDescent(font.Style) / font.FontFamily.GetLineSpacing(font.Style); } public float PointsToPageUnits(Graphics grfx, Font font) { float fFontSize; if (grfx.PageUnit == GraphicsUnit.Display) fFontSize = 100 * font.SizeInPoints / 72; else fFontSize = grfx.DpiX * font.SizeInPoints / 72; return fFontSize; } } [/csharp]
Draw rectangle and string inside
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public class MainForm : System.Windows.Forms.Form
{
public MainForm()
{
InitializeComponent();
CenterToScreen();
SetStyle(ControlStyles.ResizeRedraw, true);
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Text = "Pens...";
this.Resize += new System.EventHandler(this.Form1_Resize);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.MainForm_Paint);
}
static void Main()
{
Application.Run(new MainForm());
}
private void MainForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
// And a rect with some text...
Rectangle r = new Rectangle(150, 10, 130, 60);
g.DrawRectangle(Pens.Blue, r);
g.DrawString("www.kutayzorlu.com/java2s/com",
new Font("Arial", 12), Brushes.Black, r);
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}
Draw the string, using the rectangle as a bounding box
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;
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 g = e.Graphics;
g.FillRectangle(Brushes.White, this.ClientRectangle);
String s = "Text Text Text Text Text Text Text Text Text Text ";
s += "Text Text Text Text Text Text Text Text Text Text Text Text Text ";
s += "Text Text Text Text Text Text Text Text Text Text Text Text ";
Font f = new Font("Arial", 12);
Rectangle r = new Rectangle(20, 20, 200, f.Height * 6);
//
g.DrawRectangle(Pens.Black, r);
g.DrawString(s, f, Brushes.Black, r);
f.Dispose();
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}
String Format based on Tab info data
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;
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 = "";
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 g = e.Graphics;
g.FillRectangle(Brushes.White, this.ClientRectangle);
Font f = new Font("Courier New", 12);
Font bf = new Font(f, FontStyle.Bold);
StringFormat sf = new StringFormat();
float[] ts = { 100.0f, 150.0f, 170.0f };
sf.SetTabStops(0.0f, ts);
string fs = "{0} {1,8} {2,10} {3,12}";
string s1 = String.Format(fs, "Name", "Salary", "New Salary", "Difference");
string s2 = String.Format(fs, "January", "900.00", "1050.00", "-150.00");
string s3 = String.Format(fs, "February", "1100.00", "990.00", "110.00");
// Draw the text strings
g.DrawString(s1, bf, Brushes.Black, 20, 20, sf);
g.DrawString(s2, f, Brushes.Blue, 20, 20 + bf.Height, sf);
g.DrawString(s3, f, Brushes.Blue, 20, 20 + bf.Height + f.Height, sf);
f.Dispose();
bf.Dispose();
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}
Deal with Tab and New Line in Painting String
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;
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 = "";
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 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);
// The escape-sequence in these lines specifies the tab
string s1 = " Name Eye Color Height";
string s2 = " Bob Brown 175cm";
string s3 = " Mary Blond 161cm
Bill Black 168cm";
g.DrawString(s1, bf, Brushes.Black, 20, 20, sf);
g.DrawString(s2, f, Brushes.Blue, 20, 20 + bf.Height, sf);
g.DrawString(s3, f, Brushes.Blue, 20,
20 + bf.Height + f.Height, sf);
f.Dispose();
bf.Dispose();
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}