MemoEditExtension.cs 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using DevExpress.XtraEditors;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace ExtensionsDev
  10. {
  11. public static class MemoEditExtension
  12. {
  13. private readonly static string codeKeyWord = "public;private;protect;internal;protected;readonly;virtual;override;abstract;sealed;base;this;break;continue;foreach;static;class;interface;bool;string;double;float;decimal;byte;shout;int;long;ushort;uint;ulong;void;null;extern;const;ref;out;namespace;new;goto;as;case;catch;char;checked;default;delegate;do;else;enum;event;explicit;false;finally;fixed;for;if;implicit;in;is;lock;object;operator;params;return;sbyte;short;sizeof;stackalloc;struct;switch;throw;true;try;typeof;unchecked;unsafe;using;volatile;while;add;and;alias;ascending;args;async;await;by;descending;dynamic;equals;from;get;global;group;init;into;join;let;nameof;nint;not;notnull;nuint;on;or;orderby;partial;record;remove;select;set;value;var;when;where;where;with;yield";
  14. private readonly static string[] keyWords = codeKeyWord.Split(';');
  15. private static Color KeywordColor = Color.Blue;
  16. public static Color StringColor = Color.FromArgb(163, 21, 21);
  17. private static Color CommentColor = Color.Gray;
  18. /// <summary>
  19. /// 代码展示模式
  20. /// </summary>
  21. /// <param name="ctrl"></param>
  22. public static void UseCodeShowMode(this MemoEdit ctrl)
  23. {
  24. ctrl.Properties.Appearance.Options.UseFont = true;
  25. ctrl.Properties.UseAdvancedMode = DevExpress.Utils.DefaultBoolean.True;
  26. ctrl.Properties.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
  27. ctrl.CustomHighlightText += Ctrl_CustomHighlightText;
  28. ctrl.UpdateTextHighlight();
  29. }
  30. private static void Ctrl_CustomHighlightText(object sender, TextEditCustomHighlightTextEventArgs e)
  31. {
  32. HighlightKeyword(e);
  33. HighlightStrings(e);
  34. HighlightComment(e);
  35. }
  36. private static void HighlightKeyword(TextEditCustomHighlightTextEventArgs e)
  37. {
  38. foreach (var item in keyWords)
  39. {
  40. e.HighlightWords(item, KeywordColor);
  41. }
  42. }
  43. private static void HighlightComment(TextEditCustomHighlightTextEventArgs e)
  44. {
  45. string text = e.Text;
  46. int index = text.IndexOf("//");
  47. if (index != -1)
  48. e.HighlightRange(index, text.Length - index, CommentColor);
  49. else if (text.StartsWith("/*")||text.StartsWith("*"))
  50. {
  51. int endIdx = text.IndexOf("*/");
  52. if (endIdx == -1)
  53. endIdx = text.Length - index;
  54. e.HighlightRange(0, text.Length, CommentColor);
  55. }
  56. }
  57. private static void HighlightStrings(TextEditCustomHighlightTextEventArgs e)
  58. {
  59. string text = e.Text;
  60. int length = text.Length;
  61. int startTextIndex = -1;
  62. while (startTextIndex < length)
  63. {
  64. startTextIndex = text.IndexOf('\'', startTextIndex + 1);
  65. if (startTextIndex == -1) break;
  66. int endTextIndex = text.IndexOf('\'', startTextIndex + 1);
  67. if (endTextIndex == -1)
  68. endTextIndex = length;
  69. e.HighlightRange(startTextIndex, endTextIndex - startTextIndex + 1, StringColor);
  70. startTextIndex = endTextIndex;
  71. }
  72. startTextIndex = -1;
  73. while (startTextIndex < length)
  74. {
  75. startTextIndex = text.IndexOf('"', startTextIndex + 1);
  76. if (startTextIndex == -1) break;
  77. int endTextIndex = text.IndexOf('"', startTextIndex + 1);
  78. if (endTextIndex == -1)
  79. endTextIndex = length;
  80. e.HighlightRange(startTextIndex, endTextIndex - startTextIndex + 1, StringColor);
  81. startTextIndex = endTextIndex;
  82. }
  83. }
  84. }
  85. }