<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        .NET中用ICSharpCode.TextEditor自定義代碼折疊與高亮

        來源:懂視網 責編:小采 時間:2020-11-27 22:35:58
        文檔

        .NET中用ICSharpCode.TextEditor自定義代碼折疊與高亮

        .NET中用ICSharpCode.TextEditor自定義代碼折疊與高亮:前言 ICSharpCode.TextEditor 是一款非常不錯的.NET代碼編輯控件,內置了多種高亮語言支持,同時完美支持中文,非常贊! 先來看一下運行效果: 一、項目結構 這里需要注意lib文件夾下導入的類庫,這個Demo需要這些dll. 二、代碼折疊 需要實現IFol
        推薦度:
        導讀.NET中用ICSharpCode.TextEditor自定義代碼折疊與高亮:前言 ICSharpCode.TextEditor 是一款非常不錯的.NET代碼編輯控件,內置了多種高亮語言支持,同時完美支持中文,非常贊! 先來看一下運行效果: 一、項目結構 這里需要注意lib文件夾下導入的類庫,這個Demo需要這些dll. 二、代碼折疊 需要實現IFol

        前言

        ICSharpCode.TextEditor 是一款非常不錯的.NET代碼編輯控件,內置了多種高亮語言支持,同時完美支持中文,非常贊!

        先來看一下運行效果:

        一、項目結構

        這里需要注意lib文件夾下導入的類庫,這個Demo需要這些dll.

        二、代碼折疊

        需要實現IFoldingStrategy中的 GenerateFoldMarkers 方法,代碼如下:

        using ICSharpCode.TextEditor.Document;
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
        
        namespace JackWangCUMT.WinForm
        {
         
         /// <summary>
         /// The class to generate the foldings, it implements ICSharpCode.TextEditor.Document.IFoldingStrategy
         /// </summary>
         public class MingFolding : IFoldingStrategy
         {
         /// <summary>
         /// Generates the foldings for our document.
         /// </summary>
         /// <param name="document">The current document.</param>
         /// <param name="fileName">The filename of the document.</param>
         /// <param name="parseInformation">Extra parse information, not used in this sample.</param>
         /// <returns>A list of FoldMarkers.</returns>
         public List<FoldMarker> GenerateFoldMarkers(IDocument document, string fileName, object parseInformation)
         {
         List<FoldMarker> list = new List<FoldMarker>();
         //stack 先進先出
         var startLines = new Stack<int>();
         // Create foldmarkers for the whole document, enumerate through every line.
         for (int i = 0; i < document.TotalNumberOfLines; i++)
         {
         // Get the text of current line.
         string text = document.GetText(document.GetLineSegment(i));
        
         if (text.Trim().StartsWith("#region")) // Look for method starts
         {
         startLines.Push(i);
        
         }
         if (text.Trim().StartsWith("#endregion")) // Look for method endings
         {
         int start = startLines.Pop();
         // Add a new FoldMarker to the list.
         // document = the current document
         // start = the start line for the FoldMarker
         // document.GetLineSegment(start).Length = the ending of the current line = the start column of our foldmarker.
         // i = The current line = end line of the FoldMarker.
         // 7 = The end column
         list.Add(new FoldMarker(document, start, document.GetLineSegment(start).Length, i, 57, FoldType.Region, "..."));
         }
         //支持嵌套 {}
         if (text.Trim().StartsWith("{")) // Look for method starts
         {
         startLines.Push(i);
         }
         if (text.Trim().StartsWith("}")) // Look for method endings
         {
         if (startLines.Count > 0)
         {
         int start = startLines.Pop();
         list.Add(new FoldMarker(document, start, document.GetLineSegment(start).Length, i, 57, FoldType.TypeBody, "...}"));
         }
         }
        
        
         // /// <summary>
         if (text.Trim().StartsWith("http:/// <summary>")) // Look for method starts
         {
         startLines.Push(i);
         }
         if (text.Trim().StartsWith("http:/// <returns>")) // Look for method endings
         {
        
         int start = startLines.Pop();
         //獲取注釋文本(包括空格)
         string display = document.GetText(document.GetLineSegment(start + 1).Offset, document.GetLineSegment(start + 1).Length);
         //remove ///
         display = display.Trim().TrimStart('/');
         list.Add(new FoldMarker(document, start, document.GetLineSegment(start).Length, i, 57, FoldType.TypeBody, display));
         }
         }
        
         return list;
         }
         }
        }

        三、高亮配置

        拷貝CSharp-Mode.xshd為 JackCSharp-Mode.xshd ,將其中的名字修改為: SyntaxDefinition name = "JackC#" ,并添加高亮關鍵字,如下:

        這樣代碼中出現的JackWang就會高亮。下面的代碼片段將自定義高亮文件進行加載,并用SetHighlighting進行設置,這里一定注意目錄下必須有xshd的配置文件,否則高亮將失效。

        textEditor.Encoding = System.Text.Encoding.UTF8;
         textEditor.Font = new Font("Hack",12);
         textEditor.Document.FoldingManager.FoldingStrategy = new JackWangCUMT.WinForm.MingFolding();
         textEditor.Text = sampleCode;
        
         //自定義代碼高亮
         string path = Application.StartupPath+ "\\HighLighting";
         FileSyntaxModeProvider fsmp;
         if (Directory.Exists(path))
         {
         fsmp = new FileSyntaxModeProvider(path);
         HighlightingManager.Manager.AddSyntaxModeFileProvider(fsmp);
         textEditor.SetHighlighting("JackC#");
         
        
         }

        為了保持代碼適時進行折疊,這里監聽文本變化,如下所示:

         private void TextEditor_TextChanged(object sender, EventArgs e)
         {
         //更新,以便進行代碼折疊
         textEditor.Document.FoldingManager.UpdateFoldings(null, null);
         }

        最后說明的是,我們可以定義一個格式化代碼的類,來格式化C#代碼:

        總結

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        .NET中用ICSharpCode.TextEditor自定義代碼折疊與高亮

        .NET中用ICSharpCode.TextEditor自定義代碼折疊與高亮:前言 ICSharpCode.TextEditor 是一款非常不錯的.NET代碼編輯控件,內置了多種高亮語言支持,同時完美支持中文,非常贊! 先來看一下運行效果: 一、項目結構 這里需要注意lib文件夾下導入的類庫,這個Demo需要這些dll. 二、代碼折疊 需要實現IFol
        推薦度:
        標簽: net 高亮 net代碼
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 人人公开免费超级碰碰碰视频| 久久久久亚洲AV无码去区首| 精品国产污污免费网站| 精品亚洲一区二区三区在线观看| 激情吃奶吻胸免费视频xxxx| 亚洲av无码不卡私人影院| 一区二区三区免费在线视频| 中文字幕亚洲乱码熟女一区二区| 国产午夜无码片免费| 久久精品国产精品亚洲艾 | 人人爽人人爽人人片av免费| 亚洲 另类 无码 在线| jizz免费在线观看| 亚洲国产精品无码久久一区二区| 免费视频成人手机在线观看网址| 日韩亚洲AV无码一区二区不卡| 51精品视频免费国产专区| 欧洲乱码伦视频免费国产| 午夜亚洲国产成人不卡在线| 国产黄色免费观看| 亚洲精品无码久久久久久久| 成年人免费网站在线观看| 无套内射无矿码免费看黄| 亚洲国产精品无码专区影院| www.黄色免费网站| 思思久久99热免费精品6| 亚洲Av无码精品色午夜| 在线看片免费不卡人成视频| 男男gay做爽爽的视频免费| 亚洲香蕉成人AV网站在线观看| 韩国亚洲伊人久久综合影院| 亚洲午夜久久久久妓女影院 | 久久亚洲国产成人精品性色| 女人18毛片免费观看| 一区二区免费电影| 亚洲乱码卡一卡二卡三| 亚洲高清无码综合性爱视频| 猫咪免费人成网站在线观看| 狼色精品人妻在线视频免费| 久久精品国产亚洲77777| 国产精品成人免费综合|