2009-08-20 4 views
2

Je dois installer un site Web Myriad pro. otf sur la machine client en utilisant C#. Veuillez suggérer. i essayé d'installer selon le code mentionné dans le postComment installer OpenType (type de fichier: otf) Police sur l'application Commencer à utiliser C#

[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); 
} 

Après l'exécution du code je peux voir l'entrée dans le registre et est également copié dans le dossier système Fonts, mais je ne suis pas en mesure de voir la police de Microsoft Office mot.

Est-ce que je manque quelque chose ici?

Répondre

1

Où est dit

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

devrait-il pas être

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

C:\windows\fonts\ 

est le dossier de la police de l'utilisateur?

Questions connexes