分享
 
 
 

利用TextEditorControl控件现代码语法高亮显示

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

/**********************************************************************************************

* 创建时间: 2005-1-19 16:32:06

* 作者: Jack

*

* 介绍: 一个利用ICSharpCode.TextEditor.TextEditorControl控件

* 实现代码语法高亮显示的示例程序,需要引用ICSharpCode.TextEditor.dll文件

* 该文件可以在#develop的库中找到

**********************************************************************************************/

#region UsingBlock

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using ICSharpCode.TextEditor;

using ICSharpCode.TextEditor.Gui.CompletionWindow;

using ICSharpCode.TextEditor.Document;

using ICSharpCode.TextEditor.Actions;

#endregion

namespace HighLightingCodeFile

{

/// <summary>

/// ICSharpCode.TextEditor.

/// TextEditorControl的示例程序

/// </summary>

public class TestCSharpTextBox : System.Windows.Forms.Form

{

#region 私有属性

private ICSharpCode.TextEditor.TextEditorControl textEditorControl;

private System.Windows.Forms.MainMenu mainMenu;

private System.Windows.Forms.MenuItem menuItem1;

private System.Windows.Forms.MenuItem OpenMenu;

private System.Windows.Forms.MenuItem Save;

private System.Windows.Forms.SaveFileDialog saveFileDialog;

private System.Windows.Forms.OpenFileDialog openFileDialog;

private System.Windows.Forms.MenuItem menuItem2;

private System.Windows.Forms.MenuItem menuItem3;

private System.Windows.Forms.MenuItem menuItem4;

private System.Windows.Forms.MenuItem menuItem5;

private System.Windows.Forms.MenuItem menuItem6;

private System.Windows.Forms.MenuItem menuItem7;

private System.Windows.Forms.MenuItem menuItem8;

#endregion

#region 属性

#endregion

#region 构造函数

public TestCSharpTextBox()

{

//

// Windows 窗体设计器支持所必需的

//

InitializeComponent();

textEditorControl.Document.HighlightingStrategy =

HighlightingStrategyFactory.CreateHighlightingStrategy("C#");

textEditorControl.Encoding = System.Text.Encoding.Default;

}

#endregion

#region 私有方法

#region Windows 窗体设计器生成的代码

/// <summary>

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

/// 此方法的内容。

/// </summary>

private void InitializeComponent()

{

System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(TestCSharpTextBox));

this.textEditorControl = new ICSharpCode.TextEditor.TextEditorControl();

this.mainMenu = new System.Windows.Forms.MainMenu();

this.menuItem1 = new System.Windows.Forms.MenuItem();

this.OpenMenu = new System.Windows.Forms.MenuItem();

this.Save = new System.Windows.Forms.MenuItem();

this.menuItem2 = new System.Windows.Forms.MenuItem();

this.menuItem3 = new System.Windows.Forms.MenuItem();

this.menuItem4 = new System.Windows.Forms.MenuItem();

this.menuItem5 = new System.Windows.Forms.MenuItem();

this.menuItem6 = new System.Windows.Forms.MenuItem();

this.menuItem7 = new System.Windows.Forms.MenuItem();

this.menuItem8 = new System.Windows.Forms.MenuItem();

this.openFileDialog = new System.Windows.Forms.OpenFileDialog();

this.saveFileDialog = new System.Windows.Forms.SaveFileDialog();

this.SuspendLayout();

//

// textEditorControl

//

this.textEditorControl.AutoScroll = true;

this.textEditorControl.Dock = System.Windows.Forms.DockStyle.Fill;

this.textEditorControl.Encoding = ((System.Text.Encoding)(resources.GetObject("textEditorControl.Encoding")));

this.textEditorControl.Location = new System.Drawing.Point(0, 0);

this.textEditorControl.Name = "textEditorControl";

this.textEditorControl.ShowEOLMarkers = true;

this.textEditorControl.ShowInvalidLines = false;

this.textEditorControl.ShowSpaces = true;

this.textEditorControl.ShowTabs = true;

this.textEditorControl.ShowVRuler = true;

this.textEditorControl.Size = new System.Drawing.Size(472, 454);

this.textEditorControl.TabIndex = 0;

//

// mainMenu

//

this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {

this.menuItem1,

this.menuItem2});

//

// menuItem1

//

this.menuItem1.Index = 0;

this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {

this.OpenMenu,

this.Save});

this.menuItem1.Text = "File(&F)";

//

// OpenMenu

//

this.OpenMenu.Index = 0;

this.OpenMenu.Text = "&Open";

this.OpenMenu.Click += new System.EventHandler(this.OpenMenu_Click);

//

// Save

//

this.Save.Index = 1;

this.Save.Text = "&Save";

this.Save.Click += new System.EventHandler(this.Save_Click);

//

// menuItem2

//

this.menuItem2.Index = 1;

this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {

this.menuItem3,

this.menuItem4,

this.menuItem5,

this.menuItem6,

this.menuItem7,

this.menuItem8});

this.menuItem2.Text = "CodeType(&T)";

//

// menuItem3

//

this.menuItem3.Index = 0;

this.menuItem3.Text = "&C#";

this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);

//

// menuItem4

//

this.menuItem4.Index = 1;

this.menuItem4.Text = "C++.&NET";

this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);

//

// menuItem5

//

this.menuItem5.Index = 2;

this.menuItem5.Text = "&Xml";

this.menuItem5.Click += new System.EventHandler(this.menuItem5_Click);

//

// menuItem6

//

this.menuItem6.Index = 3;

this.menuItem6.Text = "&Html";

this.menuItem6.Click += new System.EventHandler(this.menuItem6_Click);

//

// menuItem7

//

this.menuItem7.Index = 4;

this.menuItem7.Text = "&Java";

this.menuItem7.Click += new System.EventHandler(this.menuItem7_Click);

//

// menuItem8

//

this.menuItem8.Index = 5;

this.menuItem8.Text = "&VB.NET";

this.menuItem8.Click += new System.EventHandler(this.menuItem8_Click);

//

// TestCSharpTextBox

//

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

this.ClientSize = new System.Drawing.Size(472, 454);

this.Controls.Add(this.textEditorControl);

this.Menu = this.mainMenu;

this.Name = "TestCSharpTextBox";

this.Text = "TestCSharpTextBox";

this.ResumeLayout(false);

}

#endregion

#region 事件处理

private void OpenMenu_Click(object sender, System.EventArgs e)

{

OpenFileDialog oDia = new OpenFileDialog();

DialogResult result = oDia.ShowDialog();

if (result == DialogResult.OK)

{

textEditorControl.LoadFile(oDia.FileName);

}

}

private void menuItem3_Click(object sender, System.EventArgs e)

{

textEditorControl.Document.HighlightingStrategy =

HighlightingStrategyFactory.CreateHighlightingStrategy("C#");

}

private void menuItem4_Click(object sender, System.EventArgs e)

{

textEditorControl.Document.HighlightingStrategy =

HighlightingStrategyFactory.CreateHighlightingStrategy("C++.NET");

}

private void menuItem5_Click(object sender, System.EventArgs e)

{

textEditorControl.Document.HighlightingStrategy =

HighlightingStrategyFactory.CreateHighlightingStrategy("XML");

}

private void menuItem6_Click(object sender, System.EventArgs e)

{

textEditorControl.Document.HighlightingStrategy =

HighlightingStrategyFactory.CreateHighlightingStrategy("HTML");

}

private void menuItem7_Click(object sender, System.EventArgs e)

{

textEditorControl.Document.HighlightingStrategy =

HighlightingStrategyFactory.CreateHighlightingStrategy("Java");

}

private void menuItem8_Click(object sender, System.EventArgs e)

{

textEditorControl.Document.HighlightingStrategy =

HighlightingStrategyFactory.CreateHighlightingStrategy("VBNET");

}

private void Save_Click(object sender, System.EventArgs e)

{

SaveFileDialog sDia = new SaveFileDialog();

DialogResult result = sDia.ShowDialog();

if (result == DialogResult.OK)

{

textEditorControl.SaveFile(sDia.FileName);

}

}

#endregion

#endregion

#region 入口点

[STAThread]

static void Main()

{

Application.Run(new TestCSharpTextBox());

}

#endregion

#region 公有方法

#endregion

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有