C#安装字体后使用问题
我在当前程序中安装了一个新的字体,但是在需要重新启动程序才能InstalledFontCollection集合里找到当前安装的字体,请问各位兄台,有没有办法在不重新启动程序的情况下使用该字体的方法?代码如下:public static bool installFont(string FontFileName, string FontName)
{
string WinFontDir = System.Environment.GetEnvironmentVariable("WINDIR") + "\\fonts";
string FontPath;
FontPath = WinFontDir + "\\" + FontFileName;
try
{
if (!File.Exists(FontPath))
{
File.Copy(System.Windows.Forms.Application.StartupPath + "\\font\\" + FontFileName, FontPath);
AddFontResource(FontPath);
}
}
catch (Exception ex)
{
MessageBox.Show("[ " + FontName + " ]字体安装失败!原因:" + ex.Message);
return false;
}
return true;
}
private void button1_Click(object sender, EventArgs e)
{
string fontNames = "";
InstalledFontCollection fonts = new InstalledFontCollection();
foreach (System.Drawing.FontFamily item in fonts.Families)
{
fontNames += item.Name + "|";
}
}
C# 字体 --------------------编程问答-------------------- 我也遇到了同样的问题,怎么解决的 ??? --------------------编程问答-------------------- 调用EnumFonts API
view sourceprint?
01.
public partial class Form1 : Form
02.
{
03.
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
04.
public class LOGFONT
05.
{
06.
public int lfHeight = 0;
07.
public int lfWidth = 0;
08.
public int lfEscapement = 0;
09.
public int lfOrientation = 0;
10.
public int lfWeight = 0;
11.
public byte lfItalic = 0;
12.
public byte lfUnderline = 0;
13.
public byte lfStrikeOut = 0;
14.
public byte lfCharSet = 0;
15.
public byte lfOutPrecision = 0;
16.
public byte lfClipPrecision = 0;
17.
public byte lfQuality = 0;
18.
public byte lfPitchAndFamily = 0;
19.
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
20.
public string lfFaceName = string.Empty;
21.
}
22.
23.
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
24.
public struct ENUMLOGFONTEX
25.
{
26.
public const int LF_FACESIZE = 32;
27.
public LOGFONT elfLogFont;
28.
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FACESIZE)]
29.
public string elfFullName;
30.
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FACESIZE)]
31.
public string elfStyle;
32.
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FACESIZE)]
33.
public string elfScript;
34.
}
35.
36.
[DllImport("gdi32.dll")]
37.
static extern int EnumFonts(IntPtr hdc, IntPtr lpFaceName,
38.
EnumFontExDelegate lpFontFunc, IntPtr lParam);
39.
40.
[DllImport("user32.dll", EntryPoint = "GetDC")]
41.
static extern IntPtr GetDC(IntPtr hWnd);
42.
43.
[DllImport("user32.dll")]
44.
static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
45.
46.
delegate int EnumFontExDelegate(ref ENUMLOGFONTEX lpelfe,
47.
IntPtr lpntme, int FontType, int lParam);
48.
49.
private int Callback(ref ENUMLOGFONTEX lpelfe, IntPtr lpntme,
50.
int FontType, int lParam)
51.
{
52.
if ((lpelfe.elfLogFont.lfPitchAndFamily & 3) != 1)
53.
return 1;
54.
else if (lpelfe.elfFullName[0] == '@')
55.
return 1;
56.
else if (listBox1.Items.IndexOf(lpelfe.elfFullName) != -1)
57.
return 1;
58.
59.
listBox1.Items.Add(lpelfe.elfFullName);
60.
61.
return 1;
62.
}
63.
64.
private void button1_Click(object sender, EventArgs e)
65.
{
66.
IntPtr dc = GetDC(IntPtr.Zero);
67.
try
68.
{
69.
EnumFontExDelegate myefd = new EnumFontExDelegate(Callback);
70.
EnumFonts(dc, IntPtr.Zero, myefd, IntPtr.Zero);
71.
}
72.
finally
73.
{
74.
ReleaseDC(IntPtr.Zero, dc);
75.
}
76.
}
77.
78.
public Form1()
79.
{
80.
InitializeComponent();
81.
button1.Click += button1_Click;
82.
}
83.
84.
}
补充:.NET技术 , C#