|
|
using System; |
|
|
using System.Windows; |
|
|
using System.Windows.Media; |
|
|
|
|
|
namespace WpfEditor |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
public class FontUsageExample |
|
|
{ |
|
|
private TextEditorControl textEditor; |
|
|
|
|
|
public FontUsageExample(TextEditorControl editor) |
|
|
{ |
|
|
textEditor = editor; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Example1_SetSystemFont() |
|
|
{ |
|
|
|
|
|
FontManager.SetFont(textEditor, "Consolas", 14); |
|
|
|
|
|
|
|
|
FontManager.SetFont(textEditor, "Arial", 16, FontWeights.Bold, FontStyles.Italic); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Example2_SetProgrammingFont() |
|
|
{ |
|
|
|
|
|
FontManager.SetProgrammingFont(textEditor, 14); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Example3_LoadCustomFont() |
|
|
{ |
|
|
string fontPath = @"C:\MyFonts\CustomFont.ttf"; |
|
|
|
|
|
if (FontManager.LoadFontFromFile(textEditor, fontPath, 14)) |
|
|
{ |
|
|
MessageBox.Show("自定义字体加载成功!"); |
|
|
} |
|
|
else |
|
|
{ |
|
|
MessageBox.Show("自定义字体加载失败,请检查文件路径。"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Example4_LoadEmbeddedFont() |
|
|
{ |
|
|
|
|
|
|
|
|
string resourcePath = "./Fonts/#MyFont"; |
|
|
|
|
|
if (FontManager.LoadFontFromResource(textEditor, resourcePath, 14)) |
|
|
{ |
|
|
MessageBox.Show("嵌入字体加载成功!"); |
|
|
} |
|
|
else |
|
|
{ |
|
|
MessageBox.Show("嵌入字体加载失败。"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Example5_UseFontConfig() |
|
|
{ |
|
|
var config = new FontConfig |
|
|
{ |
|
|
FontFamily = "JetBrains Mono", |
|
|
FontSize = 16, |
|
|
FontWeight = FontWeights.Medium, |
|
|
FontStyle = FontStyles.Normal |
|
|
}; |
|
|
|
|
|
if (FontManager.ApplyFontConfig(textEditor, config)) |
|
|
{ |
|
|
MessageBox.Show("字体配置应用成功!"); |
|
|
} |
|
|
else |
|
|
{ |
|
|
MessageBox.Show("字体配置应用失败,可能字体不存在。"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Example6_FontSelector() |
|
|
{ |
|
|
|
|
|
var systemFonts = FontManager.GetSystemFonts(); |
|
|
|
|
|
|
|
|
var fontDialog = new Microsoft.Win32.OpenFileDialog |
|
|
{ |
|
|
Title = "选择字体文件", |
|
|
Filter = "字体文件|*.ttf;*.otf;*.woff|所有文件|*.*" |
|
|
}; |
|
|
|
|
|
if (fontDialog.ShowDialog() == true) |
|
|
{ |
|
|
FontManager.LoadFontFromFile(textEditor, fontDialog.FileName, 14); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Example7_CheckFontAvailability() |
|
|
{ |
|
|
string[] fontsToCheck = { "Consolas", "JetBrains Mono", "Fira Code", "Comic Sans MS" }; |
|
|
|
|
|
foreach (string font in fontsToCheck) |
|
|
{ |
|
|
bool available = FontManager.IsFontAvailable(font); |
|
|
Console.WriteLine($"字体 '{font}' {(available ? "可用" : "不可用")}"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void Example8_GetMonospaceFonts() |
|
|
{ |
|
|
var monospaceFonts = FontManager.GetMonospaceFonts(); |
|
|
|
|
|
Console.WriteLine("系统中可用的等宽字体:"); |
|
|
foreach (string font in monospaceFonts) |
|
|
{ |
|
|
Console.WriteLine($"- {font}"); |
|
|
} |
|
|
|
|
|
|
|
|
if (monospaceFonts.Count > 0) |
|
|
{ |
|
|
FontManager.SetFont(textEditor, monospaceFonts[0], 14); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void IntegrateWithMainWindow(MainWindow mainWindow, TextEditorControl editor) |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
FontManager.SetProgrammingFont(editor, 14); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static class TextEditorControlFontExtensions |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
public static void SetEditorFont(this TextEditorControl control, string fontFamily, double fontSize = 14) |
|
|
{ |
|
|
FontManager.SetFont(control, fontFamily, fontSize); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void ApplyProgrammingFont(this TextEditorControl control, double fontSize = 14) |
|
|
{ |
|
|
FontManager.SetProgrammingFont(control, fontSize); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void LoadFontFromConfig(this TextEditorControl control, string configPath) |
|
|
{ |
|
|
try |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
var config = new FontConfig("Consolas", 14); |
|
|
FontManager.ApplyFontConfig(control, config); |
|
|
} |
|
|
catch (Exception ex) |
|
|
{ |
|
|
System.Diagnostics.Debug.WriteLine($"加载字体配置失败: {ex.Message}"); |
|
|
|
|
|
FontManager.SetProgrammingFont(control); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |