Csharp/C Sharp/2D Graphics
Версия от 15:31, 26 мая 2010; (обсуждение)
- Ani File
- Animation
- Arrow
- Bitmap
- Blend
- Brush
- Clip
- Color
- Color Matrix
- Coordiate
- Curve
- Dash Pen
- Double Buffer
- Drag and Draw
- Ellipse
- FillMode
- Gif Animation
- Gradient
- Graphics
- Graphics File
- Graphics Unit
- Hatch Brush
- Icon Image
- Image
- Image Decoder
- ImageAttributes
- Interpolation Mode
- JPG
- Line
- Line Cap
- Line Join
- LinearGradientBrush
- Matrix
- Mouse Draw
- Path
- PathGradientBrush
- Pens
- Pie
- Pixel
- Point
- Polygon
- Rectangle
- Region
- Region Clip
- Rotate
- Scale
- Screen
- Scrolling
- Smooth Paint
- Smoothing Mode
- SolidBrush
- String Format
- StringAlignment
- StringTrmming
- SystemBrushes
- SystemColors
- SystemPens
- Text
- Text Justify
- Text Rendering
- Texture
- Transform
- wmf file
Содержание
- 1 Calculate font unit metrics from the font family
- 2 Calculate GraphicsUnit metrics from the font
- 3 Derive surrounding Rectangle from String
- 4 Draw a line for the ascent, descent, baseline, textOrigin
- 5 Draw font cell ascent, cell descent, line space, em height
- 6 Font Attributes
- 7 FontFamilies: IsStyleAvailable,
- 8 Font Famylies
- 9 Font Metrics
- 10 Fonts Class
- 11 Font size, name and strike out
- 12 Font Style: Bold
- 13 Font Style: Bold and Italic
- 14 Font Style: Bold and Strikeout
- 15 Font Style: Italic
- 16 Font Style: Regular
- 17 Font Style: Strike out
- 18 Font Style: Underline
- 19 Font Viewer
- 20 Get font cell ascent, descent, LineSpacing and EmHeight
- 21 Get font family info
- 22 Get font from Font dialog and redraw string
- 23 List Fonts
- 24 Measure Font height and draw surrounding Rectangle
- 25 Smoothing Fonts
- 26 Write font metrics for Arial font family
- 27 Write font metrics for Courier New font family
- 28 Write font metrics for Times New Roman font family
Calculate font unit metrics from the font family
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
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);
FontFamily ff = new FontFamily("Times New Roman");
float emSizeInGU = 24f;
Font f = new Font(ff, emSizeInGU);
// Get the design unit metrics from the font family
int emSizeInDU = ff.GetEmHeight(FontStyle.Regular);
int ascentInDU = ff.GetCellAscent(FontStyle.Regular);
int descentInDU = ff.GetCellDescent(FontStyle.Regular);
int lineSpacingInDU = ff.GetLineSpacing(FontStyle.Regular);
// Calculate the GraphicsUnit metrics from the font
float ascentInGU = ascentInDU * (emSizeInGU / emSizeInDU);
float descentInGU = descentInDU * (emSizeInGU / emSizeInDU);
float lineSpacingInGU = lineSpacingInDU * (emSizeInGU / emSizeInDU);
// Output the metrics to the console
Console.WriteLine("emSize = " + emSizeInDU + " DesignUnits");
Console.WriteLine("emSize = " + emSizeInGU + " GraphicsUnits");
string format = "{0,-16}{1,12}{2,16}";
Console.WriteLine(format, "Font Metric", "DesignUnits", "GraphicsUnits");
Console.WriteLine(format, "Ascent", ascentInDU, ascentInGU);
Console.WriteLine(format, "Descent", descentInDU, descentInGU);
Console.WriteLine(format, "Line Spacing", lineSpacingInDU, lineSpacingInGU);
// Draw two lines of the text string
PointF textOrigin = new PointF(20, 20);
PointF nextLineOrigin = new PointF(textOrigin.X,
textOrigin.Y + f.Height);
g.DrawString("WWW.nfex.ru", f, Brushes.Black, textOrigin);
g.DrawString("www.nfex.ru", f, Brushes.Black, nextLineOrigin);
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}
Calculate GraphicsUnit metrics from the font
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
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);
FontFamily ff = new FontFamily("Times New Roman");
float emSizeInGU = 24f;
Font f = new Font(ff, emSizeInGU);
// Get the design unit metrics from the font family
int emSizeInDU = ff.GetEmHeight(FontStyle.Regular);
int ascentInDU = ff.GetCellAscent(FontStyle.Regular);
int descentInDU = ff.GetCellDescent(FontStyle.Regular);
int lineSpacingInDU = ff.GetLineSpacing(FontStyle.Regular);
// Calculate the GraphicsUnit metrics from the font
float ascentInGU = ascentInDU * (emSizeInGU / emSizeInDU);
float descentInGU = descentInDU * (emSizeInGU / emSizeInDU);
float lineSpacingInGU = lineSpacingInDU * (emSizeInGU / emSizeInDU);
// Output the metrics to the console
Console.WriteLine("emSize = " + emSizeInDU + " DesignUnits");
Console.WriteLine("emSize = " + emSizeInGU + " GraphicsUnits");
string format = "{0,-16}{1,12}{2,16}";
Console.WriteLine(format, "Font Metric", "DesignUnits", "GraphicsUnits");
Console.WriteLine(format, "Ascent", ascentInDU, ascentInGU);
Console.WriteLine(format, "Descent", descentInDU, descentInGU);
Console.WriteLine(format, "Line Spacing", lineSpacingInDU, lineSpacingInGU);
// Draw two lines of the text string
PointF textOrigin = new PointF(20, 20);
PointF nextLineOrigin = new PointF(textOrigin.X,
textOrigin.Y + f.Height);
g.DrawString("WWW.nfex.ru", f, Brushes.Black, textOrigin);
g.DrawString("www.nfex.ru", f, Brushes.Black, nextLineOrigin);
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}
Derive surrounding Rectangle from String
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
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.PageUnit = GraphicsUnit.Inch;
Pen p = new Pen(Color.Black, 1 / 96f);
Font f = new Font("Times New Roman", 16);
String s = "www.nfex.ru";
SizeF sf = g.MeasureString(s, f);
g.DrawRectangle(p, 1, 1, sf.Width, sf.Height);
g.DrawString(s, f, Brushes.Black, 1, 1);
f.Dispose();
p.Dispose();
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}
Draw a line for the ascent, descent, baseline, textOrigin
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
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);
FontFamily ff = new FontFamily("Times New Roman");
float emSizeInGU = 24f;
Font f = new Font(ff, emSizeInGU);
// Get the design unit metrics from the font family
int emSizeInDU = ff.GetEmHeight(FontStyle.Regular);
int ascentInDU = ff.GetCellAscent(FontStyle.Regular);
int descentInDU = ff.GetCellDescent(FontStyle.Regular);
int lineSpacingInDU = ff.GetLineSpacing(FontStyle.Regular);
// Calculate the GraphicsUnit metrics from the font
float ascentInGU = ascentInDU * (emSizeInGU / emSizeInDU);
float descentInGU = descentInDU * (emSizeInGU / emSizeInDU);
float lineSpacingInGU = lineSpacingInDU * (emSizeInGU / emSizeInDU);
// Draw two lines of the text string
PointF textOrigin = new PointF(20, 20);
PointF nextLineOrigin = new PointF(textOrigin.X,
textOrigin.Y + f.Height);
g.DrawString("WWW.nfex.ru", f, Brushes.Black, textOrigin);
g.DrawString("www.nfex.ru", f, Brushes.Black, nextLineOrigin);
// Draw a line at the textOrigin
int lineLen = 300;
g.DrawLine(Pens.Blue, textOrigin, new PointF(textOrigin.X + lineLen, textOrigin.Y));
g.DrawLine(Pens.Red, nextLineOrigin, new PointF(nextLineOrigin.X + lineLen, nextLineOrigin.Y));
// Draw a line at the baseline
PointF p = new PointF(textOrigin.X, textOrigin.Y + lineSpacingInGU);
g.DrawLine(Pens.Blue, p, new PointF(p.X + lineLen, p.Y));
p = new PointF(nextLineOrigin.X, nextLineOrigin.Y + lineSpacingInGU);
g.DrawLine(Pens.Red, p, new PointF(p.X + lineLen, p.Y));
// Draw a line at the top of the ascent
p = new PointF(textOrigin.X, textOrigin.Y + lineSpacingInGU - ascentInGU);
g.DrawLine(Pens.Blue, p, new PointF(p.X + lineLen, p.Y));
p = new PointF(nextLineOrigin.X, nextLineOrigin.Y + lineSpacingInGU - ascentInGU);
g.DrawLine(Pens.Red, p, new PointF(p.X + lineLen, p.Y));
// Draw a line at the bottom of the descent
p = new PointF(textOrigin.X, textOrigin.Y + lineSpacingInGU + descentInGU);
g.DrawLine(Pens.Blue, p, new PointF(p.X + lineLen, p.Y));
p = new PointF(nextLineOrigin.X, nextLineOrigin.Y + lineSpacingInGU + descentInGU);
g.DrawLine(Pens.Red, p, new PointF(p.X + lineLen, p.Y));
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}
Draw font cell ascent, cell descent, line space, em height
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, this.ClientRectangle);
FontFamily ff = new FontFamily("Times New Roman");
float emSizeInGU = 24f;
Font f = new Font(ff, emSizeInGU);
int emSizeInDU = ff.GetEmHeight(FontStyle.Regular);
int ascentInDU = ff.GetCellAscent(FontStyle.Regular);
int descentInDU = ff.GetCellDescent(FontStyle.Regular);
int lineSpacingInDU = ff.GetLineSpacing(FontStyle.Regular);
float ascentInGU = ascentInDU * (emSizeInGU / emSizeInDU);
float descentInGU = descentInDU * (emSizeInGU / emSizeInDU);
float lineSpacingInGU = lineSpacingInDU * (emSizeInGU / emSizeInDU);
PointF textOrigin = new PointF(20, 20);
PointF nextLineOrigin = new PointF(textOrigin.X,textOrigin.Y + f.Height);
g.DrawString("AxgQ", f, Brushes.Black, textOrigin);
g.DrawString("AxgQ", f, Brushes.Black, nextLineOrigin);
int lineLen = 100;
g.DrawLine(Pens.Blue,textOrigin,new PointF(textOrigin.X + lineLen, textOrigin.Y));
g.DrawLine(Pens.Red,nextLineOrigin,new PointF(nextLineOrigin.X + lineLen, nextLineOrigin.Y));
PointF p = new PointF(textOrigin.X,textOrigin.Y + lineSpacingInGU);
g.DrawLine(Pens.Blue, p,new PointF(p.X + lineLen, p.Y));
p = new PointF(nextLineOrigin.X,nextLineOrigin.Y + lineSpacingInGU);
g.DrawLine(Pens.Red, p,new PointF(p.X + lineLen, p.Y));
p = new PointF(textOrigin.X,textOrigin.Y + lineSpacingInGU - ascentInGU);
g.DrawLine(Pens.Blue, p,new PointF(p.X + lineLen, p.Y));
p = new PointF(nextLineOrigin.X, nextLineOrigin.Y +lineSpacingInGU - ascentInGU);
g.DrawLine(Pens.Red, p, new PointF(p.X + lineLen, p.Y));
p = new PointF(textOrigin.X,textOrigin.Y + lineSpacingInGU + descentInGU);
g.DrawLine(Pens.Blue, p,new PointF(p.X + lineLen, p.Y));
p = new PointF(nextLineOrigin.X,nextLineOrigin.Y + lineSpacingInGU + descentInGU);
g.DrawLine(Pens.Red, p,new PointF(p.X + lineLen, p.Y));
}
public static void Main() {
Application.Run(new Form1());
}
}
Font Attributes
/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds
Publisher: Apress
ISBN: 159059035X
*/
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace FontAttr_c
{
/// <summary>
/// Summary description for FontAttr.
/// </summary>
public class FontAttr : System.Windows.Forms.Form
{
private System.Windows.Forms.Button cmdGo;
private System.Windows.Forms.Panel P1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ruponentModel.Container components = null;
public FontAttr()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.cmdGo = new System.Windows.Forms.Button();
this.P1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// cmdGo
//
this.cmdGo.Location = new System.Drawing.Point(264, 288);
this.cmdGo.Name = "cmdGo";
this.cmdGo.Size = new System.Drawing.Size(56, 24);
this.cmdGo.TabIndex = 0;
this.cmdGo.Text = "GO";
this.cmdGo.Click += new System.EventHandler(this.cmdGo_Click);
//
// P1
//
this.P1.AutoScroll = true;
this.P1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.P1.Location = new System.Drawing.Point(16, 32);
this.P1.Name = "P1";
this.P1.Size = new System.Drawing.Size(304, 240);
this.P1.TabIndex = 1;
//
// FontAttr
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(342, 323);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.P1,
this.cmdGo});
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "FontAttr";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "FontAttr";
this.Load += new System.EventHandler(this.FontAttr_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new FontAttr());
}
private void FontAttr_Load(object sender, System.EventArgs e)
{
}
private void EnumInstalledFonts()
{
FontStyle Style;
int y = 0;
foreach (FontFamily ff in FontFamily.Families)
{
if ( ff.IsStyleAvailable(Style = FontStyle.Regular) )
AddString(ff, ref y, Style);
if ( ff.IsStyleAvailable(Style = FontStyle.Bold) )
AddString(ff, ref y, Style);
if ( ff.IsStyleAvailable(Style = FontStyle.Italic) )
AddString(ff, ref y, Style);
if ( ff.IsStyleAvailable(Style = FontStyle.Strikeout) )
AddString(ff, ref y, Style);
if ( ff.IsStyleAvailable(Style = FontStyle.Underline) )
AddString(ff, ref y, Style);
}
}
private void AddString(FontFamily ff, ref int y, FontStyle Style)
{
using ( Font fnt = new Font(ff, 12, Style, GraphicsUnit.Pixel) )
{
int LineSpace = (int)(ff.GetLineSpacing(Style) *
fnt.Size / ff.GetEmHeight(Style));
y += LineSpace + 2;
PictureBox P = new PictureBox();
P.Height = LineSpace;
P.Width = P1.Width;
Bitmap B = new Bitmap(P.Width, P.Height);
using (Graphics G = Graphics.FromImage(B))
{
G.DrawString(ff.Name + " : Style = " + Style.ToString(),
fnt, Brushes.Black, 0, 0);
}
P.Image=B;
P1.Controls.Add(P);
P1.Controls[P1.Controls.Count-1].Location = new Point(2, y);
if ( y < P1.Height )
P1.Refresh();
}
}
private void cmdGo_Click(object sender, System.EventArgs e)
{
P1.Controls.Clear();
EnumInstalledFonts();
}
}
}
FontAttr-c.zip( 10 k)</a>
FontFamilies: IsStyleAvailable,
using System;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
class FamiliesList : Form {
const int iPointSize = 12;
public static void Main() {
Application.Run(new FamiliesList());
}
public FamiliesList() {
Text = "Font Families List";
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs pea) {
DoPage(pea.Graphics, ForeColor, ClientSize.Width, ClientSize.Height);
}
protected void DoPage(Graphics grfx, Color clr, int cx, int cy) {
Brush brush = new SolidBrush(clr);
float x = 0, y = 0, fMaxWidth = 0;
FontCollection fc = new InstalledFontCollection();
FontFamily[] aff = fc.Families;
foreach (FontFamily ff in aff) {
Font font = CreateSampleFont(ff, iPointSize);
SizeF sizef = grfx.MeasureString(ff.Name, font);
fMaxWidth = Math.Max(fMaxWidth, sizef.Width);
}
foreach (FontFamily ff in aff) {
Font font = CreateSampleFont(ff, iPointSize);
float fHeight = font.GetHeight(grfx);
if (y > 0 && y + fHeight > cy) {
x += fMaxWidth;
y = 0;
}
grfx.DrawString(ff.Name, font, brush, x, y);
y += fHeight;
}
}
Font CreateSampleFont(FontFamily ff, float fPointSize) {
if (ff.IsStyleAvailable(FontStyle.Regular))
return new Font(ff, fPointSize);
else if (ff.IsStyleAvailable(FontStyle.Bold))
return new Font(ff, fPointSize, FontStyle.Bold);
else if (ff.IsStyleAvailable(FontStyle.Italic))
return new Font(ff, fPointSize, FontStyle.Italic);
else
return Font;
}
}
Font Famylies
/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow
Publisher: Peer Information
ISBN: 1861007663
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Text; // InstalledFontCollection
namespace FontFamylies
{
/// <summary>
/// Summary description for FontFamylies.
/// </summary>
public class FontFamylies : System.Windows.Forms.Form
{
private System.Windows.Forms.ruboBox comboBox1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ruponentModel.Container components = null;
FontFamily[] iFCF;
ArrayList iFCFN;
public FontFamylies()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.Text = "Installed Font Families";
iFCF = null; // fontfamilies
iFCFN = new ArrayList(); // strings
iFCF = InstalledFontFamilies(iFCFN);
this.ruboBox1.Sorted = true;
this.ruboBox1.DataSource = iFCFN; //set the combo"s data source
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.ruboBox1 = new System.Windows.Forms.ruboBox();
this.SuspendLayout();
//
// comboBox1
//
this.ruboBox1.Name = "comboBox1";
this.ruboBox1.Size = new System.Drawing.Size(121, 21);
this.ruboBox1.TabIndex = 0;
this.ruboBox1.Text = "comboBox1";
//
// FontFamylies
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.ruboBox1});
this.Name = "FontFamylies";
this.Text = "FontFamylies";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new FontFamylies());
}
private FontFamily[] InstalledFontFamilies(ArrayList iFCFN)
{
InstalledFontCollection iFC = new InstalledFontCollection();
foreach(FontFamily ff in iFC.Families)
iFCFN.Add(ff.Name);
return iFC.Families;
}
protected override void OnPaint(PaintEventArgs pea)
{
Graphics g = pea.Graphics;
int wi = 150, hi = 12, rectNb = 4;
this.Width = (wi + 2)*rectNb + 9;
int iFCFNb = iFCF.Length;
DisplayInstalledFontFamilies(g, iFCFNb, wi, hi, rectNb);
g.Dispose();
}
private void DisplayInstalledFontFamilies(Graphics g, int iFCFNb, int wi,
int hi, int rectNb)
{
Rectangle rec;
Pen p = new Pen(this.ForeColor);
Brush b = null;
Font f;
StringFormat strfmt = new StringFormat();
strfmt.LineAlignment = strfmt.Alignment = StringAlignment.Near;
int x, y;
for (int i = 0; i < iFCFNb; i++)
{
x = (int)(i % rectNb);
y = (int)(i / rectNb);
rec = new Rectangle(1 + x*(2 + wi), 25 + y*(2 + hi), wi, hi);
g.DrawRectangle(p, rec);
try
{
f = new Font(iFCF[i], 8, FontStyle.Regular, GraphicsUnit.Point);
}
catch
{
// some fonts do not support Regular style
f = new Font("Arial", 8, FontStyle.Strikeout, GraphicsUnit.Point);
}
b = new SolidBrush(Color.Black);
g.DrawString((string)iFCFN[i], f, b, rec, strfmt);
}
p.Dispose(); b.Dispose();
}
}
}
Font Metrics
/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds
Publisher: Apress
ISBN: 159059035X
*/
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace FontMetrics_c
{
/// <summary>
/// Summary description for FontMetrics.
/// </summary>
public class FontMetrics : System.Windows.Forms.Form
{
private System.Windows.Forms.Button cmdRoman;
private System.Windows.Forms.Button cmdArial;
private System.Windows.Forms.Button cmdComic;
private System.Windows.Forms.Button cmdCourier;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ruponentModel.Container components = null;
public FontMetrics()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.cmdRoman = new System.Windows.Forms.Button();
this.cmdArial = new System.Windows.Forms.Button();
this.cmdComic = new System.Windows.Forms.Button();
this.cmdCourier = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// cmdRoman
//
this.cmdRoman.Location = new System.Drawing.Point(8, 240);
this.cmdRoman.Name = "cmdRoman";
this.cmdRoman.Size = new System.Drawing.Size(104, 24);
this.cmdRoman.TabIndex = 0;
this.cmdRoman.Text = "Times Roman";
this.cmdRoman.Click += new System.EventHandler(this.cmdRoman_Click);
//
// cmdArial
//
this.cmdArial.Location = new System.Drawing.Point(128, 240);
this.cmdArial.Name = "cmdArial";
this.cmdArial.Size = new System.Drawing.Size(104, 24);
this.cmdArial.TabIndex = 1;
this.cmdArial.Text = "Arial Black";
this.cmdArial.Click += new System.EventHandler(this.cmdArial_Click);
//
// cmdComic
//
this.cmdComic.Location = new System.Drawing.Point(248, 240);
this.cmdComic.Name = "cmdComic";
this.cmdComic.Size = new System.Drawing.Size(104, 24);
this.cmdComic.TabIndex = 2;
this.cmdComic.Text = "Comic Sans MS";
this.cmdComic.Click += new System.EventHandler(this.cmdComic_Click);
//
// cmdCourier
//
this.cmdCourier.Location = new System.Drawing.Point(368, 240);
this.cmdCourier.Name = "cmdCourier";
this.cmdCourier.Size = new System.Drawing.Size(104, 24);
this.cmdCourier.TabIndex = 3;
this.cmdCourier.Text = "Courier New";
this.cmdCourier.Click += new System.EventHandler(this.cmdCourier_Click);
//
// FontMetrics
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(492, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.cmdCourier,
this.cmdComic,
this.cmdArial,
this.cmdRoman});
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "FontMetrics";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "FontMetrics";
this.Load += new System.EventHandler(this.FontMetrics_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new FontMetrics());
}
private void FontMetrics_Load(object sender, System.EventArgs e)
{
}
private void DisplayFontMetrics(FontFamily ff, Font fnt)
{
//Create graphics object and make it pretty
Graphics G = this.CreateGraphics();
G.SmoothingMode=SmoothingMode.AntiAlias;
G.TextRenderingHint=TextRenderingHint.AntiAlias;
//Get some metrics
int LineSpace = (int)(ff.GetLineSpacing(fnt.Style)*
fnt.Size/ff.GetEmHeight(fnt.Style));
int Descent = (int)(ff.GetCellDescent(fnt.Style)*
fnt.Size/ff.GetEmHeight(fnt.Style));
int Ascent = (int)(ff.GetCellAscent(fnt.Style)*
fnt.Size/ff.GetEmHeight(fnt.Style));
//Create the base line to sit the text on
Point BaseLineStart = new Point ( 15, this.Height*3/5);
Point BaseLineEnd = new Point ( this.Width-15, this.Height*3/5);
//Top left corner of text is the ascent
Point StringPoint = new Point(75, (int)(BaseLineStart.Y-Ascent));
//Clear the screen and draw the string on a base line
G.Clear(Color.AliceBlue);
G.DrawString("A j Q", fnt, Brushes.Blue, StringPoint);
G.DrawLine(Pens.Black, BaseLineStart, BaseLineEnd);
//Draw the annotation lines
Size LineSize = new Size(0, LineSpace);
Size AscentSize = new Size(0, Ascent);
Size DescentSize = new Size(0, Descent);
G.DrawLine(Pens.Black, BaseLineStart-LineSize, BaseLineEnd-LineSize);
G.DrawLine(Pens.Red, BaseLineStart-AscentSize, BaseLineEnd-AscentSize);
G.DrawLine(Pens.DarkGreen, BaseLineStart+DescentSize,
BaseLineEnd+DescentSize);
//Annotate
Font AnnoFont = new Font("Arial", 10);
G.DrawString("Line Space = " + LineSpace.ToString(), AnnoFont,
Brushes.Black,
20,
(int)(BaseLineStart.Y-LineSpace-12));
G.DrawString("Ascent = " + Ascent.ToString(), AnnoFont,
Brushes.Red,
250,
(int)(BaseLineStart.Y-Ascent-12));
G.DrawString("Descent = " + Descent.ToString(), AnnoFont,
Brushes.DarkGreen,
350,
(int)(BaseLineStart.Y+Descent/8));
}
private void cmdRoman_Click(object sender, System.EventArgs e)
{
FontFamily ff = new FontFamily("Times New Roman");
Font f = new Font(ff, 75, FontStyle.Regular, GraphicsUnit.Pixel);
DisplayFontMetrics(ff, f);
}
private void cmdArial_Click(object sender, System.EventArgs e)
{
FontFamily ff = new FontFamily("Arial Black");
Font f = new Font(ff, 75, FontStyle.Regular, GraphicsUnit.Pixel);
DisplayFontMetrics(ff, f);
}
private void cmdComic_Click(object sender, System.EventArgs e)
{
FontFamily ff = new FontFamily("Comic Sans MS");
Font f = new Font(ff, 75, FontStyle.Regular, GraphicsUnit.Pixel);
DisplayFontMetrics(ff, f);
}
private void cmdCourier_Click(object sender, System.EventArgs e)
{
FontFamily ff = new FontFamily("Courier New");
Font f = new Font(ff, 75, FontStyle.Regular, GraphicsUnit.Pixel);
DisplayFontMetrics(ff, f);
}
}
}
Fonts Class
/*
GDI+ Programming in C# and VB .NET
by Nick Symmonds
Publisher: Apress
ISBN: 159059035X
*/
using System;
using System.Drawing;
using System.Drawing.Text;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
namespace FontsClass_c
{
/// <summary>
/// Summary description for FontsClass.
/// </summary>
public class FontsClass : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ruponentModel.Container components = null;
public FontsClass()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// FontsClass
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 323);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "FontsClass";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "FontsClass";
this.Load += new System.EventHandler(this.FontsClass_Load);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new FontsClass());
}
private void FontsClass_Load(object sender, System.EventArgs e)
{
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics G = e.Graphics;
G.TextRenderingHint=TextRenderingHint.AntiAlias;
int y = 0;
G.DrawString("Regular", Fonts.Arial_20, Brushes.Black, 50, y+=40);
G.DrawString("Italic", Fonts.ArialItalic_20, Brushes.Black, 50, y+=40);
G.DrawString("Regular", Fonts.Chain_20, Brushes.Black, 50, y+=40);
G.DrawString("Italic", Fonts.ChainItalic_20, Brushes.Black, 50, y+=40);
G.DrawString("Regular", Fonts.ruic_20, Brushes.Black, 50, y+=40);
G.DrawString("Italic", Fonts.ruicItalic_20, Brushes.Black, 50, y+=40);
}
}
public sealed class Fonts
{
private static PrivateFontCollection PFC;
private static FontFamily Arial_FF;
private static FontFamily Comic_FF;
private static FontFamily Chain_FF;
static Fonts(){
PFC = new PrivateFontCollection();
PFC.AddFontFile("C:\\WINNT\\Fonts\\Arial.ttf");
Arial_FF = PFC.Families[0];
PFC.AddFontFile("chainletters.ttf");
Chain_FF = PFC.Families[1];
PFC.AddFontFile("C:\\WINNT\\Fonts\\comic.ttf");
Comic_FF = PFC.Families[2];
}
public static FontFamily[] Families
{
get{ return PFC.Families; }
}
#region Arial Font
public static Font Arial_20
{
get {return new Font(Arial_FF, 20, FontStyle.Regular, GraphicsUnit.Point);}
}
public static Font ArialItalic_20
{
get {return new Font(Arial_FF, 20, FontStyle.Italic, GraphicsUnit.Point);}
}
#endregion
#region Chain font
public static Font Chain_20
{
get {return new Font(Chain_FF, 20, FontStyle.Regular, GraphicsUnit.Point);}
}
public static Font ChainItalic_20
{
get {return new Font(Chain_FF, 20, FontStyle.Italic, GraphicsUnit.Point);}
}
#endregion
#region Comic Font
public static Font Comic_20
{
get {return new Font(Comic_FF, 20, FontStyle.Regular, GraphicsUnit.Point);}
}
public static Font ComicItalic_20
{
get {return new Font(Comic_FF, 20, FontStyle.Italic, GraphicsUnit.Point);}
}
#endregion
}
}
FontsClass-c.zip( 32 k)</a>
Font size, name and strike out
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class UsingFonts : System.Windows.Forms.Form
{
private System.ruponentModel.Container components = null;
public UsingFonts()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(504, 109);
this.Name = "UsingFonts";
this.Text = "UsingFonts";
}
static void Main()
{
Application.Run( new UsingFonts() );
}
protected override void OnPaint(PaintEventArgs paintEvent )
{
Graphics graphicsObject = paintEvent.Graphics;
SolidBrush brush = new SolidBrush( Color.DarkBlue );
// arial, 12 pt bold
FontStyle style = FontStyle.Bold;
Font arial = new Font( new FontFamily( "Arial" ), 12, style );
// times new roman, 12 pt regular
style = FontStyle.Regular;
Font timesNewRoman = new Font( "Times New Roman", 12, style );
// courier new, 16 pt bold and italic
style = FontStyle.Bold | FontStyle.Italic;
Font courierNew = new Font( "Courier New", 16, style );
// tahoma, 18 pt strikeout
style = FontStyle.Strikeout;
Font tahoma = new Font( "Tahoma", 18, style );
graphicsObject.DrawString( arial.Name +
" 12 point bold.", arial, brush, 10, 10 );
graphicsObject.DrawString( timesNewRoman.Name +
" 12 point plain.", timesNewRoman, brush, 10, 30 );
graphicsObject.DrawString( courierNew.Name +
" 16 point bold and italic.", courierNew,
brush, 10, 54 );
graphicsObject.DrawString( tahoma.Name +
" 18 point strikeout.", tahoma, brush, 10, 75 );
}
}
Font Style: Bold
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
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);
Font font = new Font("Times New Roman", 12, FontStyle.Bold);
g.DrawString("Bold", font, Brushes.Black, 0, 0);
font.Dispose();
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}
Font Style: Bold and Italic
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
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);
Font font = new Font("Times New Roman", 12, FontStyle.Bold | FontStyle.Italic);
g.DrawString("Bold-Italic", font, Brushes.Black, 0, 0);
font.Dispose();
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}
Font Style: Bold and Strikeout
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
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);
Font font= new Font("Times New Roman", 12, FontStyle.Bold | FontStyle.Strikeout);
g.DrawString("Bold & Strikeout", font, Brushes.Black, 0, 0);
font.Dispose();
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}
Font Style: Italic
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
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);
Font font = new Font("Times New Roman", 12, FontStyle.Italic);
g.DrawString("Italic", font, Brushes.Black, 0, 0);
font.Dispose();
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}
Font Style: Regular
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
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);
Font font = new Font("Times New Roman", 12, FontStyle.Regular);
int h = font.Height;
g.DrawString("Regular", font, Brushes.Black, 0, 0);
font.Dispose();
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}
Font Style: Strike out
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
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);
Font font = new Font("Times New Roman", 12, FontStyle.Strikeout);
g.DrawString("Strikeout", font, Brushes.Black, 0, 0);
font.Dispose();
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}
Font Style: Underline
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
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);
Font font = new Font("Times New Roman", 12, FontStyle.Underline);
g.DrawString("Underline", font, Brushes.Black, 0, 0);
font.Dispose();
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}
Font Viewer
/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald
Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Win32;
namespace FontViewer
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class FontViewer : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ruboBox lstFonts;
private System.Windows.Forms.StatusBar statusBar;
private System.Windows.Forms.StatusBarPanel panel;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ruponentModel.Container components = null;
public FontViewer()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
// System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FontViewer));
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.label1 = new System.Windows.Forms.Label();
this.lstFonts = new System.Windows.Forms.ruboBox();
this.statusBar = new System.Windows.Forms.StatusBar();
this.panel = new System.Windows.Forms.StatusBarPanel();
this.groupBox1.SuspendLayout();
((System.ruponentModel.ISupportInitialize)(this.panel)).BeginInit();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.lstFonts,
this.label1});
this.groupBox1.Location = new System.Drawing.Point(0, -4);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(632, 40);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
//
// label1
//
this.label1.Location = new System.Drawing.Point(12, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 12);
this.label1.TabIndex = 0;
this.label1.Text = "Choose Font:";
//
// lstFonts
//
this.lstFonts.DropDownStyle = System.Windows.Forms.ruboBoxStyle.DropDownList;
this.lstFonts.DropDownWidth = 340;
this.lstFonts.Location = new System.Drawing.Point(100, 12);
this.lstFonts.Name = "lstFonts";
this.lstFonts.Size = new System.Drawing.Size(340, 21);
this.lstFonts.TabIndex = 1;
this.lstFonts.SelectedIndexChanged += new System.EventHandler(this.lstFonts_SelectedIndexChanged);
//
// statusBar
//
this.statusBar.Location = new System.Drawing.Point(0, 165);
this.statusBar.Name = "statusBar";
this.statusBar.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
this.panel});
this.statusBar.ShowPanels = true;
this.statusBar.Size = new System.Drawing.Size(632, 20);
this.statusBar.TabIndex = 1;
//
// panel
//
this.panel.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
this.panel.Width = 616;
//
// FontViewer
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
this.ClientSize = new System.Drawing.Size(632, 185);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.statusBar,
this.groupBox1});
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
// this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "FontViewer";
this.Text = "FontViewer";
this.Load += new System.EventHandler(this.FontViewer_Load);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.FontViewer_Paint);
this.groupBox1.ResumeLayout(false);
((System.ruponentModel.ISupportInitialize)(this.panel)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The FontViewer entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new FontViewer());
}
private void FontViewer_Load(object sender, System.EventArgs e)
{
System.Drawing.Text.InstalledFontCollection fonts = new System.Drawing.Text.InstalledFontCollection();
foreach (FontFamily family in fonts.Families)
{
lstFonts.Items.Add(family.Name);
}
RegistryKey rk;
rk = Registry.LocalMachine.OpenSubKey("Software\\ProseTech\\FontViewer");
if (rk != null) this.Text += " - " + rk.GetValue("Customer");
}
private void FontViewer_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
if (lstFonts.Text != "")
{
try
{
e.Graphics.DrawString(lstFonts.Text, new Font(lstFonts.Text, 50), Brushes.Black, 10, 50);
statusBar.Panels[0].Text = "";
}
catch (Exception err)
{
statusBar.Panels[0].Text = err.Message;
}
}
}
private void lstFonts_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (lstFonts.Text != "") this.Invalidate();
}
}
}
Get font cell ascent, descent, LineSpacing and EmHeight
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Test{
static void Main() {
FontFamily myFamily = new FontFamily("Verdana");
Font myFont = new Font(myFamily, 12);
int fontHeight = myFont.Height;
Console.WriteLine("Measurements are in GraphicsUnit." + myFont.Unit.ToString());
Console.WriteLine("The Verdana family.");
// Print our Family ties...
Console.WriteLine("Ascent for bold Verdana: " + myFamily.GetCellAscent(FontStyle.Bold));
Console.WriteLine("Descent for bold Verdana: " + myFamily.GetCellDescent(FontStyle.Bold));
Console.WriteLine("Line spacing for bold Verdana: " + myFamily.GetLineSpacing(FontStyle.Bold));
Console.WriteLine("Height for bold Verdana: " + myFamily.GetEmHeight(FontStyle.Bold));
}
}
Get font family info
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Imaging;
public class Test
{
static void Main()
{
// Create the format string
String formatString = "{0,-16}{1,8}{2,9}{3,10}{4,14}";
// Write the first line of the table
Console.WriteLine(formatString, "Font Family Name", "Ascent", "Descent",
"EmHeight", "Line Spacing");
// Write font metrics for Courier New font family
FontFamily ff = new FontFamily("Courier New");
Console.WriteLine(formatString, ff.GetName(0),
ff.GetCellAscent(FontStyle.Regular),
ff.GetCellDescent(FontStyle.Regular),
ff.GetEmHeight(FontStyle.Regular),
ff.GetLineSpacing(FontStyle.Regular));
}
}
Get font from Font dialog and redraw string
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form
{
private System.ruponentModel.Container components;
private System.Windows.Forms.FontDialog fontDlg;
private Font currFont;
public Form1()
{
InitializeComponent();
CenterToScreen();
fontDlg = new System.Windows.Forms.FontDialog();
fontDlg.ShowHelp = true;
Text = "Click on me to change the font";
currFont = new Font("Times New Roman", 12);
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Text = "Form1";
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
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.DrawString("www.nfex.ru...", currFont,
new SolidBrush(Color.Black), 0, 0);
}
private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (fontDlg.ShowDialog() != DialogResult.Cancel)
{
currFont = fontDlg.Font;
Invalidate();
}
}
}
List Fonts
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form {
public Form1() {
this.BackColor = Color.White;
}
protected override void OnPaint(PaintEventArgs e) {
int verticalCoordinate = 10;
Point topLeftCorner;
InstalledFontCollection insFont = new InstalledFontCollection();
FontFamily[] families = insFont.Families;
e.Graphics.TranslateTransform(AutoScrollPosition.X,AutoScrollPosition.Y);
foreach (FontFamily family in families) {
if (family.IsStyleAvailable(FontStyle.Regular)) {
Font f = new Font(family.Name, 10);
topLeftCorner = new Point(10, verticalCoordinate);
verticalCoordinate += f.Height;
e.Graphics.DrawString(family.Name, f, Brushes.Black, topLeftCorner);
f.Dispose();
}
}
}
public static void Main() {
Application.Run(new Form1());
}
}
Measure Font height and draw surrounding Rectangle
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ruponentModel;
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);
FontFamily ff = new FontFamily("Times New Roman");
Font f = new Font(ff, 30);
String s = "www.nfex.ru Height: " + f.Height;
SizeF sf = g.MeasureString(s, f, Int32.MaxValue, StringFormat.GenericTypographic);
RectangleF r = new RectangleF(0, 0, sf.Width, f.Height);
g.DrawRectangle(Pens.Black, r.Left, r.Top, r.Width, r.Height);
g.DrawString(s, f, Brushes.Black, r, StringFormat.GenericTypographic);
f.Dispose();
}
private void Form1_Resize(object sender, System.EventArgs e)
{
Invalidate();
}
}
Smoothing Fonts
/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald
Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Drawing.Text;
namespace GDI_Basics
{
/// <summary>
/// Summary description for SmoothingFonts.
/// </summary>
public class SmoothingFonts : System.Windows.Forms.Form
{
internal System.Windows.Forms.Label Label3;
internal System.Windows.Forms.Label Label2;
internal System.Windows.Forms.Label Label1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ruponentModel.Container components = null;
public SmoothingFonts()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Label3 = new System.Windows.Forms.Label();
this.Label2 = new System.Windows.Forms.Label();
this.Label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// Label3
//
this.Label3.BackColor = System.Drawing.Color.White;
this.Label3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.Label3.Location = new System.Drawing.Point(272, 168);
this.Label3.Name = "Label3";
this.Label3.Size = new System.Drawing.Size(128, 16);
this.Label3.TabIndex = 5;
this.Label3.Text = " ClearTypeGridFit";
//
// Label2
//
this.Label2.BackColor = System.Drawing.Color.White;
this.Label2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.Label2.Location = new System.Drawing.Point(272, 100);
this.Label2.Name = "Label2";
this.Label2.Size = new System.Drawing.Size(128, 16);
this.Label2.TabIndex = 4;
this.Label2.Text = " AntiAliasGridFit";
//
// Label1
//
this.Label1.BackColor = System.Drawing.Color.White;
this.Label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.Label1.Location = new System.Drawing.Point(272, 36);
this.Label1.Name = "Label1";
this.Label1.Size = new System.Drawing.Size(128, 16);
this.Label1.TabIndex = 3;
this.Label1.Text = " SingleBitPerPixelGridFit";
//
// SmoothingFonts
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
this.ClientSize = new System.Drawing.Size(440, 314);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.Label3,
this.Label2,
this.Label1});
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.Name = "SmoothingFonts";
this.Text = "SmoothingFonts";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.SmoothingFonts_Paint);
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new SmoothingFonts());
}
private void SmoothingFonts_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Font TextFont = new Font("Times New Roman", 25, FontStyle.Italic);
e.Graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;
e.Graphics.DrawString("Sample Text", TextFont, Brushes.Black, 20, 20);
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
e.Graphics.DrawString("Sample Text", TextFont, Brushes.Black, 20, 85);
e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
e.Graphics.DrawString("Sample Text", TextFont, Brushes.Black, 20, 150);
}
}
}
Write font metrics for Arial font family
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class MainClass {
public static void Main() {
String formatString = "{0,-16}{1,8}{2,9}{3,10}{4,14}";
Console.WriteLine(formatString, "Font Family Name", "Ascent", "Descent","EmHeight", "Line Spacing");
FontFamily ff = new FontFamily("Arial");
Console.WriteLine(formatString, ff.GetName(0),
ff.GetCellAscent(FontStyle.Regular),
ff.GetCellDescent(FontStyle.Regular),
ff.GetEmHeight(FontStyle.Regular),
ff.GetLineSpacing(FontStyle.Regular));
}
}
Write font metrics for Courier New font family
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class MainClass {
public static void Main() {
String formatString = "{0,-16}{1,8}{2,9}{3,10}{4,14}";
Console.WriteLine(formatString, "Font Family Name", "Ascent", "Descent","EmHeight", "Line Spacing");
// Write font metrics for Courier New font family
FontFamily ff = new FontFamily("Courier New");
Console.WriteLine(formatString, ff.GetName(0),
ff.GetCellAscent(FontStyle.Regular),
ff.GetCellDescent(FontStyle.Regular),
ff.GetEmHeight(FontStyle.Regular),
ff.GetLineSpacing(FontStyle.Regular));
}
}
Write font metrics for Times New Roman font family
using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class MainClass {
public static void Main() {
String formatString = "{0,-16}{1,8}{2,9}{3,10}{4,14}";
Console.WriteLine(formatString, "Font Family Name", "Ascent", "Descent","EmHeight", "Line Spacing");
FontFamily ff = new FontFamily("Times New Roman");
Console.WriteLine(formatString, ff.GetName(0),
ff.GetCellAscent(FontStyle.Regular),
ff.GetCellDescent(FontStyle.Regular),
ff.GetEmHeight(FontStyle.Regular),
ff.GetLineSpacing(FontStyle.Regular));
}
}