using System; // IntPtr and EventArgs
using System.Drawing; // Font and PointF
using System.Drawing.Text; // PrivateFontCollection
using System.Windows.Forms; // Form inheritance
using System.IO; // Stream
using System.Reflection; // Assembly
using System.Linq; // OfType
If you don't like using Linq you can remove it and just add an if statement in the PaintEventHandler. Probably a good idea if you have a lot of labels.
static class MyGlobal
{
private static PrivateFontCollection _pfc;
public static PrivateFontCollection pfc
{
get { return _pfc; }
set { _pfc = value; }
}
} // end of MyGlobal class
By keeping the PFC in a static class it can be accessed by more than one form.
this.Load += new System.EventHandler(this.onLoadEvent);
Once the EventHandler has been added the actual handler needs to be added:
private void onLoadEvent(object sender, EventArgs e)
{
MyGlobal.pfc = new PrivateFontCollection();
Assembly assembly = Assembly.GetExecutingAssembly();
string assemblyName = assembly.FullName.Substring(0, assembly.FullName.IndexOf(","));
string fontFname = "MeathFLF.ttf"; // use the name of the file you embedded.
Stream fontStrm = assembly.GetManifestResourceStream( assemblyName + "." + fontFname );
byte[] fdata = new byte[fontStrm.Length];
fontStrm.Read(fdata, 0, (int)fontStrm.Length);
fontStrm.Close();
fontStrm.Dispose();
unsafe // Why I hate C# sometimes
{
fixed (byte* pFontData = fdata)
{
MyGlobal.pfc.AddMemoryFont((IntPtr)pFontData, fdata.Length);
}
} // end of unsafe code
this.Paint += new System.Windows.Forms.PaintEventHandler(this.onPaintEvent);
} // End of onLoadEvent
The above code is fully commented in the downloadable project.
private void onPaintEvent(object sender, PaintEventArgs e){
foreach (Label lbl in this.Controls.OfType<Label>())
{
if (lbl.Visible == false)
{
using (Font myEmbeddedFont = new Font(MyGlobal.pfc.Families[0], lbl.Font.Size, lbl.Font.Style))
{
PointF myPointF = new PointF(lbl.Location.X, lbl.Location.Y);
if (lbl.Font.Size > 10)
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
else
e.Graphics.TextRenderingHint = TextRenderingHint.SystemDefault;
SolidBrush myBrush = new SolidBrush(lbl.ForeColor);
e.Graphics.DrawString(lbl.Text, myEmbeddedFont, myBrush, myPointF);
}
} // end of if Visible
} // end of foreach
} // end of onPaintEvent
This a PaintEvenHandler for Form1, it does not handle paint events for nested containers. If you use the embedded font in a nested container you will need to add a PaintEventHandler for the containers. Or better yet use the sender object to determine the ControlCollection for the foreach, then just reuse the same Handler. (something for another day)
using System; // IntPtr and EventArgs
using System.Drawing; // Font and PointF
using System.Drawing.Text; // PrivateFontCollection
using System.Windows.Forms; // Form inheritance
using System.IO; // Stream
using System.Reflection; // Assembly
using System.Linq; // OfType
If you don't like using Linq you can remove it and just add an if statement in the PaintEventHandler. Probably a good idea if you have a lot of labels.
static class MyGlobal
{
private static PrivateFontCollection _pfc;
public static PrivateFontCollection pfc
{
get { return _pfc; }
set { _pfc = value; }
}
} // end of MyGlobal class
By keeping the PFC in a static class it can be accessed by more than one form.
this.Load += new System.EventHandler(this.onLoadEvent);
Once the EventHandler has been added the actual handler needs to be added:
private void onLoadEvent(object sender, EventArgs e)
{
MyGlobal.pfc = new PrivateFontCollection();
Assembly assembly = Assembly.GetExecutingAssembly();
string assemblyName = assembly.FullName.Substring(0, assembly.FullName.IndexOf(","));
string fontFname = "MeathFLF.ttf"; // use the name of the file you embedded.
Stream fontStrm = assembly.GetManifestResourceStream( assemblyName + "." + fontFname );
byte[] fdata = new byte[fontStrm.Length];
fontStrm.Read(fdata, 0, (int)fontStrm.Length);
fontStrm.Close();
fontStrm.Dispose();
unsafe // Why I hate C# sometimes
{
fixed (byte* pFontData = fdata)
{
MyGlobal.pfc.AddMemoryFont((IntPtr)pFontData, fdata.Length);
}
} // end of unsafe code
this.Paint += new System.Windows.Forms.PaintEventHandler(this.onPaintEvent);
} // End of onLoadEvent
The above code is fully commented in the downloadable project.
private void onPaintEvent(object sender, PaintEventArgs e){
foreach (Label lbl in this.Controls.OfType<Label>())
{
if (lbl.Visible == false)
{
using (Font myEmbeddedFont = new Font(MyGlobal.pfc.Families[0], lbl.Font.Size, lbl.Font.Style))
{
PointF myPointF = new PointF(lbl.Location.X, lbl.Location.Y);
if (lbl.Font.Size > 10)
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
else
e.Graphics.TextRenderingHint = TextRenderingHint.SystemDefault;
SolidBrush myBrush = new SolidBrush(lbl.ForeColor);
e.Graphics.DrawString(lbl.Text, myEmbeddedFont, myBrush, myPointF);
}
} // end of if Visible
} // end of foreach
} // end of onPaintEvent
This a PaintEvenHandler for Form1, it does not handle paint events for nested containers. If you use the embedded font in a nested container you will need to add a PaintEventHandler for the containers. Or better yet use the sender object to determine the ControlCollection for the foreach, then just reuse the same Handler. (something for another day)
Thanks bro.
using System; // IntPtr and EventArgs
using System.Drawing; // Font and PointF
using System.Drawing.Text; // PrivateFontCollection
using System.Windows.Forms; // Form inheritance
using System.IO; // Stream
using System.Reflection; // Assembly
using System.Linq; // OfType
If you don't like using Linq you can remove it and just add an if statement in the PaintEventHandler. Probably a good idea if you have a lot of labels.
static class MyGlobal
{
private static PrivateFontCollection _pfc;
public static PrivateFontCollection pfc
{
get { return _pfc; }
set { _pfc = value; }
}
} // end of MyGlobal class
By keeping the PFC in a static class it can be accessed by more than one form.
this.Load += new System.EventHandler(this.onLoadEvent);
Once the EventHandler has been added the actual handler needs to be added:
private void onLoadEvent(object sender, EventArgs e)
{
MyGlobal.pfc = new PrivateFontCollection();
Assembly assembly = Assembly.GetExecutingAssembly();
string assemblyName = assembly.FullName.Substring(0, assembly.FullName.IndexOf(","));
string fontFname = "MeathFLF.ttf"; // use the name of the file you embedded.
Stream fontStrm = assembly.GetManifestResourceStream( assemblyName + "." + fontFname );
byte[] fdata = new byte[fontStrm.Length];
fontStrm.Read(fdata, 0, (int)fontStrm.Length);
fontStrm.Close();
fontStrm.Dispose();
unsafe // Why I hate C# sometimes
{
fixed (byte* pFontData = fdata)
{
MyGlobal.pfc.AddMemoryFont((IntPtr)pFontData, fdata.Length);
}
} // end of unsafe code
this.Paint += new System.Windows.Forms.PaintEventHandler(this.onPaintEvent);
} // End of onLoadEvent
The above code is fully commented in the downloadable project.
private void onPaintEvent(object sender, PaintEventArgs e){
foreach (Label lbl in this.Controls.OfType<Label>())
{
if (lbl.Visible == false)
{
using (Font myEmbeddedFont = new Font(MyGlobal.pfc.Families[0], lbl.Font.Size, lbl.Font.Style))
{
PointF myPointF = new PointF(lbl.Location.X, lbl.Location.Y);
if (lbl.Font.Size > 10)
e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
else
e.Graphics.TextRenderingHint = TextRenderingHint.SystemDefault;
SolidBrush myBrush = new SolidBrush(lbl.ForeColor);
e.Graphics.DrawString(lbl.Text, myEmbeddedFont, myBrush, myPointF);
}
} // end of if Visible
} // end of foreach
} // end of onPaintEvent
This a PaintEvenHandler for Form1, it does not handle paint events for nested containers. If you use the embedded font in a nested container you will need to add a PaintEventHandler for the containers. Or better yet use the sender object to determine the ControlCollection for the foreach, then just reuse the same Handler. (something for another day)
Copyright © 2026, NextGenUpdate.
All Rights Reserved.