客户端字体安装

不懂装懂 2019-02-23 02:34:10
从网上找的安装字体的代码
[DllImport("kernel32.dll", SetLastError = true)] 
static extern int WriteProfileString(string lpszSection, string lpszKeyName, string lpszString);

[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, // handle to destination window
uint Msg,
int wParam,
int lParam
);

[DllImport("gdi32")]
public static extern int AddFontResource(string lpFileName);

public static bool installFont(string FontFileName, string FontName)
{
string WinFontDir = System.Environment.GetEnvironmentVariable("WINDIR") + "\\fonts";
//string FontFileName = "DS-Digital Bold Italic.TTF";
//string FontName = "DS-Digital Bold Italic";
int Ret;
int Res;
string FontPath;
//const int WM_FONTCHANGE = 0x001D;
//const int HWND_BROADCAST = 0xffff;
FontPath = WinFontDir + "\\" + FontFileName;
try
{
if (!File.Exists(FontPath))
{
File.Copy(System.Windows.Forms.Application.StartupPath + "\\font\\" + FontFileName, FontPath); //font是程序目录下放字体的文件夹
Ret = AddFontResource(FontPath);

//Res = SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
//WIN7下编译会出错,不清楚什么问题。注释就行了。
Ret = WriteProfileString("fonts", FontName + "(TrueType)", FontFileName);
}
}catch(Exception ex)
{
MessageBox.Show("[ "+FontName + " ]字体安装失败!原因:"+ex.Message);
return false;
}
return true;
}

执行到File.Copy时报“对路径的访问被拒绝”的错误,权限问题的话,要修改的客户端太多,现在需要解决客户端安装的问题,该如何处理
...全文
130 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
请不要随便安装字体到系统,你自己封装自己的字体自己用是不用任何权限的。 https://blog.csdn.net/soft2buy/article/details/16840515 参考上面的文章你就能把你调用的字体封装到自己文件夹了。 同时你还可以考虑一下如果字体不慎不存在是否使用系统默认字体的设置。
lindexi_gd 2019-02-23
  • 打赏
  • 举报
回复
安装字体需要管理员权限

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧