using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Drawing.Text; namespace itextcsharpDemo
{ /// <summary>
///
/// </summary>
public partial class Form4 : Form
{ int result = -1;
int error = 0; [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, // message
int wParam, // first message parameter
int lParam // second message parameter
);
[DllImport("gdi32")]
public static extern int AddFontResource(string lpFileName);
/// <summary>
///
/// </summary>
/// <param name="FontFileName"></param>
/// <param name="FontName"></param>
private void installFont(string FontFileName,string FontName)
{ string WinFontDir = "C:\\windows\\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 + "\\" + FontName;
if (!File.Exists(FontPath))
{
File.Copy(FontFileName, FontPath); //System.Windows.Forms.Application.StartupPath + "\\DS-Digital Bold Italic.TTF"
Ret = AddFontResource(FontPath); Res = SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
Ret = WriteProfileString("fonts", FontName + "(TrueType)", FontFileName);
}
} [DllImport("gdi32", EntryPoint = "AddFontResource")]
public static extern int AddFontResourceA(string lpFileName);
//[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); //
[DllImport("gdi32.dll", EntryPoint = "RemoveFontResourceW", SetLastError = true)]
public static extern int RemoveFontResource([In][MarshalAs(UnmanagedType.LPWStr)]
string lpFileName); /// <summary>
/// Installs font on the user's system and adds it to the registry so it's available on the next session
/// Your font must be included in your project with its build path set to 'Content' and its Copy property
/// set to 'Copy Always'
/// win 10 没有权限 涂聚文注
/// </summary>
/// <param name="contentFontName">Your font to be passed as a resource (i.e. "myfont.tff")</param>
private static void RegisterFont(string contentFontName)
{
try
{
// Creates the full path where your font will be installed
var fontDestination = Path.Combine(System.Environment.GetFolderPath
(System.Environment.SpecialFolder.Fonts), contentFontName); if (!File.Exists(fontDestination))
{
// Copies font to destination
System.IO.File.Copy(Path.Combine(System.IO.Directory.GetCurrentDirectory(), contentFontName), fontDestination); // Retrieves font name
// Makes sure you reference System.Drawing
PrivateFontCollection fontCol = new PrivateFontCollection();
fontCol.AddFontFile(fontDestination);
var actualFontName = fontCol.Families[0].Name; //Add font
AddFontResource(fontDestination);
//Add registry entry
Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts",
actualFontName, contentFontName, RegistryValueKind.String);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
} /// <summary>
///
/// </summary>
/// <param name="NombreFnt"></param>
/// <param name="RutaFnt"></param>
internal static void InstalarFuente(string NombreFnt, string RutaFnt)
{
string CMD = string.Format("copy /Y \"{0}\" \"%WINDIR%\\Fonts\" ", RutaFnt);
EjecutarCMD(CMD); System.IO.FileInfo FInfo = new System.IO.FileInfo(RutaFnt);
CMD = string.Format("reg add \"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts\" /v \"{0}\" /t REG_SZ /d {1} /f", NombreFnt, FInfo.Name);
EjecutarCMD(CMD);
}
/// <summary>
///
/// </summary>
/// <param name="Comando"></param>
public static void EjecutarCMD(string Comando)
{
System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo("cmd.exe");
Info.Arguments = string.Format("/c {0}", Comando);
Info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
System.Diagnostics.Process.Start(Info);
}
/// <summary>
///
/// </summary>
public Form4()
{
InitializeComponent();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form4_Load(object sender, EventArgs e)
{ }
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
try
{ //汉仪篆书繁.ttf
//新蒂绿豆体.ttf
string fontname = System.Windows.Forms.Application.StartupPath + @"\font\汉仪篆书繁.ttf"; // "汉仪篆书繁.ttf";// //win 10 安装成功 Senty Pea 新蒂绿豆体
//windows/syste32/shell32.dll
//Shell32.Shell shell = new Shell32.Shell();
//Shell32.Folder fontFolder = shell.NameSpace(0x14);
//fontFolder.CopyHere(fontname); //没有权限
//RegisterFont(fontname); installFont(fontname, "汉仪篆书繁.ttf"); //无效
//result = AddFontResource(fontname);
//error = Marshal.GetLastWin32Error();
//if (error != 0)
//{
// MessageBox.Show(new Win32Exception(error).Message);
//} }
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
FontFamily[] fontList = new System.Drawing.Text.InstalledFontCollection().Families;
InstalledFontCollection installedFonts = new InstalledFontCollection();
List<FontList> list = new List<FontList>();
int id = 0;
foreach (FontFamily font in installedFonts.Families)
{
FontList l = new FontList();
l.FontName = font.Name;
l.FontId = id;
list.Add(l);
id++;
} listBox1.DataSource = list;
listBox1.ValueMember = "FontId";
listBox1.DisplayMember = "FontName"; }
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
object o = this.listBox1.SelectedItem;
FontList od = new FontList();
od = o as FontList;
//MessageBox.Show(od.FontName);
this.textBox1.Text = od.FontName;
}
}
}
/// <summary>
///
/// </summary>
public class FontList
{
public int FontId { get; set; }
public string FontName { get; set; }
}
}

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;
using System.Security.Principal; namespace itextcsharpDemo
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{ //使用权限
var wi = WindowsIdentity.GetCurrent();
var wp = new WindowsPrincipal(wi); bool runAsAdmin = wp.IsInRole(WindowsBuiltInRole.Administrator); //管理员权限 if (!runAsAdmin)
{
// It is not possible to launch a ClickOnce app as administrator directly,
// so instead we launch the app as administrator in a new process.
var processInfo = new ProcessStartInfo(Assembly.GetExecutingAssembly().CodeBase); // The following properties run the new process as administrator
processInfo.UseShellExecute = true;
processInfo.Verb = "runas"; // Start the new process
try
{
Process.Start(processInfo);
}
catch (Exception)
{
// The user did not allow the application to run as administrator
MessageBox.Show("Sorry, but I don't seem to be able to start " +
"this program with administrator rights!");
} // Shut down the current process
Application.Exit();
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new ManageForm());
}
}
}
}

  https://stackoverflow.com/questions/14796162/how-to-install-a-windows-font-using-c-sharp

Installing Fonts programatically C#的更多相关文章

  1. EBS条形码打印

    Oracle  提供两种方式实现 128 码的编码 第一种方式是使用 Reports Builder 实现对 128 码编码, 在 Metalink 305090.1[1]  有 比较详尽的描述,其中 ...

  2. $\TeX$ Gyre 字体安装过程与问题解决

    目录 安装过程 1. 下载字体包 2. 安装字体 3. 测试范例文件 本文地址 https://www.cnblogs.com/oberon-zjt0806/p/13672426.html 本文只是一 ...

  3. bootstrap之google fonts

    bootstrap之google fonts 在学习一个bootstrap时,看到了一行引用代码:@import url(http://fonts.googleapis.com/css?family= ...

  4. 【问题&解决】fonts/fontawesome-webfont.woff2 404 (Not Found)

    问题: 虽然网页正常显示和运行,但是有2个字体文件出现404错误.像笔者这种强迫症是接受不了的. 解决: 因为笔者的服务器是虚拟主机,只需要在主机控制器平台添加对应的MIME类型即可. 这样服务器就支 ...

  5. installing mysql,this may take a few minutes,hold on plz wdcp卡住解决办法

    centos6安装wdcp时make in progress卡住的解决办法 今天在一台centos6的vps上安装wdcp出现的这个问题,到安装程序滚动至下面这里时出现"卡死". ...

  6. CSS3 笔记三(Shadow/Text/Web Fonts)

    CSS3 Shadow Effects text-shadow box-shadow 1> text-shadow The text-shadow property adds shadow to ...

  7. fonts.googleapis.com 加载慢的解决方法

    把:fonts.googleapis.com 替换成 fonts.useso.com

  8. 最近在做外贸网站的时候,需要大量的字体来充实页面,就学习了怎么引用Google Fonts

    第一步,FQ进入谷歌官方字体网站:https://fonts.google.com  妥妥的. 第二步,点击你所选择字体演示块的右上角的加号,然后你所选择的字体会形成引用链接以及你所要写的css样式. ...

  9. fonts.useso.com 访问变慢

    fonts.useso.com 替换为        fonts.lug.ustc.edu.cn ajax.useso.com   替换为       ajax.lug.ustc.edu.cn the ...

随机推荐

  1. Eclipse运行时发生An internal error occurred during:“**************” 的解决办法

    2015-05-20 原因分析: 当前工作目录下的 .project 文件 不一致 例如1: 南京大学 Mooctest 提交考试试卷时出现的:An internal error occurred d ...

  2. SDWebImage之SDWebImageDownloader

    SDWebImageDownloader完成了对网络图片的异步下载工作,准确说这个类是一个文件下载的工具类,真正的网络请求是在继承于NSOperation的SDWebImageDownloaderOp ...

  3. MySQL--事务隔离级别RR和RC的异同

    在MySQL中,事务隔离级别RC(read commit)和RR(repeatable read)两种事务隔离级别基于多版本并发控制MVCC(multi-version concurrency con ...

  4. PMS构造函数以及apk如何扫描

    一.PackageManagerService构造函数 1.创建data目录下面以及文件(settings的构造函数),然后再添加6个SharedUserSetting 2.开始扫描并且解析APK 3 ...

  5. InnoDB体系架构(四)Master Thread工作方式

    Master Thread工作方式 在前面的文章:InnoDB体系架构——后台线程 说到:InnoDB存储引擎的主要工作都是在一个单独的后台线程Master Thread中完成.这篇具体介绍该线程的具 ...

  6. js中的行为委托和无类编程

    概述 <你不知道的JavaScript>中有这么一段话:不幸的是,将类和继承的设计模式思维带入Javascript的想法是你所做的最坏的事情,因为语法可能会让你迷惑不已,让你以为真的有类这 ...

  7. RabbitMQ服务端配置详解

    RabbitMQ支持三种配置方式: 1) 读取环境变量中配置, 这包括shell中环境变量和rabbitmq-env.conf/rabbitmq-env-conf.bat文件中配置的环境变量 可配置如 ...

  8. C# TableLayoutPanel使用方法

    一.利用TableLayoutPanel类展示表格,以10行5列为例 第1步:在前台创建一个panel,使TableLayoutPanel对象填充其内部. 第2步:创建TableLayoutPanel ...

  9. JavaScript “跑马灯”抽奖活动代码解析与优化(二)

    既然是要编写插件.那么叫做"插件"的东西肯定是具有的某些特征能够满足我们平时开发的需求或者是提高我们的开发效率.那么叫做插件的东西应该具有哪些基本特征呢?让我们来总结一下: 1.J ...

  10. php -- 目录、路径、磁盘

    ----- 028-dir.php ----- <!DOCTYPE html> <html> <head> <meta http-equiv="co ...