[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1047
  • Last Modified:

Path to font file selected via FontDiolog in C#

Hello,

I am using the excellent font library called freetype to render fonts with OpenGL.  The freetype library requires access to the actual  font files (e.g. C:\WINDOWS\Fonts\ARIALBI.TTF - Arial Bold Italic).

I would like to use the standard FontDialog via .NET and C# to select the font I want freetype to render.  After I select my font with the standard FontDialog I can get the fontDialog.Font, but this gives me no information as to where the actual font file lives on the hard drive.  I need the full path to the font file representing my selection via the FontDialog so that I can pass the file name to the freetype library so that it can extract the font information to then be rendered with OpenGL.

Any idea how I can get the path to the actual font file that represents the selection made with the standard FontDialog?

Thanks you,

Per
0
pergull
Asked:
pergull
1 Solution
 
silemoneCommented:
Well it may be easier to just include the font file in your project, maybe in your bin file so that incase the user doesn't have it available, then its there...plus it makes your program work on all machines...
0
 
Wayne Taylor (webtubbs)Commented:
Hi pergull,

Take a look at the below example. It makes use of code from here -> http:/Q_23337167.html

Regards,

Wayne
using System.Runtime.InteropServices;
using Microsoft.Win32; 
class Form1
{
    
Button1_Click(System.Object sender, System.EventArgs e)
    {
        
        string FontFolder = EnvironmentFolders.GetPath(EnvironmentFolders.SpecialFolder.Fonts);
        FontDialog fd = new FontDialog();
        if (fd.ShowDialog == Windows.Forms.DialogResult.OK) {
            
            RegistryKey rk = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts\\");
            if (rk == null) {
                Interaction.MsgBox("key not found");
            }
            else {
                string fontName = fd.Font.Name + " (TrueType)";
                tbFontName.Text = fontName;
                tbFontPath.Text = FontFolder + "\\" + rk.GetValue(fontName).ToString;
                
            }
            
        }
    }
} 
public class EnvironmentFolders s
{ 
    
    public enum SpecialFolder 
    { 
        AdministrativeTools = 48, 
        //{user name}\Start Menu\Programs\Administrative Tools 
        ApplicationData = 26, 
        //{user name}\Application Data 
        CommonAdministrativeTools = 47, 
        //All Users\Start Menu\Programs\Administrative Tools 
        CommonApplicationData = 35, 
        //All Users\Application Data 
        CommonDesktopDirectory = 25, 
        //All Users\Desktop 
        CommonDocuments = 46, 
        //All Users\Documents 
        CommonFavorites = 31, 
        CommonNonLocalizedStartup = 30, 
        //non localized common startup 
        CommonPrograms = 23, 
        //All Users\Programs 
        CommonStartMenu = 22, 
        //All Users\Start Menu 
        CommonStartup = 24, 
        //All Users\Startup 
        CommonTemplates = 45, 
        //All Users\Templates 
        ControlPanel = 3, 
        //My Computer\Control Panel 
        Cookies = 33, 
        DesktopDirectory = 16, 
        //{user name}\Desktop 
        Favorites = 6, 
        //{user name}\Favorites 
        Fonts = 20, 
        //windows\fonts 
        History = 34, 
        InternetCache = 32, 
        LocalApplicationData = 28, 
        //{user name}\Local Settings\Application Data (non roaming) 
        MyDocuments = 5, 
        //My Documents 
        MyPictures = 39, 
        //C:\Program Files\My Pictures 
        NetworkShortcuts = 19, 
        //{user name}\nethood 
        NonLocalizedStartup = 29, 
        //non localized startup 
        Printers = 4, 
        //My Computer\Printers 
        PrintHood = 27, 
        //{user name}\PrintHood 
        ProgramFiles = 38, 
        //C:\Program Files 
        ProgramFilesCommon = 43, 
        //C:\Program Files\Common 
        Programs = 2, 
        //Start Menu\Programs 
        Recent = 8, 
        //{user name}\Recent 
        RecycleBin = 10, 
        //{desktop}\Recycle Bin 
        SendTo = 9, 
        //{user name}\SendTo 
        StartMenu = 11, 
        //{user name}\Start Menu 
        Startup = 7, 
        //Start Menu\Programs\Startup 
        System = 37, 
        //GetSystemDirectory() 
        Templates = 21, 
        UserProfile = 40, 
        //USERPROFILE 
        Windows = 36 
        //GetWindowsDirectory() 
    } 
    
    [DllImport("shfolder.dll", CharSet = CharSet.Auto)] 
    private static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken, int dwFlags, StringBuilder lpszPath); 
    
    /// <summary> 
    /// Get an environment folder path for Windows environment folders 
    /// </summary> 
    /// <returns>A string pointing to the special path</returns> 
    /// <remarks></remarks> 
    public static string GetPath(SpecialFolder folder) 
    { 
        StringBuilder lpszPath = new StringBuilder(260); 
        SHGetFolderPath(IntPtr.Zero, (int)folder, IntPtr.Zero, 0, lpszPath); 
        return lpszPath.ToString(); 
    } 
    
}

Open in new window

0
 
pergullAuthor Commented:
Thank you very much.  I see that all the magic is through the registry.  I know exactly how to proceed.  Thanks again!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now