Installing Fonts programatically C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | 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 ; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | 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
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2015-09-01 10个出色的NoSQL数据库