using System; using System.Windows; using System.Windows.Media; namespace WpfEditor { /// /// 字体使用示例类,展示如何在 TextEditorControl 中设置字体 /// public class FontUsageExample { private TextEditorControl textEditor; public FontUsageExample(TextEditorControl editor) { textEditor = editor; } /// /// 示例1:设置系统字体 /// public void Example1_SetSystemFont() { // 设置 Consolas 字体,14号大小 FontManager.SetFont(textEditor, "Consolas", 14); // 或者设置带样式的字体 FontManager.SetFont(textEditor, "Arial", 16, FontWeights.Bold, FontStyles.Italic); } /// /// 示例2:设置编程专用字体 /// public void Example2_SetProgrammingFont() { // 自动选择最佳的编程字体 FontManager.SetProgrammingFont(textEditor, 14); } /// /// 示例3:从文件加载自定义字体 /// public void Example3_LoadCustomFont() { string fontPath = @"C:\MyFonts\CustomFont.ttf"; if (FontManager.LoadFontFromFile(textEditor, fontPath, 14)) { MessageBox.Show("自定义字体加载成功!"); } else { MessageBox.Show("自定义字体加载失败,请检查文件路径。"); } } /// /// 示例4:从资源加载嵌入字体 /// public void Example4_LoadEmbeddedFont() { // 假设你在项目中有一个 Fonts 文件夹,里面有 MyFont.ttf // 并且该文件的 Build Action 设置为 Resource string resourcePath = "./Fonts/#MyFont"; if (FontManager.LoadFontFromResource(textEditor, resourcePath, 14)) { MessageBox.Show("嵌入字体加载成功!"); } else { MessageBox.Show("嵌入字体加载失败。"); } } /// /// 示例5:使用字体配置 /// 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("字体配置应用失败,可能字体不存在。"); } } /// /// 示例6:动态字体选择器 /// 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); } } /// /// 示例7:检查字体可用性 /// 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 ? "可用" : "不可用")}"); } } /// /// 示例8:获取等宽字体列表 /// 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); } } /// /// 示例9:在 MainWindow 中的实际应用 /// public static void IntegrateWithMainWindow(MainWindow mainWindow, TextEditorControl editor) { // 在 MainWindow 的构造函数或初始化方法中调用 // 设置默认编程字体 FontManager.SetProgrammingFont(editor, 14); // 可以添加菜单项来让用户选择字体 // 例如在菜单栏添加"字体设置"选项 } } /// /// 扩展 TextEditorControl 类的字体相关方法 /// 你可以将这些方法直接添加到 TextEditorControl 类中 /// 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 { // 这里可以实现从 JSON 或 XML 配置文件读取字体设置 // 示例:从 config.json 读取字体配置 var config = new FontConfig("Consolas", 14); // 默认配置 FontManager.ApplyFontConfig(control, config); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine($"加载字体配置失败: {ex.Message}"); // 使用默认字体 FontManager.SetProgrammingFont(control); } } } }