0

I have an embedded font in my winform application.


Here is the codes :

This is what worked for me in VS 2013, without having to use an unsafe block.

Embed the resource

  1. Double-click Resources.resx, and in the toolbar for the designer click Add Resource/Add Existing File and select your .ttf file
  2. In your solution explorer, right-click your .ttf file (now in a Resources folder) and go to Properties. Set the Build Action to "Embedded Resource"

Load the font into memory

  1. Add using System.Drawing.Text; to your Form1.cs file

  2. Add code above and inside your default constructor to create the font in memory (without using "unsafe" as other examples have shown). Below is my entire Form1.cs:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Reflection;
    
    using System.Drawing.Text;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            [System.Runtime.InteropServices.DllImport("gdi32.dll")]
            private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont,
                IntPtr pdv, [System.Runtime.InteropServices.In] ref uint pcFonts);
    
            private PrivateFontCollection fonts = new PrivateFontCollection();
    
            Font myFont;
    
            public Form1()
            {
                InitializeComponent();
    
                byte[] fontData = Properties.Resources.MyFontName;
                IntPtr fontPtr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(fontData.Length);
                System.Runtime.InteropServices.Marshal.Copy(fontData, 0, fontPtr, fontData.Length);
                uint dummy = 0;
                fonts.AddMemoryFont(fontPtr, Properties.Resources.MyFontName.Length);
                AddFontMemResourceEx(fontPtr, (uint)Properties.Resources.MyFontName.Length, IntPtr.Zero, ref dummy);
                System.Runtime.InteropServices.Marshal.FreeCoTaskMem(fontPtr);
    
                myFont = new Font(fonts.Families[0], 16.0F);
            }
        }
    }
    

Use your font

  1. Add a label to your main form, and add a load event to set the font in Form1.cs:

    private void Form1_Load(object sender, EventArgs e)
    {
        label1.Font = myFont;
    }
    

Now i want to change Only Font-Family of all controls on WinForm using c#, Not only a label & Not Font-Size.
Mean i want to keep Font-Size of all controls.
Here is related site about this issue.
how-to-change-font-family-of-all-controls-on-window
But in codeproject answers they changed both font-family & font-size.

How can i change only font-family of all controls, not font-size?

SilverLight
  • 19,668
  • 65
  • 192
  • 300
  • 1
    Create a suitable font with the size you want for each control. There's no way to separate font size from family since the object contains both – Sami Kuhmonen Nov 30 '20 at 17:48
  • I want to set font-size of each control separately. But what is the font-family name? Can you show me how can i do that? – SilverLight Nov 30 '20 at 17:56
  • I tried this : radLabel1.Font = new Font(myFont.Name, 20.0F); > Does not work!!!!!!!!!!!!!!!!!! – SilverLight Nov 30 '20 at 18:08
  • You already have code there that creates a font with a specified size. Use the same for each size you want. Just keep the family object in a field, or create the wanted sizes beforehand. – Sami Kuhmonen Nov 30 '20 at 18:10
  • Read the notes here: [How to properly render an embedded Font?](https://stackoverflow.com/a/64512339/7444103) -- Unsafe and Safe methods [here](https://stackoverflow.com/a/57231407/7444103) -- You can keep the same Font size when you replace a FontFamily, but the actual Font size depends on the Font. You can set the height in pixels, but the presentation will be of course different, as the overall width of the text. – Jimi Nov 30 '20 at 18:34
  • Guys, Thanks fot help and comments. – SilverLight Nov 30 '20 at 19:24

0 Answers0