我必须使用C#在客户端计算机上安装Myriad pro web。 otf 字体。请建议。我尝试按照帖子中提到的代码进行安装
[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);
}
执行代码后,我可以看到注册表中的条目,也被复制到系统Fonts文件夹,但我无法从Microsoft office word查看字体。
我在这里错过任何东西吗?答案 0 :(得分:1)
在哪里说
Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts",
"MyriadPro-Semibold(TrueType)", "MyriadPro-Semibold.otf", RegistryValueKind.String);
不应该
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\
是用户的字体文件夹吗?