tags:

views:

1082

answers:

1

I have to install a Myriad pro web.otf font on the client machine using C# . Please Suggest. i tried installing as per the code mentioned in the post

   [DllImport("gdi32", EntryPoint = "AddFontResource")]
    public static extern int AddFontResourceA(string lpFileName);

    [System.Runtime.InteropServices.DllImport("shfolder.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]

    private static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder,
    IntPtr hToken, int dwFlags, StringBuilder lpszPath);

    private const int CSIDL_FONTS = 0x0014;
    private const int MAX_PATH = 260;

    // PInvoke to 'register' fonts and broadcast addition
    [System.Runtime.InteropServices.DllImport("gdi32.dll")]
    private static extern int AddFontResource(string lpszFilename);
    [System.Runtime.InteropServices.DllImport("gdi32.dll")]
    private static extern int CreateScalableFontResource(uint fdwHidden, string
    lpszFontRes, string lpszFontFile, string lpszCurrentPath);

    private static IntPtr HWND_BROADCAST = new IntPtr(0xffff);
    private const uint WM_FONTCHANGE = 0x001D;

    public Window1()
    {
        InitializeComponent();
        InstallFont();
    }

    internal static void InstallFont()
    {

        string fontsPath = GetFontsPath();

        string ttfFile = System.IO.Path.Combine(fontsPath, "MyriadPro-Semibold.otf");

        System.IO.File.Copy(@"C:\MyriadPro-Semibold.otf", ttfFile);

        int ret;

        if (System.IO.File.Exists(ttfFile))
        {
            //Add font resource
            ret = AddFontResource(ttfFile);
            //Add registry entry so the font is also available next session
            Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts",
                "MyriadPro-Semibold(TrueType)", "MyriadPro-Semibold.otf", RegistryValueKind.String);
            //Broadcast to let all top-level windows know about change
            ret = SendMessage(HWND_BROADCAST, WM_FONTCHANGE, new IntPtr(0), new IntPtr(0));
        }
    }


    private static string GetFontsPath()
    {
        StringBuilder sb = new StringBuilder(MAX_PATH);
        SHGetFolderPath(IntPtr.Zero, CSIDL_FONTS, IntPtr.Zero, 0, sb);
        return sb.ToString();
    }

    [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    private static extern int SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam,IntPtr lParam);
}

After executing the code i can see the entry in registry and is also copied to the system Fonts folder but i am not able to view the font from Microsoft office word.

am i missing any thing here ?

A: 

Where is says

Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts",
                "MyriadPro-Semibold(TrueType)", "MyriadPro-Semibold.otf", RegistryValueKind.String);

Should it not be

Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts",
                "MyriadPro-Semibold(TrueType)", "C:\windows\fonts\MyriadPro-Semibold.otf", RegistryValueKind.String);

where

C:\windows\fonts\

is the user's font folder?

Charlie Salts