程序安装字体或直接调用非注册字体[c#]
.安装字体
//程序直接将字体文件安装的系统中。
函数声明:
[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;
} 函数的使用:
fonts.installFont(字体文件, 字体名称)//fonts类名
fonts.installFont("C39P36DmTt.TTF", "C39P36DmTt") .调用字体文件
//程序直接调用字体文件,不用安装到系统字库中。
设置字体对象:
String ls_appPath = System.Windows.Forms.Application.StartupPath + "\\font\\";//font是程序目录下放字体的文件夹
String fontFile1 = ls_appPath + "C39P36DmTt.TTF";
String fontFile2 = ls_appPath + "GWGLYPTT.TTF";
......
pfc.AddFontFile(fontFile1);//字体文件的路径
pfc.AddFontFile(fontFile2);//字体文件的路径
........
Font myFont1 = new Font(pfc.Families[], , FontStyle.Regular, GraphicsUnit.Point, );//myFont1就是你创建的字体对象
Font myFont2 = new Font(pfc.Families1], , FontStyle.Bold | FontStyle.Regular); 使用字体:
label1.Font = myFont1;

最新文章

  1. python之消息队列
  2. 21.TFS文件系统搭建笔记
  3. 用CSS3在手机上写弹出框,遮盖层
  4. 简单几何(凸包+枚举) POJ 1873 The Fortified Forest
  5. glsl水包含倒影的实现(rtt) [转]
  6. SQL查询各阶段的统计信息
  7. iOS开发之UIDevice通知
  8. 5.Java 加解密技术系列之 DES
  9. java System.currentTimeMillis()毫秒值和具体日期值互相转换
  10. T-SQL 中的CROSS JOIN用法(半翻译)
  11. kubernetes系列09—Ingress控制器详解
  12. js javascript 简易随机值穿插加解密【原】
  13. ubuntu下查看磁盘读写情况
  14. BN_batch normalization
  15. CodeForces 464E The Classic Problem | 呆克斯歘 主席树维护高精度
  16. 基于nginx + lua实现的反向代理动态更新
  17. windows应急响应入侵排查思路
  18. 日志框架 NLog
  19. 分布式锁的理解,java自带的锁为什么会失效
  20. 【VS2015】未能创建 Visual C# 2015编译器

热门文章

  1. Win7下的flutter环境安装配置
  2. 元素大小-偏移量(offset)客户区大小(client)滚动大小(scroll)
  3. L1-045 宇宙无敌大招呼
  4. JAVA中int转string及String.valueOf()的使用
  5. java程序员常用的八个工具
  6. Python 之 numpy 和 tensorflow 中的各种乘法(点乘和矩阵乘)
  7. Invalid /admin/*.jsp in filter mapping
  8. Pandas 命令整理
  9. ajax请求成功回调函数没有执行问题
  10. JAVA爬取百度贴吧图片