├── Resources ├── Add.png ├── Help.png ├── Move.png ├── Next.png ├── Open.png ├── Save.png ├── Stop.png ├── Type.png ├── CSharp.png ├── Clear.png ├── Close.png ├── Field.png ├── Header.png ├── Method.png ├── Pause.png ├── Remove.png ├── SaveAs.png ├── Select.png ├── Start.png ├── Compile.png ├── Content.png ├── Default.png ├── Namespace.png ├── Preview.png ├── Previous.png ├── Property.png ├── Reference.png └── SaveAll.png ├── packages.config ├── CommonLibrary.CodeSystem.csproj.user ├── Models └── DataModel.cs ├── .gitignore ├── CodeStatus.cs ├── Common └── CodeEditorHelper.cs ├── Controls ├── RenameForm.cs ├── RenameForm.Designer.cs ├── RenameForm.resx ├── CustomReferenceForm.cs ├── CustomReferenceForm.Designer.cs ├── CodeEditPage.cs └── CustomReferenceForm.resx ├── Program.cs ├── HotKeyManager.cs ├── Properties ├── AssemblyInfo.cs ├── Resources.resx └── Resources.Designer.cs ├── CommonLibrary.CodeSystem.sln ├── README.md ├── Presenters ├── Interfaces │ └── ICodeEditor.cs └── CodeEditorPresenter.cs ├── SearchManager.cs ├── Views ├── CodeEditor.resx └── CodeEditor.cs ├── CommonLibrary.CodeSystem.csproj └── CodeManager.cs /Resources/Add.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiaowomomo/CodeEditor/HEAD/Resources/Add.png -------------------------------------------------------------------------------- /Resources/Help.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiaowomomo/CodeEditor/HEAD/Resources/Help.png -------------------------------------------------------------------------------- /Resources/Move.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiaowomomo/CodeEditor/HEAD/Resources/Move.png -------------------------------------------------------------------------------- /Resources/Next.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiaowomomo/CodeEditor/HEAD/Resources/Next.png -------------------------------------------------------------------------------- /Resources/Open.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiaowomomo/CodeEditor/HEAD/Resources/Open.png -------------------------------------------------------------------------------- /Resources/Save.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiaowomomo/CodeEditor/HEAD/Resources/Save.png -------------------------------------------------------------------------------- /Resources/Stop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiaowomomo/CodeEditor/HEAD/Resources/Stop.png -------------------------------------------------------------------------------- /Resources/Type.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiaowomomo/CodeEditor/HEAD/Resources/Type.png -------------------------------------------------------------------------------- /Resources/CSharp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiaowomomo/CodeEditor/HEAD/Resources/CSharp.png -------------------------------------------------------------------------------- /Resources/Clear.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiaowomomo/CodeEditor/HEAD/Resources/Clear.png -------------------------------------------------------------------------------- /Resources/Close.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiaowomomo/CodeEditor/HEAD/Resources/Close.png -------------------------------------------------------------------------------- /Resources/Field.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiaowomomo/CodeEditor/HEAD/Resources/Field.png -------------------------------------------------------------------------------- /Resources/Header.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiaowomomo/CodeEditor/HEAD/Resources/Header.png -------------------------------------------------------------------------------- /Resources/Method.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiaowomomo/CodeEditor/HEAD/Resources/Method.png -------------------------------------------------------------------------------- /Resources/Pause.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiaowomomo/CodeEditor/HEAD/Resources/Pause.png -------------------------------------------------------------------------------- /Resources/Remove.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiaowomomo/CodeEditor/HEAD/Resources/Remove.png -------------------------------------------------------------------------------- /Resources/SaveAs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiaowomomo/CodeEditor/HEAD/Resources/SaveAs.png -------------------------------------------------------------------------------- /Resources/Select.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiaowomomo/CodeEditor/HEAD/Resources/Select.png -------------------------------------------------------------------------------- /Resources/Start.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiaowomomo/CodeEditor/HEAD/Resources/Start.png -------------------------------------------------------------------------------- /Resources/Compile.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiaowomomo/CodeEditor/HEAD/Resources/Compile.png -------------------------------------------------------------------------------- /Resources/Content.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiaowomomo/CodeEditor/HEAD/Resources/Content.png -------------------------------------------------------------------------------- /Resources/Default.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiaowomomo/CodeEditor/HEAD/Resources/Default.png -------------------------------------------------------------------------------- /Resources/Namespace.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiaowomomo/CodeEditor/HEAD/Resources/Namespace.png -------------------------------------------------------------------------------- /Resources/Preview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiaowomomo/CodeEditor/HEAD/Resources/Preview.png -------------------------------------------------------------------------------- /Resources/Previous.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiaowomomo/CodeEditor/HEAD/Resources/Previous.png -------------------------------------------------------------------------------- /Resources/Property.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiaowomomo/CodeEditor/HEAD/Resources/Property.png -------------------------------------------------------------------------------- /Resources/Reference.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiaowomomo/CodeEditor/HEAD/Resources/Reference.png -------------------------------------------------------------------------------- /Resources/SaveAll.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiaowomomo/CodeEditor/HEAD/Resources/SaveAll.png -------------------------------------------------------------------------------- /packages.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /CommonLibrary.CodeSystem.csproj.user: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | ProjectFiles 5 | 6 | -------------------------------------------------------------------------------- /Models/DataModel.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CommonLibrary.CodeSystem.Models 8 | { 9 | public class DataModel 10 | { 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # 此 .gitignore 文件已由 Microsoft(R) Visual Studio 自动创建。 3 | ################################################################################ 4 | 5 | /.vs 6 | /obj 7 | /bin/Debug 8 | /packages/jacobslusser.ScintillaNET.3.6.3 9 | -------------------------------------------------------------------------------- /CodeStatus.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace CommonLibrary.CodeSystem 8 | { 9 | public enum CodeStatus 10 | { 11 | Idle, 12 | Run, 13 | Pause, 14 | AbnormalStop 15 | } 16 | 17 | public enum SubType 18 | { 19 | Method = 0, 20 | Class 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Common/CodeEditorHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Drawing; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace CommonLibrary.CodeSystem.Common 9 | { 10 | public static class CodeEditorHelper 11 | { 12 | public static Color IntToColor(int rgb) 13 | { 14 | return Color.FromArgb(255, (byte)(rgb >> 16), (byte)(rgb >> 8), (byte)rgb); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Controls/RenameForm.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel; 4 | using System.Data; 5 | using System.Drawing; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | using System.Windows.Forms; 10 | 11 | namespace CommonLibrary.CodeSystem.Controls 12 | { 13 | public partial class RenameForm : Form 14 | { 15 | public SubType SubType => (SubType)comboBoxType.SelectedIndex; 16 | public string MethodName => textBoxMethod.Text; 17 | 18 | public RenameForm() 19 | { 20 | InitializeComponent(); 21 | 22 | comboBoxType.SelectedIndex = 0; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Program.cs: -------------------------------------------------------------------------------- 1 | using CommonLibrary.CodeSystem.Presenters; 2 | using CommonLibrary.CodeSystem.Views; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using System.Windows.Forms; 9 | 10 | namespace CommonLibrary.CodeSystem 11 | { 12 | static class Program 13 | { 14 | /// 15 | /// 应用程序的主入口点。 16 | /// 17 | [STAThread] 18 | static void Main() 19 | { 20 | Application.EnableVisualStyles(); 21 | Application.SetCompatibleTextRenderingDefault(false); 22 | CodeEditor codeEditer = new CodeEditor(); 23 | CodeEditorPresenter codeEditorPresenter = new CodeEditorPresenter(codeEditer); 24 | Application.Run(codeEditer); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /HotKeyManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Windows.Forms; 7 | 8 | namespace CommonLibrary.CodeSystem 9 | { 10 | internal class HotKeyManager 11 | { 12 | public static void AddHotKey(Form form, Action function, Keys key, bool ctrl = false, bool shift = false, bool alt = false) 13 | { 14 | form.KeyPreview = true; 15 | 16 | form.KeyDown += delegate (object sender, KeyEventArgs e) 17 | { 18 | if (IsHotkey(e, key, ctrl, shift, alt)) 19 | { 20 | function(); 21 | } 22 | }; 23 | } 24 | 25 | public static bool IsHotkey(KeyEventArgs eventData, Keys key, bool ctrl = false, bool shift = false, bool alt = false) 26 | { 27 | return eventData.KeyCode == key && eventData.Control == ctrl && eventData.Shift == shift && eventData.Alt == alt; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 有关程序集的一般信息由以下 6 | // 控制。更改这些特性值可修改 7 | // 与程序集关联的信息。 8 | [assembly: AssemblyTitle("CodeSystem")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("CodeSystem")] 13 | [assembly: AssemblyCopyright("Copyright © 2021")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // 将 ComVisible 设置为 false 会使此程序集中的类型 18 | //对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 19 | //请将此类型的 ComVisible 特性设置为 true。 20 | [assembly: ComVisible(false)] 21 | 22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 23 | [assembly: Guid("5d2d11b2-e547-456c-a13f-51633e790d65")] 24 | 25 | // 程序集的版本信息由下列四个值组成: 26 | // 27 | // 主版本 28 | // 次版本 29 | // 生成号 30 | // 修订号 31 | // 32 | //可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 33 | //通过使用 "*",如下所示: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /CommonLibrary.CodeSystem.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.31729.503 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommonLibrary.CodeSystem", "CommonLibrary.CodeSystem.csproj", "{5D2D11B2-E547-456C-A13F-51633E790D65}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {5D2D11B2-E547-456C-A13F-51633E790D65}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {5D2D11B2-E547-456C-A13F-51633E790D65}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {5D2D11B2-E547-456C-A13F-51633E790D65}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {5D2D11B2-E547-456C-A13F-51633E790D65}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {F7A09413-2BF2-420B-BF34-0D80DD513109} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CodeEditor 2 | C#代码编辑器,可用于动态编译生成C#执行程序,使软件功能灵活性更高。周末空闲重写了一个编辑器,因此开源最初的视觉软件删减的脚本编辑模块,欢迎交流学习。 3 | 4 | C# code editor that can be used to dynamically compile and generate C# executable programs for greater flexibility in software functionality. 5 | 6 | # 开发环境/Development Environments 7 | ● Visual Studio 2019 8 | 9 | ● .NET Framework 4.7.2 10 | 11 | ● ScintillaNET,第三方文本控件 12 | 13 | # 界面/Interfaces 14 | ![image](https://user-images.githubusercontent.com/17681289/167304202-a5f1c57e-16a1-4894-bbf0-1d7d03bc52b5.png) 15 | 16 | ● Main.main,程序流程编辑页面 17 | 18 | Program Flow Edit Page 19 | 20 | ![image](https://user-images.githubusercontent.com/17681289/169446817-c5406cc0-45e4-496d-9e95-80dd3c615d45.png) 21 | 22 | ● Using.head,using指令或程序集引用编辑页面 23 | 24 | Using.head, using directive or assembly reference edit page 25 | 26 | ![image](https://user-images.githubusercontent.com/17681289/169447002-8f51df0d-667a-48af-8603-a6373a1eafeb.png) 27 | 28 | ● SystemReference.ref,添加引用.Net自带dll编辑页面 29 | 30 | Add references to .Net's own dll edit page 31 | 32 | ![image](https://user-images.githubusercontent.com/17681289/169447137-5715112b-fb24-40b6-a4ae-ef28265b6cfb.png) 33 | 34 | ● CustomReference.ref,添加引用第三方dll编辑页面 35 | 36 | Add reference to third-party dll edit page 37 | 38 | ![image](https://user-images.githubusercontent.com/17681289/169447295-ad6374cb-3475-4af8-a68a-b0caf04e398b.png) 39 | 40 | ● Code.cs,预览动态生成的代码页面 41 | 42 | Preview dynamically generated code page 43 | 44 | ![image](https://user-images.githubusercontent.com/17681289/169447405-ee20f156-f0f5-417b-a6cb-f0f33d566236.png) 45 | -------------------------------------------------------------------------------- /Presenters/Interfaces/ICodeEditor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | using System.Windows.Forms; 7 | 8 | namespace CommonLibrary.CodeSystem.Presenters.Interfaces 9 | { 10 | public interface ICodeEditor 11 | { 12 | TabPage MainCodePage { get; } 13 | TabPage CurrentCodePage { get; } 14 | TabPage SelectClosePage { get; } 15 | TabControl TabControlCode { get; } 16 | TextBox MessageTextBox { get; } 17 | ListView ListViewMethod { get; } 18 | ToolStripStatusLabel CodePathLabel { get; } 19 | 20 | event Action ClearCode; 21 | event Action OpenCode; 22 | event Action SaveAsCode; 23 | event Action SaveAllCode; 24 | event Action CompileCode; 25 | event Action StopCode; 26 | event Action PauseCode; 27 | event Action StartCode; 28 | event Action PreviousSearch; 29 | event Action NextSearch; 30 | event Action AddSubMethod; 31 | event Action CloseAllPages; 32 | event Action SetDefaultHeader; 33 | event Action SetDefaultSystemReference; 34 | event Action MoveCustomReference; 35 | event Action ObtainCustomReference; 36 | event Action AddCodePage; 37 | event Action CloseSpecifiedPage; 38 | event Action CloseOtherPages; 39 | event Action RemoveSubMethod; 40 | event Action OpenSubMethod; 41 | event Action SaveCode; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /SearchManager.cs: -------------------------------------------------------------------------------- 1 | using CommonLibrary.CodeSystem.Controls; 2 | using ScintillaNET; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | using System.Windows.Forms; 9 | 10 | namespace CommonLibrary.CodeSystem 11 | { 12 | internal class SearchManager 13 | { 14 | private static int m_nLastSearchIndex = 0; 15 | private static bool m_bIsNext = true; 16 | 17 | public static CodeEditPage TextArea { get; set; } = null; 18 | public static TextBox SearchBox { get; set; } = null; 19 | 20 | public static string LastSearch { get; private set; } = string.Empty; 21 | 22 | public static void Find(bool bIsNext) 23 | { 24 | if (TextArea == null || SearchBox == null) 25 | return; 26 | 27 | if (!LastSearch.Equals(SearchBox.Text, StringComparison.OrdinalIgnoreCase)) 28 | { 29 | LastSearch = SearchBox.Text; 30 | m_nLastSearchIndex = 0; 31 | } 32 | if (LastSearch.Length > 0) 33 | { 34 | TextArea.SearchFlags = SearchFlags.None; 35 | int nTextLength = TextArea.TextLength; 36 | if (!bIsNext.Equals(m_bIsNext)) 37 | { 38 | m_bIsNext = bIsNext; 39 | m_nLastSearchIndex = nTextLength - m_nLastSearchIndex + LastSearch.Length; 40 | } 41 | 42 | if (bIsNext) 43 | { 44 | //设置搜索范围 45 | TextArea.TargetStart = m_nLastSearchIndex; 46 | TextArea.TargetEnd = nTextLength; 47 | 48 | if (TextArea.SearchInTarget(LastSearch) == -1) 49 | { 50 | m_nLastSearchIndex = 0; 51 | TextArea.TargetStart = m_nLastSearchIndex; 52 | TextArea.TargetEnd = nTextLength; 53 | 54 | if (TextArea.SearchInTarget(LastSearch) == -1) 55 | { 56 | TextArea.ClearSelections(); 57 | return; 58 | } 59 | } 60 | 61 | m_nLastSearchIndex = TextArea.TargetEnd; 62 | } 63 | else 64 | { 65 | //反转内容 66 | string strText = new string(TextArea.Text.Reverse().ToArray()); 67 | string strSearch = new string(LastSearch.Reverse().ToArray()); 68 | 69 | int nIndex = strText.IndexOf(strSearch, m_nLastSearchIndex, StringComparison.OrdinalIgnoreCase); 70 | if (nIndex == -1) 71 | { 72 | m_nLastSearchIndex = 0; 73 | 74 | nIndex = strText.IndexOf(strSearch, m_nLastSearchIndex, StringComparison.OrdinalIgnoreCase); 75 | if (nIndex == -1) 76 | { 77 | TextArea.ClearSelections(); 78 | return; 79 | } 80 | else 81 | { 82 | //反转索引 83 | TextArea.TargetStart = nTextLength - (nIndex + strSearch.Length); 84 | TextArea.TargetEnd = nTextLength - nIndex; 85 | } 86 | } 87 | else 88 | { 89 | TextArea.TargetStart = nTextLength - (nIndex + strSearch.Length); 90 | TextArea.TargetEnd = nTextLength - nIndex; 91 | } 92 | 93 | m_nLastSearchIndex = nIndex + strSearch.Length; 94 | } 95 | 96 | //设置选择内容 97 | TextArea.SetSelection(TextArea.TargetEnd, TextArea.TargetStart); 98 | TextArea.ScrollCaret(); 99 | } 100 | 101 | SearchBox.Focus(); 102 | } 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /Controls/RenameForm.Designer.cs: -------------------------------------------------------------------------------- 1 | 2 | namespace CommonLibrary.CodeSystem.Controls 3 | { 4 | partial class RenameForm 5 | { 6 | /// 7 | /// Required designer variable. 8 | /// 9 | private System.ComponentModel.IContainer components = null; 10 | 11 | /// 12 | /// Clean up any resources being used. 13 | /// 14 | /// true if managed resources should be disposed; otherwise, false. 15 | protected override void Dispose(bool disposing) 16 | { 17 | if (disposing && (components != null)) 18 | { 19 | components.Dispose(); 20 | } 21 | base.Dispose(disposing); 22 | } 23 | 24 | #region Windows Form Designer generated code 25 | 26 | /// 27 | /// Required method for Designer support - do not modify 28 | /// the contents of this method with the code editor. 29 | /// 30 | private void InitializeComponent() 31 | { 32 | this.label1 = new System.Windows.Forms.Label(); 33 | this.textBoxMethod = new System.Windows.Forms.TextBox(); 34 | this.buttonOK = new System.Windows.Forms.Button(); 35 | this.buttonCancel = new System.Windows.Forms.Button(); 36 | this.label2 = new System.Windows.Forms.Label(); 37 | this.comboBoxType = new System.Windows.Forms.ComboBox(); 38 | this.SuspendLayout(); 39 | // 40 | // label1 41 | // 42 | this.label1.AutoSize = true; 43 | this.label1.Location = new System.Drawing.Point(34, 70); 44 | this.label1.Name = "label1"; 45 | this.label1.Size = new System.Drawing.Size(35, 14); 46 | this.label1.TabIndex = 0; 47 | this.label1.Text = "名称:"; 48 | // 49 | // textBoxMethod 50 | // 51 | this.textBoxMethod.Location = new System.Drawing.Point(89, 67); 52 | this.textBoxMethod.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); 53 | this.textBoxMethod.Name = "textBoxMethod"; 54 | this.textBoxMethod.Size = new System.Drawing.Size(200, 22); 55 | this.textBoxMethod.TabIndex = 1; 56 | // 57 | // buttonOK 58 | // 59 | this.buttonOK.DialogResult = System.Windows.Forms.DialogResult.OK; 60 | this.buttonOK.Location = new System.Drawing.Point(61, 119); 61 | this.buttonOK.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); 62 | this.buttonOK.Name = "buttonOK"; 63 | this.buttonOK.Size = new System.Drawing.Size(75, 37); 64 | this.buttonOK.TabIndex = 2; 65 | this.buttonOK.Text = "确定"; 66 | this.buttonOK.UseVisualStyleBackColor = true; 67 | // 68 | // buttonCancel 69 | // 70 | this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel; 71 | this.buttonCancel.Location = new System.Drawing.Point(185, 119); 72 | this.buttonCancel.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); 73 | this.buttonCancel.Name = "buttonCancel"; 74 | this.buttonCancel.Size = new System.Drawing.Size(75, 37); 75 | this.buttonCancel.TabIndex = 3; 76 | this.buttonCancel.Text = "取消"; 77 | this.buttonCancel.UseVisualStyleBackColor = true; 78 | // 79 | // label2 80 | // 81 | this.label2.AutoSize = true; 82 | this.label2.Location = new System.Drawing.Point(34, 41); 83 | this.label2.Name = "label2"; 84 | this.label2.Size = new System.Drawing.Size(35, 14); 85 | this.label2.TabIndex = 4; 86 | this.label2.Text = "类型:"; 87 | // 88 | // comboBoxType 89 | // 90 | this.comboBoxType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; 91 | this.comboBoxType.FormattingEnabled = true; 92 | this.comboBoxType.Items.AddRange(new object[] { 93 | "Method", 94 | "Class"}); 95 | this.comboBoxType.Location = new System.Drawing.Point(89, 38); 96 | this.comboBoxType.Name = "comboBoxType"; 97 | this.comboBoxType.Size = new System.Drawing.Size(200, 22); 98 | this.comboBoxType.TabIndex = 5; 99 | // 100 | // RenameForm 101 | // 102 | this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 14F); 103 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 104 | this.ClientSize = new System.Drawing.Size(322, 207); 105 | this.Controls.Add(this.comboBoxType); 106 | this.Controls.Add(this.label2); 107 | this.Controls.Add(this.buttonCancel); 108 | this.Controls.Add(this.buttonOK); 109 | this.Controls.Add(this.textBoxMethod); 110 | this.Controls.Add(this.label1); 111 | this.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 112 | this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); 113 | this.Name = "RenameForm"; 114 | this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 115 | this.Text = "RenameForm"; 116 | this.ResumeLayout(false); 117 | this.PerformLayout(); 118 | 119 | } 120 | 121 | #endregion 122 | 123 | private System.Windows.Forms.Label label1; 124 | private System.Windows.Forms.TextBox textBoxMethod; 125 | private System.Windows.Forms.Button buttonOK; 126 | private System.Windows.Forms.Button buttonCancel; 127 | private System.Windows.Forms.Label label2; 128 | private System.Windows.Forms.ComboBox comboBoxType; 129 | } 130 | } -------------------------------------------------------------------------------- /Controls/RenameForm.resx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | -------------------------------------------------------------------------------- /Views/CodeEditor.resx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | 121 | 17, 17 122 | 123 | 124 | 161, 17 125 | 126 | 127 | 693, 17 128 | 129 | 130 | 421, 17 131 | 132 | 133 | 291, 17 134 | 135 | 136 | 943, 17 137 | 138 | -------------------------------------------------------------------------------- /CommonLibrary.CodeSystem.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {5D2D11B2-E547-456C-A13F-51633E790D65} 8 | WinExe 9 | Properties 10 | CommonLibrary.CodeSystem 11 | CommonLibrary.CodeSystem 12 | v4.7.2 13 | 512 14 | true 15 | 16 | 17 | true 18 | full 19 | false 20 | bin\Debug\ 21 | DEBUG;TRACE 22 | prompt 23 | 4 24 | 25 | 26 | pdbonly 27 | true 28 | bin\Release\ 29 | TRACE 30 | prompt 31 | 4 32 | 33 | 34 | 35 | 36 | 37 | 38 | packages\jacobslusser.ScintillaNET.3.6.3\lib\net40\ScintillaNET.dll 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | Component 55 | 56 | 57 | Form 58 | 59 | 60 | CustomReferenceForm.cs 61 | 62 | 63 | Form 64 | 65 | 66 | RenameForm.cs 67 | 68 | 69 | 70 | 71 | 72 | 73 | Form 74 | 75 | 76 | CodeEditor.cs 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | True 85 | True 86 | Resources.resx 87 | 88 | 89 | 90 | 91 | CustomReferenceForm.cs 92 | 93 | 94 | RenameForm.cs 95 | 96 | 97 | CodeEditor.cs 98 | 99 | 100 | ResXFileCodeGenerator 101 | Resources.Designer.cs 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | -------------------------------------------------------------------------------- /Controls/CustomReferenceForm.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel; 4 | using System.Data; 5 | using System.Drawing; 6 | using System.IO; 7 | using System.Linq; 8 | using System.Reflection; 9 | using System.Text; 10 | using System.Threading.Tasks; 11 | using System.Windows.Forms; 12 | 13 | namespace CommonLibrary.CodeSystem.Controls 14 | { 15 | public partial class CustomReferenceForm : Form 16 | { 17 | private const string NAMESPACE_NODE = "Namespace"; 18 | private const string FIELD_NODE = "Fields"; 19 | private const string PROPERTY_NODE = "Properties"; 20 | private const string METHOD_NODE = "Methods"; 21 | 22 | public CustomReferenceForm() 23 | { 24 | InitializeComponent(); 25 | } 26 | 27 | private void CustomReferenceForm_Load(object sender, EventArgs e) 28 | { 29 | InitListViewReference(); 30 | } 31 | 32 | private void InitListViewReference() 33 | { 34 | listViewCustomReference.Items.Clear(); 35 | foreach (string customReference in CodeManager.Instance.CustomReferences) 36 | { 37 | listViewCustomReference.Items.Add(customReference, customReference, null); 38 | } 39 | } 40 | 41 | private void listViewCustomReference_DoubleClick(object sender, EventArgs e) 42 | { 43 | ObtainDynamicLibrary(); 44 | } 45 | 46 | private void ObtainDynamicLibrary() 47 | { 48 | if (listViewCustomReference.FocusedItem != null) 49 | { 50 | string strReference = listViewCustomReference.FocusedItem.Text; 51 | ShowDynamicLibraryInformation(strReference); 52 | } 53 | } 54 | 55 | private void ShowDynamicLibraryInformation(string strReference) 56 | { 57 | string strDLLName = strReference.Replace(".dll", string.Empty); 58 | Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); 59 | Assembly[] select = assemblies.Where(x => x.FullName.Contains(strDLLName)).ToArray(); 60 | Assembly assembly = null; 61 | if (select.Length == 0) 62 | { 63 | try 64 | { 65 | string strDLLPath; 66 | strDLLPath = Path.Combine(Application.StartupPath, strReference); 67 | if (File.Exists(strDLLPath)) 68 | { 69 | assembly = Assembly.LoadFile(strDLLPath); 70 | } 71 | else 72 | { 73 | strDLLPath = Path.Combine(CodeManager.Instance.ThirdPartyDLLDirectory, strReference); 74 | if (File.Exists(strDLLPath)) 75 | { 76 | assembly = Assembly.LoadFile(strDLLPath); 77 | } 78 | } 79 | } 80 | catch 81 | { 82 | assembly = null; 83 | } 84 | } 85 | else 86 | { 87 | assembly = select[0]; 88 | } 89 | if (assembly != null) 90 | { 91 | treeViewReference.Nodes.Clear(); 92 | try 93 | { 94 | Type[] types = assembly.GetTypes(); 95 | treeViewReference.ImageIndex = 5; 96 | treeViewReference.SelectedImageIndex = 6; 97 | foreach (Type type in types) 98 | { 99 | treeViewReference.Nodes.Add(type.Name, type.Name); 100 | treeViewReference.Nodes[type.Name].ImageIndex = 0; 101 | treeViewReference.Nodes[type.Name].Nodes.Add(NAMESPACE_NODE, NAMESPACE_NODE); 102 | treeViewReference.Nodes[type.Name].Nodes[NAMESPACE_NODE].ImageIndex = 1; 103 | treeViewReference.Nodes[type.Name].Nodes.Add(FIELD_NODE, FIELD_NODE); 104 | treeViewReference.Nodes[type.Name].Nodes[FIELD_NODE].ImageIndex = 2; 105 | treeViewReference.Nodes[type.Name].Nodes.Add(PROPERTY_NODE, PROPERTY_NODE); 106 | treeViewReference.Nodes[type.Name].Nodes[PROPERTY_NODE].ImageIndex = 3; 107 | treeViewReference.Nodes[type.Name].Nodes.Add(METHOD_NODE, METHOD_NODE); 108 | treeViewReference.Nodes[type.Name].Nodes[METHOD_NODE].ImageIndex = 4; 109 | FieldInfo[] fieldInfos = type.GetFields(); 110 | PropertyInfo[] propertyInfos = type.GetProperties(); 111 | ConstructorInfo[] constructorInfos = type.GetConstructors(); 112 | MethodInfo[] methodInfos = type.GetMethods(); 113 | treeViewReference.Nodes[type.Name].Nodes[NAMESPACE_NODE].Nodes.Add(type.Namespace); 114 | foreach (FieldInfo fieldInfo in fieldInfos) 115 | { 116 | treeViewReference.Nodes[type.Name].Nodes[FIELD_NODE].Nodes.Add($"{(fieldInfo.IsStatic ? "static " : string.Empty)}{(fieldInfo.IsLiteral ? "const " : string.Empty)}{(fieldInfo.IsInitOnly ? "readonly " : string.Empty)}{fieldInfo.FieldType.FullName} {fieldInfo.Name}"); 117 | } 118 | foreach (PropertyInfo propertyInfo in propertyInfos) 119 | { 120 | MethodInfo methodInfo = propertyInfo.GetGetMethod(); 121 | treeViewReference.Nodes[type.Name].Nodes[PROPERTY_NODE].Nodes.Add($"{(methodInfo != null && methodInfo.IsStatic ? "static " : string.Empty)}{propertyInfo.PropertyType.FullName} {propertyInfo.Name} {(propertyInfo.CanRead ? "get;" : string.Empty)}{ (propertyInfo.CanWrite ? "set;" : string.Empty)}"); 122 | } 123 | foreach (ConstructorInfo constructorInfo in constructorInfos) 124 | { 125 | treeViewReference.Nodes[type.Name].Nodes[METHOD_NODE].Nodes.Add($"{constructorInfo}"); 126 | } 127 | foreach (MethodInfo methodInfo in methodInfos) 128 | { 129 | treeViewReference.Nodes[type.Name].Nodes[METHOD_NODE].Nodes.Add($"{(methodInfo.IsStatic ? "static " : string.Empty)}{methodInfo}"); 130 | } 131 | } 132 | } 133 | catch (Exception ex) 134 | { 135 | string strError = $"无法查阅相关内容,原因:\r\n{ex.Message}\r\n"; 136 | if (ex.InnerException != null) 137 | { 138 | strError += $"{ex.InnerException.Message}\r\n"; 139 | } 140 | if (ex is System.Reflection.ReflectionTypeLoadException typeLoadException) 141 | { 142 | foreach (Exception item in typeLoadException.LoaderExceptions) 143 | { 144 | strError += $"{item.Message}\r\n"; 145 | } 146 | } 147 | MessageBox.Show(strError, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning); 148 | } 149 | } 150 | } 151 | 152 | private void CustomReferenceForm_FormClosing(object sender, FormClosingEventArgs e) 153 | { 154 | e.Cancel = true; 155 | this.Hide(); 156 | } 157 | 158 | private void toolStripMenuItemObtain_Click(object sender, EventArgs e) 159 | { 160 | ObtainDynamicLibrary(); 161 | } 162 | 163 | private void toolStripMenuItemRefresh_Click(object sender, EventArgs e) 164 | { 165 | InitListViewReference(); 166 | } 167 | 168 | private void treeViewReference_KeyDown(object sender, KeyEventArgs e) 169 | { 170 | if (e.Control && e.KeyCode == Keys.C) 171 | { 172 | if (treeViewReference.SelectedNode != null) 173 | { 174 | Clipboard.SetDataObject(treeViewReference.SelectedNode.Text); 175 | } 176 | } 177 | } 178 | } 179 | } 180 | -------------------------------------------------------------------------------- /Controls/CustomReferenceForm.Designer.cs: -------------------------------------------------------------------------------- 1 | 2 | namespace CommonLibrary.CodeSystem.Controls 3 | { 4 | partial class CustomReferenceForm 5 | { 6 | /// 7 | /// Required designer variable. 8 | /// 9 | private System.ComponentModel.IContainer components = null; 10 | 11 | /// 12 | /// Clean up any resources being used. 13 | /// 14 | /// true if managed resources should be disposed; otherwise, false. 15 | protected override void Dispose(bool disposing) 16 | { 17 | if (disposing && (components != null)) 18 | { 19 | components.Dispose(); 20 | } 21 | base.Dispose(disposing); 22 | } 23 | 24 | #region Windows Form Designer generated code 25 | 26 | /// 27 | /// Required method for Designer support - do not modify 28 | /// the contents of this method with the code editor. 29 | /// 30 | private void InitializeComponent() 31 | { 32 | this.components = new System.ComponentModel.Container(); 33 | System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(CustomReferenceForm)); 34 | this.listViewCustomReference = new System.Windows.Forms.ListView(); 35 | this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components); 36 | this.toolStripMenuItemObtain = new System.Windows.Forms.ToolStripMenuItem(); 37 | this.toolStripMenuItemRefresh = new System.Windows.Forms.ToolStripMenuItem(); 38 | this.treeViewReference = new System.Windows.Forms.TreeView(); 39 | this.imageList1 = new System.Windows.Forms.ImageList(this.components); 40 | this.splitter1 = new System.Windows.Forms.Splitter(); 41 | this.contextMenuStrip1.SuspendLayout(); 42 | this.SuspendLayout(); 43 | // 44 | // listViewCustomReference 45 | // 46 | this.listViewCustomReference.ContextMenuStrip = this.contextMenuStrip1; 47 | this.listViewCustomReference.Dock = System.Windows.Forms.DockStyle.Left; 48 | this.listViewCustomReference.Font = new System.Drawing.Font("Tahoma", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 49 | this.listViewCustomReference.HideSelection = false; 50 | this.listViewCustomReference.Location = new System.Drawing.Point(0, 0); 51 | this.listViewCustomReference.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); 52 | this.listViewCustomReference.Name = "listViewCustomReference"; 53 | this.listViewCustomReference.Size = new System.Drawing.Size(229, 540); 54 | this.listViewCustomReference.TabIndex = 0; 55 | this.listViewCustomReference.UseCompatibleStateImageBehavior = false; 56 | this.listViewCustomReference.View = System.Windows.Forms.View.List; 57 | this.listViewCustomReference.DoubleClick += new System.EventHandler(this.listViewCustomReference_DoubleClick); 58 | // 59 | // contextMenuStrip1 60 | // 61 | this.contextMenuStrip1.ImageScalingSize = new System.Drawing.Size(20, 20); 62 | this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { 63 | this.toolStripMenuItemObtain, 64 | this.toolStripMenuItemRefresh}); 65 | this.contextMenuStrip1.Name = "contextMenuStrip1"; 66 | this.contextMenuStrip1.Size = new System.Drawing.Size(109, 52); 67 | // 68 | // toolStripMenuItemObtain 69 | // 70 | this.toolStripMenuItemObtain.Name = "toolStripMenuItemObtain"; 71 | this.toolStripMenuItemObtain.Size = new System.Drawing.Size(108, 24); 72 | this.toolStripMenuItemObtain.Text = "获取"; 73 | this.toolStripMenuItemObtain.Click += new System.EventHandler(this.toolStripMenuItemObtain_Click); 74 | // 75 | // toolStripMenuItemRefresh 76 | // 77 | this.toolStripMenuItemRefresh.Name = "toolStripMenuItemRefresh"; 78 | this.toolStripMenuItemRefresh.Size = new System.Drawing.Size(108, 24); 79 | this.toolStripMenuItemRefresh.Text = "刷新"; 80 | this.toolStripMenuItemRefresh.Click += new System.EventHandler(this.toolStripMenuItemRefresh_Click); 81 | // 82 | // treeViewReference 83 | // 84 | this.treeViewReference.Dock = System.Windows.Forms.DockStyle.Fill; 85 | this.treeViewReference.Font = new System.Drawing.Font("Tahoma", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 86 | this.treeViewReference.ImageIndex = 0; 87 | this.treeViewReference.ImageList = this.imageList1; 88 | this.treeViewReference.Location = new System.Drawing.Point(229, 0); 89 | this.treeViewReference.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); 90 | this.treeViewReference.Name = "treeViewReference"; 91 | this.treeViewReference.SelectedImageIndex = 0; 92 | this.treeViewReference.Size = new System.Drawing.Size(571, 540); 93 | this.treeViewReference.TabIndex = 1; 94 | this.treeViewReference.KeyDown += new System.Windows.Forms.KeyEventHandler(this.treeViewReference_KeyDown); 95 | // 96 | // imageList1 97 | // 98 | this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream"))); 99 | this.imageList1.TransparentColor = System.Drawing.Color.Transparent; 100 | this.imageList1.Images.SetKeyName(0, "Type.png"); 101 | this.imageList1.Images.SetKeyName(1, "Namespace.png"); 102 | this.imageList1.Images.SetKeyName(2, "Field.png"); 103 | this.imageList1.Images.SetKeyName(3, "Property.png"); 104 | this.imageList1.Images.SetKeyName(4, "Method.png"); 105 | this.imageList1.Images.SetKeyName(5, "Content.png"); 106 | this.imageList1.Images.SetKeyName(6, "Select.png"); 107 | // 108 | // splitter1 109 | // 110 | this.splitter1.Location = new System.Drawing.Point(229, 0); 111 | this.splitter1.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); 112 | this.splitter1.Name = "splitter1"; 113 | this.splitter1.Size = new System.Drawing.Size(3, 540); 114 | this.splitter1.TabIndex = 2; 115 | this.splitter1.TabStop = false; 116 | // 117 | // CustomReferenceForm 118 | // 119 | this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 18F); 120 | this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 121 | this.ClientSize = new System.Drawing.Size(800, 540); 122 | this.Controls.Add(this.splitter1); 123 | this.Controls.Add(this.treeViewReference); 124 | this.Controls.Add(this.listViewCustomReference); 125 | this.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 126 | this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4); 127 | this.Name = "CustomReferenceForm"; 128 | this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 129 | this.Text = "CustomReferenceForm"; 130 | this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.CustomReferenceForm_FormClosing); 131 | this.Load += new System.EventHandler(this.CustomReferenceForm_Load); 132 | this.contextMenuStrip1.ResumeLayout(false); 133 | this.ResumeLayout(false); 134 | 135 | } 136 | 137 | #endregion 138 | 139 | private System.Windows.Forms.ListView listViewCustomReference; 140 | private System.Windows.Forms.TreeView treeViewReference; 141 | private System.Windows.Forms.Splitter splitter1; 142 | private System.Windows.Forms.ContextMenuStrip contextMenuStrip1; 143 | private System.Windows.Forms.ToolStripMenuItem toolStripMenuItemObtain; 144 | private System.Windows.Forms.ToolStripMenuItem toolStripMenuItemRefresh; 145 | private System.Windows.Forms.ImageList imageList1; 146 | } 147 | } -------------------------------------------------------------------------------- /Controls/CodeEditPage.cs: -------------------------------------------------------------------------------- 1 | using CommonLibrary.CodeSystem.Common; 2 | using ScintillaNET; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Drawing; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | using System.Windows.Forms; 10 | 11 | namespace CommonLibrary.CodeSystem.Controls 12 | { 13 | public class CodeEditPage : Scintilla 14 | { 15 | private const int BACK_COLOR = 0x2A211C; 16 | private const int FORE_COLOR = 0xB7B7B7; 17 | private const int FOLDING_MARGIN = 3; 18 | private const bool CODEFOLDING_CIRCULAR = false; 19 | private const int BOOKMARK_MARGIN = 2; 20 | private const int BOOKMARK_MARKER = 2; 21 | 22 | private const string KEYWORD0 = "class extends implements import interface new case " + 23 | "do while else if for in switch throw get set function " + 24 | "var try catch finally while with default break continue " + 25 | "delete return each const namespace package include use is as " + 26 | "instanceof typeof author copy default deprecated eventType example " + 27 | "exampleText exception haxe inheritDoc internal link mtasc mxmlc param " + 28 | "private return see serial serialData serialField since throws usage version " + 29 | "langversion playerversion productversion dynamic private public partial static " + 30 | "intrinsic internal native override protected AS3 final super this arguments null " + 31 | "Infinity NaN undefined true false abstract as base bool break by byte case catch char " + 32 | "checked class const continue decimal default delegate do double descending explicit event " + 33 | "extern else enum false finally fixed float for foreach from goto group if implicit in int " + 34 | "interface internal into is lock long new null namespace object operator out override orderby " + 35 | "params private protected public readonly ref return switch struct sbyte sealed short sizeof " + 36 | "stackalloc static string select this throw true try typeof uint ulong unchecked unsafe ushort " + 37 | "using var virtual volatile void while where yield"; 38 | 39 | private const string KEYWORD1 = "void Null ArgumentError arguments Array Boolean Class Date " + 40 | "DefinitionError Error EvalError Function int Math Namespace Number " + 41 | "Object RangeError ReferenceError RegExp SecurityError String SyntaxError " + 42 | "TypeError uint XML XMLList Boolean Byte Char DateTime Decimal Double Int16 " + 43 | "Int32 Int64 IntPtr SByte Single UInt16 UInt32 UInt64 UIntPtr Void Path File " + 44 | "System Windows Forms ScintillaNET"; 45 | 46 | public bool CanEdit { get; set; } = false; 47 | public event Action CodeChanged = null; 48 | 49 | public CodeEditPage() 50 | { 51 | InitPage(); 52 | } 53 | 54 | private void InitPage() 55 | { 56 | this.Dock = DockStyle.Fill; 57 | //初始视图配置 58 | this.WrapMode = WrapMode.None; 59 | this.IndentationGuides = IndentView.LookBoth; 60 | 61 | //配置样式 62 | InitColors(); 63 | InitSyntaxColoring(); 64 | 65 | //数字边距 66 | InitNumberMargin(); 67 | 68 | //标记边距 69 | InitBookmarkMargin(); 70 | 71 | //代码折叠边距 72 | InitCodeFolding(); 73 | 74 | //初始化热键 75 | InitHotkeys(); 76 | 77 | this.TextChanged += CodeEditPage_TextChanged; 78 | } 79 | 80 | private void InitColors() 81 | { 82 | this.SetSelectionBackColor(true, CodeEditorHelper.IntToColor(0x114D9C));//配置选中内容颜色 83 | this.CaretForeColor = Color.White;//设置光标颜色 84 | } 85 | 86 | private void InitSyntaxColoring() 87 | { 88 | //配置默认样式 89 | this.StyleResetDefault(); 90 | this.Styles[Style.Default].Font = "Consolas";//字体格式 91 | this.Styles[Style.Default].Size = 15;//字体大小 92 | this.Styles[Style.Default].BackColor = Color.Black;//背景颜色 93 | this.Styles[Style.Default].ForeColor = CodeEditorHelper.IntToColor(0xFFFFFF); 94 | this.StyleClearAll(); 95 | 96 | //配置CPP(C#)词法分析器样式 97 | this.Styles[Style.Cpp.Identifier].ForeColor = CodeEditorHelper.IntToColor(0xD0DAE2); 98 | this.Styles[Style.Cpp.Comment].ForeColor = CodeEditorHelper.IntToColor(0xBD758B); 99 | this.Styles[Style.Cpp.CommentLine].ForeColor = CodeEditorHelper.IntToColor(0x40BF57); 100 | this.Styles[Style.Cpp.CommentDoc].ForeColor = CodeEditorHelper.IntToColor(0x2FAE35); 101 | this.Styles[Style.Cpp.Number].ForeColor = CodeEditorHelper.IntToColor(0xFFFF00); 102 | this.Styles[Style.Cpp.String].ForeColor = CodeEditorHelper.IntToColor(0xFFFF00); 103 | this.Styles[Style.Cpp.Character].ForeColor = CodeEditorHelper.IntToColor(0xE95454); 104 | this.Styles[Style.Cpp.Preprocessor].ForeColor = CodeEditorHelper.IntToColor(0x8AAFEE); 105 | this.Styles[Style.Cpp.Operator].ForeColor = CodeEditorHelper.IntToColor(0xE0E0E0); 106 | this.Styles[Style.Cpp.Regex].ForeColor = CodeEditorHelper.IntToColor(0xff00ff); 107 | this.Styles[Style.Cpp.CommentLineDoc].ForeColor = CodeEditorHelper.IntToColor(0x77A7DB); 108 | this.Styles[Style.Cpp.Word].ForeColor = CodeEditorHelper.IntToColor(0x48A8EE); 109 | this.Styles[Style.Cpp.Word2].ForeColor = CodeEditorHelper.IntToColor(0xF98906); 110 | this.Styles[Style.Cpp.CommentDocKeyword].ForeColor = CodeEditorHelper.IntToColor(0xB3D991); 111 | this.Styles[Style.Cpp.CommentDocKeywordError].ForeColor = CodeEditorHelper.IntToColor(0xFF0000); 112 | this.Styles[Style.Cpp.GlobalClass].ForeColor = CodeEditorHelper.IntToColor(0x48A8EE); 113 | 114 | this.Lexer = Lexer.Cpp;//设置词法分析器类型 115 | 116 | //设置关键字 117 | this.SetKeywords(0, KEYWORD0); 118 | this.SetKeywords(1, KEYWORD1); 119 | } 120 | 121 | private void InitNumberMargin() 122 | { 123 | this.Styles[Style.LineNumber].ForeColor = CodeEditorHelper.IntToColor(FORE_COLOR); 124 | this.Styles[Style.LineNumber].BackColor = CodeEditorHelper.IntToColor(BACK_COLOR); 125 | this.Styles[Style.IndentGuide].ForeColor = CodeEditorHelper.IntToColor(FORE_COLOR); 126 | this.Styles[Style.IndentGuide].BackColor = CodeEditorHelper.IntToColor(BACK_COLOR); 127 | 128 | var nums = this.Margins[1]; 129 | nums.Width = 30; 130 | nums.Type = MarginType.Number; 131 | nums.Sensitive = false; 132 | nums.Mask = 0; 133 | } 134 | 135 | private void InitBookmarkMargin() 136 | { 137 | //this.SetFoldMarginColor(true, CodeEditorHelper.IntToColor(BACK_COLOR)); 138 | 139 | var margin = this.Margins[BOOKMARK_MARGIN]; 140 | margin.Width = 20; 141 | margin.Sensitive = true; 142 | margin.Type = MarginType.Symbol; 143 | margin.Mask = (1 << BOOKMARK_MARKER); 144 | //margin.Cursor = MarginCursor.Arrow; 145 | 146 | var marker = this.Markers[BOOKMARK_MARKER]; 147 | marker.Symbol = MarkerSymbol.Circle; 148 | marker.SetBackColor(CodeEditorHelper.IntToColor(0xFF003B)); 149 | marker.SetForeColor(CodeEditorHelper.IntToColor(0x000000)); 150 | marker.SetAlpha(100); 151 | } 152 | 153 | private void InitCodeFolding() 154 | { 155 | this.SetFoldMarginColor(true, CodeEditorHelper.IntToColor(BACK_COLOR)); 156 | this.SetFoldMarginHighlightColor(true, CodeEditorHelper.IntToColor(BACK_COLOR)); 157 | 158 | //使能代码折叠 159 | this.SetProperty("fold", "1"); 160 | this.SetProperty("fold.compact", "1"); 161 | 162 | //配置边距显示折叠符号 163 | this.Margins[FOLDING_MARGIN].Type = MarginType.Symbol; 164 | this.Margins[FOLDING_MARGIN].Mask = Marker.MaskFolders; 165 | this.Margins[FOLDING_MARGIN].Sensitive = true; 166 | this.Margins[FOLDING_MARGIN].Width = 20; 167 | 168 | //设置所有折叠标记的颜色 169 | for (int i = 25; i <= 31; i++) 170 | { 171 | this.Markers[i].SetForeColor(CodeEditorHelper.IntToColor(BACK_COLOR)); // styles for [+] and [-] 172 | this.Markers[i].SetBackColor(CodeEditorHelper.IntToColor(FORE_COLOR)); // styles for [+] and [-] 173 | } 174 | 175 | //配置带有各自符号的折叠标记 176 | this.Markers[Marker.Folder].Symbol = CODEFOLDING_CIRCULAR ? MarkerSymbol.CirclePlus : MarkerSymbol.BoxPlus; 177 | this.Markers[Marker.FolderOpen].Symbol = CODEFOLDING_CIRCULAR ? MarkerSymbol.CircleMinus : MarkerSymbol.BoxMinus; 178 | this.Markers[Marker.FolderEnd].Symbol = CODEFOLDING_CIRCULAR ? MarkerSymbol.CirclePlusConnected : MarkerSymbol.BoxPlusConnected; 179 | this.Markers[Marker.FolderMidTail].Symbol = MarkerSymbol.TCorner; 180 | this.Markers[Marker.FolderOpenMid].Symbol = CODEFOLDING_CIRCULAR ? MarkerSymbol.CircleMinusConnected : MarkerSymbol.BoxMinusConnected; 181 | this.Markers[Marker.FolderSub].Symbol = MarkerSymbol.VLine; 182 | this.Markers[Marker.FolderTail].Symbol = MarkerSymbol.LCorner; 183 | 184 | //使能自动折叠 185 | this.AutomaticFold = (AutomaticFold.Show | AutomaticFold.Click | AutomaticFold.Change); 186 | } 187 | 188 | private void InitHotkeys() 189 | { 190 | // remove conflicting hotkeys from scintilla 191 | this.ClearCmdKey(Keys.Control | Keys.F); 192 | this.ClearCmdKey(Keys.Control | Keys.R); 193 | this.ClearCmdKey(Keys.Control | Keys.H); 194 | this.ClearCmdKey(Keys.Control | Keys.L); 195 | this.ClearCmdKey(Keys.Control | Keys.U); 196 | } 197 | 198 | private void CodeEditPage_TextChanged(object sender, EventArgs e) 199 | { 200 | if (CanEdit) 201 | { 202 | CodeChanged?.Invoke(); 203 | } 204 | } 205 | } 206 | } 207 | -------------------------------------------------------------------------------- /Controls/CustomReferenceForm.resx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | 121 | 17, 17 122 | 123 | 124 | 215, 17 125 | 126 | 127 | 128 | AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w 129 | LCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAACZTeXN0 130 | ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkDAAAADwMAAABG 131 | DAAAAk1TRnQBSQFMAgEBBwEAARABAAEQAQABEAEAARABAAT/AQkBAAj/AUIBTQE2AQQGAAE2AQQCAAEo 132 | AwABQAMAASADAAEBAQABCAYAAQgYAAGAAgABgAMAAoABAAGAAwABgAEAAYABAAKAAgADwAEAAcAB3AHA 133 | AQAB8AHKAaYBAAEzBQABMwEAATMBAAEzAQACMwIAAxYBAAMcAQADIgEAAykBAANVAQADTQEAA0IBAAM5 134 | AQABgAF8Af8BAAJQAf8BAAGTAQAB1gEAAf8B7AHMAQABxgHWAe8BAAHWAucBAAGQAakBrQIAAf8BMwMA 135 | AWYDAAGZAwABzAIAATMDAAIzAgABMwFmAgABMwGZAgABMwHMAgABMwH/AgABZgMAAWYBMwIAAmYCAAFm 136 | AZkCAAFmAcwCAAFmAf8CAAGZAwABmQEzAgABmQFmAgACmQIAAZkBzAIAAZkB/wIAAcwDAAHMATMCAAHM 137 | AWYCAAHMAZkCAALMAgABzAH/AgAB/wFmAgAB/wGZAgAB/wHMAQABMwH/AgAB/wEAATMBAAEzAQABZgEA 138 | ATMBAAGZAQABMwEAAcwBAAEzAQAB/wEAAf8BMwIAAzMBAAIzAWYBAAIzAZkBAAIzAcwBAAIzAf8BAAEz 139 | AWYCAAEzAWYBMwEAATMCZgEAATMBZgGZAQABMwFmAcwBAAEzAWYB/wEAATMBmQIAATMBmQEzAQABMwGZ 140 | AWYBAAEzApkBAAEzAZkBzAEAATMBmQH/AQABMwHMAgABMwHMATMBAAEzAcwBZgEAATMBzAGZAQABMwLM 141 | AQABMwHMAf8BAAEzAf8BMwEAATMB/wFmAQABMwH/AZkBAAEzAf8BzAEAATMC/wEAAWYDAAFmAQABMwEA 142 | AWYBAAFmAQABZgEAAZkBAAFmAQABzAEAAWYBAAH/AQABZgEzAgABZgIzAQABZgEzAWYBAAFmATMBmQEA 143 | AWYBMwHMAQABZgEzAf8BAAJmAgACZgEzAQADZgEAAmYBmQEAAmYBzAEAAWYBmQIAAWYBmQEzAQABZgGZ 144 | AWYBAAFmApkBAAFmAZkBzAEAAWYBmQH/AQABZgHMAgABZgHMATMBAAFmAcwBmQEAAWYCzAEAAWYBzAH/ 145 | AQABZgH/AgABZgH/ATMBAAFmAf8BmQEAAWYB/wHMAQABzAEAAf8BAAH/AQABzAEAApkCAAGZATMBmQEA 146 | AZkBAAGZAQABmQEAAcwBAAGZAwABmQIzAQABmQEAAWYBAAGZATMBzAEAAZkBAAH/AQABmQFmAgABmQFm 147 | ATMBAAGZATMBZgEAAZkBZgGZAQABmQFmAcwBAAGZATMB/wEAApkBMwEAApkBZgEAA5kBAAKZAcwBAAKZ 148 | Af8BAAGZAcwCAAGZAcwBMwEAAWYBzAFmAQABmQHMAZkBAAGZAswBAAGZAcwB/wEAAZkB/wIAAZkB/wEz 149 | AQABmQHMAWYBAAGZAf8BmQEAAZkB/wHMAQABmQL/AQABzAMAAZkBAAEzAQABzAEAAWYBAAHMAQABmQEA 150 | AcwBAAHMAQABmQEzAgABzAIzAQABzAEzAWYBAAHMATMBmQEAAcwBMwHMAQABzAEzAf8BAAHMAWYCAAHM 151 | AWYBMwEAAZkCZgEAAcwBZgGZAQABzAFmAcwBAAGZAWYB/wEAAcwBmQIAAcwBmQEzAQABzAGZAWYBAAHM 152 | ApkBAAHMAZkBzAEAAcwBmQH/AQACzAIAAswBMwEAAswBZgEAAswBmQEAA8wBAALMAf8BAAHMAf8CAAHM 153 | Af8BMwEAAZkB/wFmAQABzAH/AZkBAAHMAf8BzAEAAcwC/wEAAcwBAAEzAQAB/wEAAWYBAAH/AQABmQEA 154 | AcwBMwIAAf8CMwEAAf8BMwFmAQAB/wEzAZkBAAH/ATMBzAEAAf8BMwH/AQAB/wFmAgAB/wFmATMBAAHM 155 | AmYBAAH/AWYBmQEAAf8BZgHMAQABzAFmAf8BAAH/AZkCAAH/AZkBMwEAAf8BmQFmAQAB/wKZAQAB/wGZ 156 | AcwBAAH/AZkB/wEAAf8BzAIAAf8BzAEzAQAB/wHMAWYBAAH/AcwBmQEAAf8CzAEAAf8BzAH/AQAC/wEz 157 | AQABzAH/AWYBAAL/AZkBAAL/AcwBAAJmAf8BAAFmAf8BZgEAAWYC/wEAAf8CZgEAAf8BZgH/AQAC/wFm 158 | AQABIQEAAaUBAANfAQADdwEAA4YBAAOWAQADywEAA7IBAAPXAQAD3QEAA+MBAAPqAQAD8QEAA/gBAAHw 159 | AfsB/wEAAaQCoAEAA4ADAAH/AgAB/wMAAv8BAAH/AwAB/wEAAf8BAAL/AgAD/0gAAf8BswEZAQkJAAkJ 160 | KAAB/wGzArIBGQKyAfMGAAGzCQABCSYAAbsEsgEZBLIB/wQAAboJAAG6CAAC9RwABbIBGQSyARkEAAG6 161 | CQABuwcAAfUCWgH1GwAFsgEZBLIBGQQAAboJAAEJBgAB9QRaAf8aAAWyARkEsgEZBAABugkAAQkFAAH1 162 | BloB/xkABbIBGQSyARkEAAG6CQABCQQAAfUDWgIAA1oB/xgAA7IBGQH0AbsB/wGzArIBGQQAAboJAAEJ 163 | BAAB9QJaBAADWgH/FwABsgG7Af8BswOyAQkB/wGyARkEAAG6BAABCQSyARkFAAH1BgADWgH/FgAB/wGz 164 | B7IB8wH/BAABugQAAboDsgEJDgADWgH/FgABsweyAfQFAAG6BAABugKyAQkQAAFaARsYAAH/AbMDsgEZ 165 | BwABugQAAboBsgEJLQAB9AEJCQAGsgEJrQABugGzAfMGAAayBAAB9AWyJQAB8gGyAbMBugGyAbMB/wQA 166 | AbIBCQ0AAbIBAA6yAgAB/wOyAf8NAAH/AbMBsgH/AwABuwGyARkDAAGyAQkNAAGyAQAOsgIAAboBuwEA 167 | AfIBsgwAARkBsgG7AQABGQKyAbMC/wGyAbMB/wEAAbIBCQEAAbMJsgIAAbIRAAGzAQkBAAH0AbIB9AoA 168 | ArIC/wGzAbIB9AH/AbsBsgEJAQABCQGyARkBsgEJDQABshEAAf8FsgH0CQABCQEAAQkBsgEJAQAB8wEZ 169 | Av8CsgL/AboBsgEJDQABsgQAArINAAEZBLIB9AgAAf8BsgGzAv8EsgEJAQABCQGyAbsBAAGyAQkDAAKy 170 | ARkHAAGyBAACsg4AAfQEsgL0AfMFAAGyAQkBAAEJB7IC/wGzAbQBsgEJAwADsgEZBgABsgQAArIDAAay 171 | BgAB9AeyAboDAAL/CrIBuwEAAfQBsgEJAwAB/wOyARkFAAGyBAACsgMABrIHAAH0B7IBugIAAboNsgH/ 172 | AbIBCQQAAf8DsgEZBAABsgQAArIRAAGzB7ICAA6yAbMBsgEJBQAB/wKyAv8DAAGyBAACshEAA7IBuwIA 173 | AboBsgIAAfQMsgGzAQABsgEJBgAD/wGzAwABsgQAArIRAAOyAbsDAAG6BAABugmyAfQCAAGyAQkIAAGz 174 | BAABsgQAArIRAAEZArIBtAkAAf8GsgG6BAABsgEJDQABsgEACLICAASyCQABswKyAboKAAG7A7IB/wUA 175 | AbIBugQJBAAB/wQJAbIBAAiyAgAEsgoAAfQCswHzEwAGsgQAAfQFsiAAAUIBTQE+BwABPgMAASgDAAFA 176 | AwABIAMAAQEBAAEBBgABARYAA/8BAAb/AgAB/gEfAfABBwL/AgAB+AEHAe8B+wL/AgAB8AEBAe8B+wH8 177 | Af8CAAHwAQEB7wH7AfgBfwIAAfABAQHvAfsB8AE/AgAB8AEBAe8B+wHgAR8CAAHwAQEB7wH7AcMBDwIA 178 | AfABAQHvAfsBxwGHAgAB8AEBAe8BAwHvAcMCAAHwAQEB7wEHAf8B4QIAAfgBAwHvAQ8B/wHzAgAB/AEP 179 | Ae8BHwL/AgAB/wE/AeABPwL/AgAG/wIABv8CAAH+AT8BAwHABP8B+AEPAT8B/gGAAQEBgwH/AeEBxwE/ 180 | Af4BgAEBAZMB/wHEAQEBIAEGAv8BkQH/AYABCAE/Af4C/wGAAf8BogEAAT8B/gHzAf8B4AF/AYABEQE4 181 | Af4B8wH/AfABDwGQAQABOAF+AfMBgQH4AQMBgAECATgBPgHzAYEB/AEBAYABAAE8AR4B8wH/Af4BAQGA 182 | AQABPgEOAfMB/wH+ARkBgAEBAT8BDgHzAf8B/gEdAeABAwE/Ad4B8wH/Af4BHwHwAQ8BPwH+AYABYQH/ 183 | AQ8B/AEfAQMBwAGAAWEB/wGHAv8BAwHABP8L 184 | 185 | 186 | -------------------------------------------------------------------------------- /Properties/Resources.resx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | text/microsoft-resx 110 | 111 | 112 | 2.0 113 | 114 | 115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 116 | 117 | 118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 119 | 120 | 121 | 122 | ..\Resources\Add.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 123 | 124 | 125 | ..\Resources\Clear.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 126 | 127 | 128 | ..\Resources\Close.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 129 | 130 | 131 | ..\Resources\Compile.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 132 | 133 | 134 | ..\Resources\Content.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 135 | 136 | 137 | ..\Resources\CSharp.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 138 | 139 | 140 | ..\Resources\Default.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 141 | 142 | 143 | ..\Resources\Field.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 144 | 145 | 146 | ..\Resources\Header.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 147 | 148 | 149 | ..\Resources\Help.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 150 | 151 | 152 | ..\Resources\Method.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 153 | 154 | 155 | ..\Resources\Move.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 156 | 157 | 158 | ..\Resources\Namespace.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 159 | 160 | 161 | ..\Resources\Next.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 162 | 163 | 164 | ..\Resources\Open.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 165 | 166 | 167 | ..\Resources\Pause.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 168 | 169 | 170 | ..\Resources\Preview.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 171 | 172 | 173 | ..\Resources\Previous.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 174 | 175 | 176 | ..\Resources\Property.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 177 | 178 | 179 | ..\Resources\Reference.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 180 | 181 | 182 | ..\Resources\Remove.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 183 | 184 | 185 | ..\Resources\Save.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 186 | 187 | 188 | ..\Resources\SaveAll.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 189 | 190 | 191 | ..\Resources\SaveAs.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 192 | 193 | 194 | ..\Resources\Select.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 195 | 196 | 197 | ..\Resources\Start.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 198 | 199 | 200 | ..\Resources\Stop.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 201 | 202 | 203 | ..\Resources\Type.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 204 | 205 | -------------------------------------------------------------------------------- /Properties/Resources.Designer.cs: -------------------------------------------------------------------------------- 1 | //------------------------------------------------------------------------------ 2 | // 3 | // 此代码由工具生成。 4 | // 运行时版本:4.0.30319.42000 5 | // 6 | // 对此文件的更改可能会导致不正确的行为,并且如果 7 | // 重新生成代码,这些更改将会丢失。 8 | // 9 | //------------------------------------------------------------------------------ 10 | 11 | namespace CommonLibrary.CodeSystem.Properties { 12 | using System; 13 | 14 | 15 | /// 16 | /// 一个强类型的资源类,用于查找本地化的字符串等。 17 | /// 18 | // 此类是由 StronglyTypedResourceBuilder 19 | // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 20 | // 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen 21 | // (以 /str 作为命令选项),或重新生成 VS 项目。 22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] 23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] 25 | internal class Resources { 26 | 27 | private static global::System.Resources.ResourceManager resourceMan; 28 | 29 | private static global::System.Globalization.CultureInfo resourceCulture; 30 | 31 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] 32 | internal Resources() { 33 | } 34 | 35 | /// 36 | /// 返回此类使用的缓存的 ResourceManager 实例。 37 | /// 38 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 39 | internal static global::System.Resources.ResourceManager ResourceManager { 40 | get { 41 | if (object.ReferenceEquals(resourceMan, null)) { 42 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("CommonLibrary.CodeSystem.Properties.Resources", typeof(Resources).Assembly); 43 | resourceMan = temp; 44 | } 45 | return resourceMan; 46 | } 47 | } 48 | 49 | /// 50 | /// 重写当前线程的 CurrentUICulture 属性,对 51 | /// 使用此强类型资源类的所有资源查找执行重写。 52 | /// 53 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] 54 | internal static global::System.Globalization.CultureInfo Culture { 55 | get { 56 | return resourceCulture; 57 | } 58 | set { 59 | resourceCulture = value; 60 | } 61 | } 62 | 63 | /// 64 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 65 | /// 66 | internal static System.Drawing.Bitmap Add { 67 | get { 68 | object obj = ResourceManager.GetObject("Add", resourceCulture); 69 | return ((System.Drawing.Bitmap)(obj)); 70 | } 71 | } 72 | 73 | /// 74 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 75 | /// 76 | internal static System.Drawing.Bitmap Clear { 77 | get { 78 | object obj = ResourceManager.GetObject("Clear", resourceCulture); 79 | return ((System.Drawing.Bitmap)(obj)); 80 | } 81 | } 82 | 83 | /// 84 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 85 | /// 86 | internal static System.Drawing.Bitmap Close { 87 | get { 88 | object obj = ResourceManager.GetObject("Close", resourceCulture); 89 | return ((System.Drawing.Bitmap)(obj)); 90 | } 91 | } 92 | 93 | /// 94 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 95 | /// 96 | internal static System.Drawing.Bitmap Compile { 97 | get { 98 | object obj = ResourceManager.GetObject("Compile", resourceCulture); 99 | return ((System.Drawing.Bitmap)(obj)); 100 | } 101 | } 102 | 103 | /// 104 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 105 | /// 106 | internal static System.Drawing.Bitmap Content { 107 | get { 108 | object obj = ResourceManager.GetObject("Content", resourceCulture); 109 | return ((System.Drawing.Bitmap)(obj)); 110 | } 111 | } 112 | 113 | /// 114 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 115 | /// 116 | internal static System.Drawing.Bitmap CSharp { 117 | get { 118 | object obj = ResourceManager.GetObject("CSharp", resourceCulture); 119 | return ((System.Drawing.Bitmap)(obj)); 120 | } 121 | } 122 | 123 | /// 124 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 125 | /// 126 | internal static System.Drawing.Bitmap Default { 127 | get { 128 | object obj = ResourceManager.GetObject("Default", resourceCulture); 129 | return ((System.Drawing.Bitmap)(obj)); 130 | } 131 | } 132 | 133 | /// 134 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 135 | /// 136 | internal static System.Drawing.Bitmap Field { 137 | get { 138 | object obj = ResourceManager.GetObject("Field", resourceCulture); 139 | return ((System.Drawing.Bitmap)(obj)); 140 | } 141 | } 142 | 143 | /// 144 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 145 | /// 146 | internal static System.Drawing.Bitmap Header { 147 | get { 148 | object obj = ResourceManager.GetObject("Header", resourceCulture); 149 | return ((System.Drawing.Bitmap)(obj)); 150 | } 151 | } 152 | 153 | /// 154 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 155 | /// 156 | internal static System.Drawing.Bitmap Help { 157 | get { 158 | object obj = ResourceManager.GetObject("Help", resourceCulture); 159 | return ((System.Drawing.Bitmap)(obj)); 160 | } 161 | } 162 | 163 | /// 164 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 165 | /// 166 | internal static System.Drawing.Bitmap Method { 167 | get { 168 | object obj = ResourceManager.GetObject("Method", resourceCulture); 169 | return ((System.Drawing.Bitmap)(obj)); 170 | } 171 | } 172 | 173 | /// 174 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 175 | /// 176 | internal static System.Drawing.Bitmap Move { 177 | get { 178 | object obj = ResourceManager.GetObject("Move", resourceCulture); 179 | return ((System.Drawing.Bitmap)(obj)); 180 | } 181 | } 182 | 183 | /// 184 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 185 | /// 186 | internal static System.Drawing.Bitmap Namespace { 187 | get { 188 | object obj = ResourceManager.GetObject("Namespace", resourceCulture); 189 | return ((System.Drawing.Bitmap)(obj)); 190 | } 191 | } 192 | 193 | /// 194 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 195 | /// 196 | internal static System.Drawing.Bitmap Next { 197 | get { 198 | object obj = ResourceManager.GetObject("Next", resourceCulture); 199 | return ((System.Drawing.Bitmap)(obj)); 200 | } 201 | } 202 | 203 | /// 204 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 205 | /// 206 | internal static System.Drawing.Bitmap Open { 207 | get { 208 | object obj = ResourceManager.GetObject("Open", resourceCulture); 209 | return ((System.Drawing.Bitmap)(obj)); 210 | } 211 | } 212 | 213 | /// 214 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 215 | /// 216 | internal static System.Drawing.Bitmap Pause { 217 | get { 218 | object obj = ResourceManager.GetObject("Pause", resourceCulture); 219 | return ((System.Drawing.Bitmap)(obj)); 220 | } 221 | } 222 | 223 | /// 224 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 225 | /// 226 | internal static System.Drawing.Bitmap Preview { 227 | get { 228 | object obj = ResourceManager.GetObject("Preview", resourceCulture); 229 | return ((System.Drawing.Bitmap)(obj)); 230 | } 231 | } 232 | 233 | /// 234 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 235 | /// 236 | internal static System.Drawing.Bitmap Previous { 237 | get { 238 | object obj = ResourceManager.GetObject("Previous", resourceCulture); 239 | return ((System.Drawing.Bitmap)(obj)); 240 | } 241 | } 242 | 243 | /// 244 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 245 | /// 246 | internal static System.Drawing.Bitmap Property { 247 | get { 248 | object obj = ResourceManager.GetObject("Property", resourceCulture); 249 | return ((System.Drawing.Bitmap)(obj)); 250 | } 251 | } 252 | 253 | /// 254 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 255 | /// 256 | internal static System.Drawing.Bitmap Reference { 257 | get { 258 | object obj = ResourceManager.GetObject("Reference", resourceCulture); 259 | return ((System.Drawing.Bitmap)(obj)); 260 | } 261 | } 262 | 263 | /// 264 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 265 | /// 266 | internal static System.Drawing.Bitmap Remove { 267 | get { 268 | object obj = ResourceManager.GetObject("Remove", resourceCulture); 269 | return ((System.Drawing.Bitmap)(obj)); 270 | } 271 | } 272 | 273 | /// 274 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 275 | /// 276 | internal static System.Drawing.Bitmap Save { 277 | get { 278 | object obj = ResourceManager.GetObject("Save", resourceCulture); 279 | return ((System.Drawing.Bitmap)(obj)); 280 | } 281 | } 282 | 283 | /// 284 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 285 | /// 286 | internal static System.Drawing.Bitmap SaveAll { 287 | get { 288 | object obj = ResourceManager.GetObject("SaveAll", resourceCulture); 289 | return ((System.Drawing.Bitmap)(obj)); 290 | } 291 | } 292 | 293 | /// 294 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 295 | /// 296 | internal static System.Drawing.Bitmap SaveAs { 297 | get { 298 | object obj = ResourceManager.GetObject("SaveAs", resourceCulture); 299 | return ((System.Drawing.Bitmap)(obj)); 300 | } 301 | } 302 | 303 | /// 304 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 305 | /// 306 | internal static System.Drawing.Bitmap Select { 307 | get { 308 | object obj = ResourceManager.GetObject("Select", resourceCulture); 309 | return ((System.Drawing.Bitmap)(obj)); 310 | } 311 | } 312 | 313 | /// 314 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 315 | /// 316 | internal static System.Drawing.Bitmap Start { 317 | get { 318 | object obj = ResourceManager.GetObject("Start", resourceCulture); 319 | return ((System.Drawing.Bitmap)(obj)); 320 | } 321 | } 322 | 323 | /// 324 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 325 | /// 326 | internal static System.Drawing.Bitmap Stop { 327 | get { 328 | object obj = ResourceManager.GetObject("Stop", resourceCulture); 329 | return ((System.Drawing.Bitmap)(obj)); 330 | } 331 | } 332 | 333 | /// 334 | /// 查找 System.Drawing.Bitmap 类型的本地化资源。 335 | /// 336 | internal static System.Drawing.Bitmap Type { 337 | get { 338 | object obj = ResourceManager.GetObject("Type", resourceCulture); 339 | return ((System.Drawing.Bitmap)(obj)); 340 | } 341 | } 342 | } 343 | } 344 | -------------------------------------------------------------------------------- /CodeManager.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.CSharp; 2 | using System; 3 | using System.CodeDom.Compiler; 4 | using System.Collections.Generic; 5 | using System.IO; 6 | using System.Linq; 7 | using System.Reflection; 8 | using System.Text; 9 | using System.Threading; 10 | using System.Threading.Tasks; 11 | using System.Windows.Forms; 12 | 13 | namespace CommonLibrary.CodeSystem 14 | { 15 | public class CodeManager 16 | { 17 | private static readonly Lazy m_instance = new Lazy(() => new CodeManager()); 18 | 19 | private static readonly string m_strCodeDirectory = Path.Combine(Application.StartupPath, "CodeSystem"); 20 | private static readonly string m_strCodePartDirectory = Path.Combine(m_strCodeDirectory, "CodePart"); 21 | private static readonly string m_strThirdPartyDLLDirectory = Path.Combine(m_strCodeDirectory, "ThirdPartyDLL"); 22 | private static readonly string m_strHistoryMainCodePath = Path.Combine(m_strCodeDirectory, "MainCodePath.txt"); 23 | private static readonly string m_strCodeHeaderPath = Path.Combine(m_strCodePartDirectory, HEADER_PAGE); 24 | private static readonly string m_strCompiledCodePath = Path.Combine(m_strCodePartDirectory, GENERATE_PAGE); 25 | private static readonly string m_strCodeSystemReferencePath = Path.Combine(m_strCodePartDirectory, SYSTEM_REFERENCE_PAGE); 26 | private static readonly string m_strCodeCustomReferencePath = Path.Combine(m_strCodePartDirectory, CUSTOM_REFERENCE_PAGE); 27 | 28 | public const string NAMESPACE_NAME = "DynamicCodeGenerate"; 29 | public const string CLASS_NAME = "CodeSystem"; 30 | public const string EXECUTE_NAME = "Main"; 31 | public const string SUB_SUFFIX = ".sub"; 32 | public const string MAIN_PAGE = "Main.main"; 33 | public const string HEADER_PAGE = "Using.head"; 34 | public const string SYSTEM_REFERENCE_PAGE = "SystemReference.ref"; 35 | public const string CUSTOM_REFERENCE_PAGE = "CustomReference.ref"; 36 | public const string GENERATE_PAGE = "Code.cs"; 37 | 38 | public const string DEFAULT_HEADER = "using System;\r\n" + 39 | "using System.Collections.Generic;\r\n" + 40 | "using System.ComponentModel;\r\n" + 41 | "using System.Data;\r\n" + 42 | "using System.Diagnostics;\r\n" + 43 | "using System.IO;\r\n" + 44 | "using System.Linq;\r\n" + 45 | "using System.Reflection;\r\n" + 46 | "using System.Text;\r\n" + 47 | "using System.Threading;\r\n" + 48 | "using System.Threading.Tasks;\r\n" + 49 | "using System.Windows.Forms;"; 50 | 51 | public const string DEFAULT_SYSTEM_REFERENCE = "Microsoft.CSharp.dll\r\n" + 52 | "System.dll\r\n" + 53 | "System.Core.dll\r\n" + 54 | "System.Data.dll\r\n" + 55 | "System.Data.DataSetExtensions.dll\r\n" + 56 | "System.Deployment.dll\r\n" + 57 | "System.Drawing.dll\r\n" + 58 | "System.Net.Http.dll\r\n" + 59 | "System.Windows.Forms.dll\r\n" + 60 | "System.Xml.dll\r\n" + 61 | "System.Xml.Linq.dll"; 62 | 63 | private CompilerResults m_compilerResults; 64 | private List m_listSystemReferences = new List(); 65 | private List m_listCustomReferences = new List(); 66 | private Thread m_threadCode; 67 | private string m_strErrors = string.Empty; 68 | private CodeStatus m_codeStatus = CodeStatus.Idle; 69 | 70 | public static CodeManager Instance { get => m_instance.Value; } 71 | 72 | public delegate void StatusChangeEventHandler(); 73 | public StatusChangeEventHandler OnStatusChange { get; set; } 74 | public string MainCodeFullPath { get; set; } 75 | public string CodeDirectory => m_strCodeDirectory; 76 | public string CodePartDirectory => m_strCodePartDirectory; 77 | public string ThirdPartyDLLDirectory => m_strThirdPartyDLLDirectory; 78 | public HashSet SubMethods = new HashSet(); 79 | public List CustomReferences => m_listCustomReferences; 80 | 81 | public CodeStatus Status 82 | { 83 | get { return m_codeStatus; } 84 | set 85 | { 86 | m_codeStatus = value; 87 | OnStatusChange?.Invoke(); 88 | } 89 | } 90 | 91 | private CodeManager() 92 | { 93 | if (!Directory.Exists(m_strCodeDirectory)) 94 | { 95 | Directory.CreateDirectory(m_strCodeDirectory); 96 | } 97 | if (!Directory.Exists(m_strCodePartDirectory)) 98 | { 99 | Directory.CreateDirectory(m_strCodePartDirectory); 100 | } 101 | if (!Directory.Exists(m_strThirdPartyDLLDirectory)) 102 | { 103 | Directory.CreateDirectory(m_strThirdPartyDLLDirectory); 104 | } 105 | if (!File.Exists(m_strCodeHeaderPath)) 106 | { 107 | using (StreamWriter sw = new StreamWriter(m_strCodeHeaderPath)) 108 | { 109 | sw.Write(DEFAULT_HEADER); 110 | } 111 | } 112 | if (!File.Exists(m_strCodeSystemReferencePath)) 113 | { 114 | using (StreamWriter sw = new StreamWriter(m_strCodeSystemReferencePath)) 115 | { 116 | sw.Write(DEFAULT_SYSTEM_REFERENCE); 117 | } 118 | } 119 | string[] strSubMethods = Directory.GetFiles(m_strCodePartDirectory, $"*{SUB_SUFFIX}"); 120 | foreach (string strMethod in strSubMethods) 121 | { 122 | SubMethods.Add(Path.GetFileName(strMethod)); 123 | } 124 | if (File.Exists(m_strHistoryMainCodePath)) 125 | { 126 | using (StreamReader sr = new StreamReader(m_strHistoryMainCodePath)) 127 | { 128 | MainCodeFullPath = sr.ReadLine(); 129 | if (!File.Exists(MainCodeFullPath)) 130 | { 131 | MainCodeFullPath = Path.Combine(m_strCodePartDirectory, MAIN_PAGE); 132 | } 133 | } 134 | } 135 | else 136 | { 137 | MainCodeFullPath = Path.Combine(m_strCodePartDirectory, MAIN_PAGE); 138 | } 139 | GetCodeReference(); 140 | 141 | AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; 142 | } 143 | 144 | private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) 145 | { 146 | Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); 147 | Assembly[] select = assemblies.Where(x => x.FullName.Equals(args.Name, StringComparison.OrdinalIgnoreCase)).ToArray(); 148 | if (select.Length == 0) 149 | { 150 | try 151 | { 152 | string strDLLPath; 153 | string strDLLName = $"{args.Name.Split(',')[0]}.dll"; 154 | Assembly assembly = null; 155 | strDLLPath = Path.Combine(Application.StartupPath, strDLLName); 156 | if (File.Exists(strDLLPath)) 157 | { 158 | assembly = Assembly.LoadFile(strDLLPath); 159 | } 160 | else 161 | { 162 | strDLLPath = Path.Combine(m_strThirdPartyDLLDirectory, strDLLName); 163 | if (File.Exists(strDLLPath)) 164 | { 165 | assembly = Assembly.LoadFile(strDLLPath); 166 | } 167 | } 168 | return assembly; 169 | } 170 | catch 171 | { 172 | return null; 173 | } 174 | } 175 | else 176 | { 177 | return select[0]; 178 | } 179 | } 180 | 181 | private string GenerateCode() 182 | { 183 | StringBuilder sb = new StringBuilder(); 184 | sb.Append(GetCodeHeader()); 185 | sb.Append(Environment.NewLine); 186 | sb.Append(Environment.NewLine); 187 | sb.Append($"namespace {NAMESPACE_NAME}\r\n"); 188 | sb.Append("{\r\n"); 189 | sb.Append($" public class {CLASS_NAME}\r\n"); 190 | sb.Append(" {\r\n"); 191 | sb.Append($" public void {EXECUTE_NAME}()\r\n"); 192 | sb.Append(" {\r\n"); 193 | sb.Append(GetMainCode()); 194 | sb.Append(" }\r\n"); 195 | sb.Append(GetSubMethodCode()); 196 | sb.Append(" }\r\n"); 197 | sb.Append("}"); 198 | 199 | string code = sb.ToString(); 200 | return code; 201 | } 202 | 203 | private void GetCodeReference() 204 | { 205 | if (File.Exists(m_strCodeSystemReferencePath)) 206 | { 207 | using (StreamReader sr = new StreamReader(m_strCodeSystemReferencePath)) 208 | { 209 | string strDLLs = sr.ReadToEnd(); 210 | m_listSystemReferences = strDLLs.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList(); 211 | } 212 | } 213 | else 214 | { 215 | m_listSystemReferences = DEFAULT_SYSTEM_REFERENCE.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList(); 216 | } 217 | if (File.Exists(m_strCodeCustomReferencePath)) 218 | { 219 | using (StreamReader sr = new StreamReader(m_strCodeCustomReferencePath)) 220 | { 221 | string strDLLs = sr.ReadToEnd(); 222 | m_listCustomReferences = strDLLs.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList(); 223 | } 224 | } 225 | } 226 | 227 | private string GetCodeHeader() 228 | { 229 | string strHeaderCode = string.Empty; 230 | if (File.Exists(m_strCodeHeaderPath)) 231 | { 232 | using (StreamReader sr = new StreamReader(m_strCodeHeaderPath)) 233 | { 234 | strHeaderCode = sr.ReadToEnd(); 235 | } 236 | } 237 | else 238 | { 239 | strHeaderCode = DEFAULT_HEADER; 240 | } 241 | return strHeaderCode; 242 | } 243 | 244 | private string GetMainCode() 245 | { 246 | string strMainCode; 247 | StringBuilder stringBuilder = new StringBuilder(); 248 | if (File.Exists(MainCodeFullPath)) 249 | { 250 | using (StreamReader sr = new StreamReader(MainCodeFullPath)) 251 | { 252 | while (!sr.EndOfStream) 253 | { 254 | stringBuilder.AppendLine($"\t\t\t{sr.ReadLine()}"); 255 | } 256 | } 257 | } 258 | strMainCode = stringBuilder.ToString(); 259 | return strMainCode; 260 | } 261 | 262 | private string GetSubMethodCode() 263 | { 264 | StringBuilder sb = new StringBuilder(); 265 | string strSubMethodPath; 266 | foreach (string strSubMethod in SubMethods) 267 | { 268 | strSubMethodPath = Path.Combine(m_strCodePartDirectory, strSubMethod); 269 | if (File.Exists(strSubMethodPath)) 270 | { 271 | using (StreamReader sr = new StreamReader(strSubMethodPath)) 272 | { 273 | sb.Append(Environment.NewLine); 274 | while (!sr.EndOfStream) 275 | { 276 | sb.AppendLine($"\t\t{sr.ReadLine()}"); 277 | } 278 | } 279 | } 280 | } 281 | return sb.ToString(); 282 | } 283 | 284 | private void Run() 285 | { 286 | if ((m_compilerResults != null) && (!m_compilerResults.Errors.HasErrors)) 287 | { 288 | try 289 | { 290 | Assembly assembly = m_compilerResults.CompiledAssembly; 291 | object objectInstance = assembly.CreateInstance($"{NAMESPACE_NAME}.{CLASS_NAME}"); 292 | if (objectInstance == null) 293 | { 294 | MessageBox.Show("创建实例失败,请检查代码."); 295 | return; 296 | } 297 | MethodInfo methodInfo = objectInstance.GetType().GetMethod(EXECUTE_NAME); 298 | if (methodInfo == null) 299 | { 300 | MessageBox.Show("执行方法不存在,请检查代码."); 301 | return; 302 | } 303 | methodInfo.Invoke(objectInstance, null); 304 | Status = CodeStatus.Idle; 305 | } 306 | catch (Exception ex) 307 | { 308 | MessageBox.Show($"{ex.Message}\r\n{(ex.InnerException != null ? ex.InnerException.Message : string.Empty)}"); 309 | Status = CodeStatus.AbnormalStop; 310 | } 311 | } 312 | else 313 | { 314 | MessageBox.Show("代码编译失败,请检查代码"); 315 | } 316 | } 317 | 318 | public bool Compile() 319 | { 320 | bool bIsCompileSuccess; 321 | try 322 | { 323 | CSharpCodeProvider cSharpCodeProvider = new CSharpCodeProvider(); 324 | 325 | CompilerParameters compilerParameters = new CompilerParameters(); 326 | GetCodeReference(); 327 | if (m_listSystemReferences.Count != 0) 328 | { 329 | for (int i = 0; i < m_listSystemReferences.Count; i++) 330 | { 331 | compilerParameters.ReferencedAssemblies.Add(m_listSystemReferences[i]); 332 | } 333 | } 334 | if (m_listCustomReferences.Count != 0) 335 | { 336 | string strDLLPath; 337 | for (int i = 0; i < m_listCustomReferences.Count; i++) 338 | { 339 | strDLLPath = Path.Combine(Application.StartupPath, m_listCustomReferences[i]); 340 | if (File.Exists(strDLLPath)) 341 | { 342 | compilerParameters.ReferencedAssemblies.Add(strDLLPath); 343 | } 344 | else 345 | { 346 | strDLLPath = Path.Combine(m_strThirdPartyDLLDirectory, m_listCustomReferences[i]); 347 | if (File.Exists(strDLLPath)) 348 | { 349 | compilerParameters.ReferencedAssemblies.Add(strDLLPath); 350 | } 351 | } 352 | } 353 | } 354 | compilerParameters.GenerateExecutable = false; 355 | compilerParameters.GenerateInMemory = true; 356 | string strFullCode = GenerateCode(); 357 | m_compilerResults = cSharpCodeProvider.CompileAssemblyFromSource(compilerParameters, strFullCode); 358 | if (m_compilerResults.Errors.HasErrors) 359 | { 360 | string strError = string.Empty; 361 | strError += "编译错误:\r\n"; 362 | foreach (CompilerError err in m_compilerResults.Errors) 363 | { 364 | strError += $"Line{err.Line}:{err.ErrorText}\r\n"; 365 | } 366 | m_strErrors = strError; 367 | m_compilerResults = null; 368 | bIsCompileSuccess = false; 369 | } 370 | else 371 | { 372 | m_strErrors = "编译成功"; 373 | bIsCompileSuccess = true; 374 | } 375 | using (StreamWriter sw = new StreamWriter(m_strCompiledCodePath)) 376 | { 377 | sw.WriteLine(strFullCode); 378 | } 379 | } 380 | catch (Exception ex) 381 | { 382 | m_strErrors += $"编译错误:\r\n{ex.Message}"; 383 | m_compilerResults = null; 384 | bIsCompileSuccess = false; 385 | } 386 | return bIsCompileSuccess; 387 | } 388 | 389 | public bool RunCode() 390 | { 391 | if (Status == CodeStatus.Pause) 392 | { 393 | Status = CodeStatus.Run; 394 | m_threadCode.Resume(); 395 | } 396 | else if (Status == CodeStatus.Idle || Status == CodeStatus.AbnormalStop) 397 | { 398 | if ((m_compilerResults == null)) 399 | { 400 | if (!Compile()) 401 | return false; 402 | } 403 | 404 | Status = CodeStatus.Run; 405 | m_threadCode = new Thread(Run); 406 | m_threadCode.IsBackground = true; 407 | m_threadCode.Start(); 408 | } 409 | return true; 410 | } 411 | 412 | public void PauseCode() 413 | { 414 | m_threadCode.Suspend(); 415 | Status = CodeStatus.Pause; 416 | } 417 | 418 | public void StopCode() 419 | { 420 | try 421 | { 422 | if ((m_threadCode.ThreadState & ThreadState.Suspended) == ThreadState.Suspended) 423 | { 424 | m_threadCode.Resume(); 425 | } 426 | m_threadCode.Abort(); 427 | } 428 | catch 429 | { 430 | } 431 | Status = CodeStatus.Idle; 432 | } 433 | 434 | public string GetErrors() 435 | { 436 | return m_strErrors; 437 | } 438 | 439 | public void SaveHistoryCodePath() 440 | { 441 | if (!Directory.Exists(CodeDirectory)) 442 | { 443 | Directory.CreateDirectory(CodeDirectory); 444 | } 445 | using (StreamWriter sw = new StreamWriter(m_strHistoryMainCodePath)) 446 | { 447 | sw.Write(MainCodeFullPath); 448 | } 449 | } 450 | } 451 | } 452 | -------------------------------------------------------------------------------- /Views/CodeEditor.cs: -------------------------------------------------------------------------------- 1 | using CommonLibrary.CodeSystem.Controls; 2 | using CommonLibrary.CodeSystem.Presenters.Interfaces; 3 | using Microsoft.CSharp; 4 | using ScintillaNET; 5 | using System; 6 | using System.CodeDom.Compiler; 7 | using System.Collections.Generic; 8 | using System.ComponentModel; 9 | using System.Data; 10 | using System.Diagnostics; 11 | using System.Drawing; 12 | using System.IO; 13 | using System.Linq; 14 | using System.Reflection; 15 | using System.Text; 16 | using System.Threading.Tasks; 17 | using System.Windows.Forms; 18 | 19 | namespace CommonLibrary.CodeSystem.Views 20 | { 21 | public partial class CodeEditor : Form, ICodeEditor 22 | { 23 | private bool m_bIsSearchShow = false; 24 | private Point m_searchPanelLocation = Point.Empty; 25 | 26 | private TabPage m_currentCodePage = null; 27 | private TabPage m_mainCodePage = null; 28 | private TabPage m_selectClosePage = null; 29 | 30 | public TabPage CurrentCodePage => m_currentCodePage; 31 | public TabPage MainCodePage => m_mainCodePage; 32 | public TabPage SelectClosePage => m_selectClosePage; 33 | public TabControl TabControlCode => tabControlCode; 34 | public TextBox MessageTextBox => textBoxError; 35 | public ListView ListViewMethod => listViewMethods; 36 | public ToolStripStatusLabel CodePathLabel => toolStripStatusLabel1; 37 | 38 | public event Action ClearCode; 39 | public event Action OpenCode; 40 | public event Action SaveAsCode; 41 | public event Action SaveAllCode; 42 | public event Action CompileCode; 43 | public event Action StopCode; 44 | public event Action PauseCode; 45 | public event Action StartCode; 46 | public event Action PreviousSearch; 47 | public event Action NextSearch; 48 | public event Action AddSubMethod; 49 | public event Action CloseAllPages; 50 | public event Action SetDefaultHeader; 51 | public event Action SetDefaultSystemReference; 52 | public event Action MoveCustomReference; 53 | public event Action ObtainCustomReference; 54 | public event Action AddCodePage; 55 | public event Action CloseSpecifiedPage; 56 | public event Action CloseOtherPages; 57 | public event Action RemoveSubMethod; 58 | public event Action OpenSubMethod; 59 | public event Action SaveCode; 60 | 61 | public CodeEditor() 62 | { 63 | InitializeComponent(); 64 | } 65 | 66 | private void CodeEdit_Load(object sender, EventArgs e) 67 | { 68 | AddCodePage?.Invoke(CodeManager.MAIN_PAGE); 69 | if (tabControlCode.TabPages.Count > 0) 70 | { 71 | m_currentCodePage = tabControlCode.TabPages[0]; 72 | m_mainCodePage = tabControlCode.TabPages[0]; 73 | toolStripStatusLabel1.Text = $"当前脚本: {CodeManager.Instance.MainCodeFullPath}"; 74 | } 75 | 76 | CodeManager.Instance.OnStatusChange += OnStatusChange; 77 | SetToolStrip(); 78 | InitHotkeys(); 79 | InitSubToolStrip(); 80 | InitSubMethodList(); 81 | } 82 | 83 | private void SetToolStrip() 84 | { 85 | switch (CodeManager.Instance.Status) 86 | { 87 | case CodeStatus.Idle: 88 | case CodeStatus.AbnormalStop: 89 | toolStripButtonRun.Enabled = true; 90 | toolStripButtonPause.Enabled = false; 91 | toolStripButtonStop.Enabled = false; 92 | toolStripMenuItemRun.Enabled = true; 93 | toolStripMenuItemPause.Enabled = false; 94 | toolStripMenuItemStop.Enabled = false; 95 | GC.Collect(); 96 | break; 97 | case CodeStatus.Run: 98 | toolStripButtonRun.Enabled = false; 99 | toolStripButtonPause.Enabled = true; 100 | toolStripButtonStop.Enabled = true; 101 | toolStripMenuItemRun.Enabled = false; 102 | toolStripMenuItemPause.Enabled = true; 103 | toolStripMenuItemStop.Enabled = true; 104 | break; 105 | case CodeStatus.Pause: 106 | toolStripButtonRun.Enabled = true; 107 | toolStripButtonPause.Enabled = false; 108 | toolStripButtonStop.Enabled = true; 109 | toolStripMenuItemRun.Enabled = true; 110 | toolStripMenuItemPause.Enabled = false; 111 | toolStripMenuItemStop.Enabled = true; 112 | break; 113 | default: 114 | break; 115 | } 116 | } 117 | 118 | private void InitHotkeys() 119 | { 120 | //注册热键 121 | HotKeyManager.AddHotKey(this, OpenSearch, Keys.F, true); 122 | } 123 | 124 | private void InitSubToolStrip() 125 | { 126 | toolStripButtonDefaultHeader.Visible = false; 127 | toolStripButtonDefaultSystemReference.Visible = false; 128 | } 129 | 130 | private void InitSubMethodList() 131 | { 132 | foreach (string subMethod in CodeManager.Instance.SubMethods) 133 | { 134 | listViewMethods.Items.Add(subMethod, subMethod, null); 135 | } 136 | } 137 | 138 | private void InvokeIfNeeded(Action action) 139 | { 140 | if (this.InvokeRequired) 141 | { 142 | this.BeginInvoke(action); 143 | } 144 | else 145 | { 146 | action.Invoke(); 147 | } 148 | } 149 | 150 | private void toolStripButtonCompile_Click(object sender, EventArgs e) 151 | { 152 | Compile(); 153 | } 154 | 155 | private void toolStripButtonRun_Click(object sender, EventArgs e) 156 | { 157 | RunCode(); 158 | } 159 | 160 | private void toolStripButtonPause_Click(object sender, EventArgs e) 161 | { 162 | PauseRunningCode(); 163 | } 164 | 165 | private void toolStripButtonStop_Click(object sender, EventArgs e) 166 | { 167 | StopRunningCode(); 168 | } 169 | 170 | private void toolStripButtonClear_Click(object sender, EventArgs e) 171 | { 172 | ClearCurrentPageCode(); 173 | } 174 | 175 | private void toolStripButtonOpen_Click(object sender, EventArgs e) 176 | { 177 | OpenMainCode(); 178 | } 179 | 180 | private void toolStripButtonSave_Click(object sender, EventArgs e) 181 | { 182 | SaveCurrentPageCode(); 183 | } 184 | 185 | private void OnStatusChange() 186 | { 187 | Action action = new Action(() => 188 | { 189 | SetToolStrip(); 190 | }); 191 | InvokeIfNeeded(action); 192 | } 193 | 194 | private void CodeEdit_FormClosing(object sender, FormClosingEventArgs e) 195 | { 196 | CodeManager.Instance.OnStatusChange -= OnStatusChange; 197 | } 198 | 199 | private void tabControlCode_Selected(object sender, TabControlEventArgs e) 200 | { 201 | m_currentCodePage = e.TabPage; 202 | if (m_bIsSearchShow) 203 | { 204 | CodeEditPage codeEditPage = null; 205 | if (CurrentCodePage != null) 206 | { 207 | codeEditPage = CurrentCodePage.Controls[0] as CodeEditPage; 208 | } 209 | SearchManager.TextArea = codeEditPage; 210 | SearchManager.Find(true); 211 | } 212 | toolStripButtonDefaultHeader.Visible = m_currentCodePage.Name.Equals(CodeManager.HEADER_PAGE, StringComparison.OrdinalIgnoreCase); 213 | toolStripButtonDefaultSystemReference.Visible = m_currentCodePage.Name.Equals(CodeManager.SYSTEM_REFERENCE_PAGE, StringComparison.OrdinalIgnoreCase); 214 | } 215 | 216 | #region Search 217 | 218 | private void OpenSearch() 219 | { 220 | SearchManager.SearchBox = TxtSearch; 221 | CodeEditPage codeEditPage = null; 222 | if (CurrentCodePage != null) 223 | { 224 | codeEditPage = CurrentCodePage.Controls[0] as CodeEditPage; 225 | } 226 | SearchManager.TextArea = codeEditPage; 227 | 228 | if (!m_bIsSearchShow) 229 | { 230 | m_bIsSearchShow = true; 231 | InvokeIfNeeded(delegate () 232 | { 233 | PanelSearch.Visible = true; 234 | TxtSearch.Text = SearchManager.LastSearch; 235 | TxtSearch.Focus(); 236 | TxtSearch.SelectAll(); 237 | }); 238 | } 239 | else 240 | { 241 | InvokeIfNeeded(delegate () 242 | { 243 | TxtSearch.Focus(); 244 | TxtSearch.SelectAll(); 245 | }); 246 | } 247 | } 248 | 249 | private void TxtSearch_TextChanged(object sender, EventArgs e) 250 | { 251 | SearchManager.Find(true); 252 | } 253 | 254 | private void TxtSearch_KeyDown(object sender, KeyEventArgs e) 255 | { 256 | if (HotKeyManager.IsHotkey(e, Keys.Enter)) 257 | { 258 | SearchManager.Find(true); 259 | } 260 | if (HotKeyManager.IsHotkey(e, Keys.Enter, true) || HotKeyManager.IsHotkey(e, Keys.Enter, false, true)) 261 | { 262 | SearchManager.Find(false); 263 | } 264 | } 265 | 266 | private void BtnPrevSearch_Click(object sender, EventArgs e) 267 | { 268 | PreviousSearch?.Invoke(); 269 | } 270 | 271 | private void BtnNextSearch_Click(object sender, EventArgs e) 272 | { 273 | NextSearch?.Invoke(); 274 | } 275 | 276 | private void BtnCloseSearch_Click(object sender, EventArgs e) 277 | { 278 | if (m_bIsSearchShow) 279 | { 280 | m_bIsSearchShow = false; 281 | SearchManager.TextArea = null; 282 | SearchManager.SearchBox = null; 283 | InvokeIfNeeded(delegate () 284 | { 285 | PanelSearch.Visible = false; 286 | }); 287 | } 288 | } 289 | 290 | private void PanelSearch_MouseDown(object sender, MouseEventArgs e) 291 | { 292 | m_searchPanelLocation = Cursor.Position; 293 | PanelSearch.Cursor = Cursors.NoMove2D; 294 | } 295 | 296 | private void PanelSearch_MouseMove(object sender, MouseEventArgs e) 297 | { 298 | if (e.Button == MouseButtons.Left) 299 | { 300 | int nOffsetX = Cursor.Position.X - m_searchPanelLocation.X; 301 | int nOffsetY = Cursor.Position.Y - m_searchPanelLocation.Y; 302 | PanelSearch.Location = new Point(PanelSearch.Location.X + nOffsetX, PanelSearch.Location.Y + nOffsetY); 303 | 304 | m_searchPanelLocation = Cursor.Position; 305 | } 306 | } 307 | 308 | private void PanelSearch_MouseUp(object sender, MouseEventArgs e) 309 | { 310 | PanelSearch.Cursor = Cursors.Default; 311 | } 312 | 313 | #endregion 314 | 315 | private void toolStripButtonAddSub_Click(object sender, EventArgs e) 316 | { 317 | SetSubMethod(); 318 | } 319 | 320 | private void toolStripButtonRemoveSub_Click(object sender, EventArgs e) 321 | { 322 | RemoveSub(); 323 | } 324 | 325 | private void toolStripButtonOpenSub_Click(object sender, EventArgs e) 326 | { 327 | OpenSub(); 328 | } 329 | 330 | private void toolStripMenuItemClose_Click(object sender, EventArgs e) 331 | { 332 | CloseSpecifiedEditPage(m_selectClosePage); 333 | } 334 | 335 | private void toolStripMenuItemCloseOther_Click(object sender, EventArgs e) 336 | { 337 | CloseOtherEditPages(m_selectClosePage); 338 | } 339 | 340 | private void toolStripMenuItemCloseAll_Click(object sender, EventArgs e) 341 | { 342 | CloseAllEditPages(); 343 | } 344 | 345 | private void toolStripButtonSaveAll_Click(object sender, EventArgs e) 346 | { 347 | SaveAllPagesCode(); 348 | } 349 | 350 | private void toolStripButtonOpenHeader_Click(object sender, EventArgs e) 351 | { 352 | OpenHeaderCode(); 353 | } 354 | 355 | private void toolStripButtonDefaultHeader_Click(object sender, EventArgs e) 356 | { 357 | SetDefaultHeaderPage(); 358 | } 359 | 360 | private void toolStripButtonOpenSystemReference_Click(object sender, EventArgs e) 361 | { 362 | OpenSystemReferenceCode(); 363 | } 364 | 365 | private void toolStripButtonDefaultSystemReference_Click(object sender, EventArgs e) 366 | { 367 | SetDefaultSystemReferencePage(); 368 | } 369 | 370 | private void toolStripButtonOpenCustomReference_Click(object sender, EventArgs e) 371 | { 372 | OpenCustomReferenceCode(); 373 | } 374 | 375 | private void toolStripButtonMoveCustomReference_Click(object sender, EventArgs e) 376 | { 377 | MoveCustomReferenceLibrary(); 378 | } 379 | 380 | private void toolStripButtonObtainCustomReference_Click(object sender, EventArgs e) 381 | { 382 | ObtainCustomReferenceLibrary(); 383 | } 384 | 385 | private void toolStripButtonPreview_Click(object sender, EventArgs e) 386 | { 387 | Preview(); 388 | } 389 | 390 | private void listViewMethods_DoubleClick(object sender, EventArgs e) 391 | { 392 | OpenSubMethodPage(); 393 | } 394 | 395 | private void toolStripMenuItemCreateSub_Click(object sender, EventArgs e) 396 | { 397 | SetSubMethod(); 398 | } 399 | 400 | private void toolStripMenuItemRemoveSub_Click(object sender, EventArgs e) 401 | { 402 | RemoveSub(); 403 | } 404 | 405 | private void toolStripMenuItemOpenSub_Click(object sender, EventArgs e) 406 | { 407 | OpenSubMethodPage(); 408 | } 409 | 410 | private void toolStripMenuItemCloseSub_Click(object sender, EventArgs e) 411 | { 412 | if (listViewMethods.FocusedItem != null) 413 | { 414 | string strMethodName = listViewMethods.FocusedItem.Text; 415 | CloseSpecifiedPage?.Invoke(strMethodName); 416 | } 417 | } 418 | 419 | private void toolStripButtonSaveAs_Click(object sender, EventArgs e) 420 | { 421 | SaveAsMainCode(); 422 | } 423 | 424 | private void tabControlCode_MouseDown(object sender, MouseEventArgs e) 425 | { 426 | if (e.Button == MouseButtons.Right) 427 | { 428 | m_selectClosePage = GetTabPageByLocation(e.Location); 429 | } 430 | } 431 | 432 | private TabPage GetTabPageByLocation(Point point) 433 | { 434 | for (int i = 0; i < tabControlCode.TabPages.Count; i++) 435 | { 436 | if (tabControlCode.GetTabRect(i).Contains(point)) 437 | { 438 | return tabControlCode.TabPages[i]; 439 | } 440 | } 441 | return null; 442 | } 443 | 444 | private void toolStripMenuItemOpen_Click(object sender, EventArgs e) 445 | { 446 | OpenMainCode(); 447 | } 448 | 449 | private void toolStripMenuItemOpenHeader_Click(object sender, EventArgs e) 450 | { 451 | OpenHeaderCode(); 452 | } 453 | 454 | private void toolStripMenuItemOpenSystemReference_Click(object sender, EventArgs e) 455 | { 456 | OpenSystemReferenceCode(); 457 | } 458 | 459 | private void toolStripMenuItemOpenCustomReference_Click(object sender, EventArgs e) 460 | { 461 | OpenCustomReferenceCode(); 462 | } 463 | 464 | private void toolStripMenuItemAddSub_Click(object sender, EventArgs e) 465 | { 466 | SetSubMethod(); 467 | } 468 | 469 | private void toolStripMenuItemMoveCustomReference_Click(object sender, EventArgs e) 470 | { 471 | MoveCustomReferenceLibrary(); 472 | } 473 | 474 | private void toolStripMenuItemObtainCustomReference_Click(object sender, EventArgs e) 475 | { 476 | ObtainCustomReferenceLibrary(); 477 | } 478 | 479 | private void toolStripMenuItemSaveAs_Click(object sender, EventArgs e) 480 | { 481 | SaveAsMainCode(); 482 | } 483 | 484 | private void toolStripMenuItemSave_Click(object sender, EventArgs e) 485 | { 486 | SaveCurrentPageCode(); 487 | } 488 | 489 | private void toolStripMenuItemSaveAll_Click(object sender, EventArgs e) 490 | { 491 | SaveAllPagesCode(); 492 | } 493 | 494 | private void toolStripMenuItemCloseThisPage_Click(object sender, EventArgs e) 495 | { 496 | CloseSpecifiedEditPage(m_currentCodePage); 497 | } 498 | 499 | private void toolStripMenuItemCloseOtherPages_Click(object sender, EventArgs e) 500 | { 501 | CloseOtherEditPages(m_currentCodePage); 502 | } 503 | 504 | private void toolStripMenuItemCloseAllPages_Click(object sender, EventArgs e) 505 | { 506 | CloseAllEditPages(); 507 | } 508 | 509 | private void toolStripMenuItemFind_Click(object sender, EventArgs e) 510 | { 511 | OpenSearch(); 512 | } 513 | 514 | private void toolStripMenuItemClear_Click(object sender, EventArgs e) 515 | { 516 | ClearCurrentPageCode(); 517 | } 518 | 519 | private void toolStripMenuItemDefaultHeader_Click(object sender, EventArgs e) 520 | { 521 | SetDefaultHeaderPage(); 522 | } 523 | 524 | private void toolStripMenuItemDefaultSystemReference_Click(object sender, EventArgs e) 525 | { 526 | SetDefaultSystemReferencePage(); 527 | } 528 | 529 | private void toolStripMenuItemCompile_Click(object sender, EventArgs e) 530 | { 531 | Compile(); 532 | } 533 | 534 | private void toolStripMenuItemPreview_Click(object sender, EventArgs e) 535 | { 536 | Preview(); 537 | } 538 | 539 | private void toolStripMenuItemRun_Click(object sender, EventArgs e) 540 | { 541 | RunCode(); 542 | } 543 | 544 | private void toolStripMenuItemPause_Click(object sender, EventArgs e) 545 | { 546 | PauseRunningCode(); 547 | } 548 | 549 | private void toolStripMenuItemStop_Click(object sender, EventArgs e) 550 | { 551 | StopRunningCode(); 552 | } 553 | 554 | private void OpenMainCode() 555 | { 556 | OpenCode?.Invoke(); 557 | } 558 | 559 | private void OpenHeaderCode() 560 | { 561 | AddCodePage?.Invoke(CodeManager.HEADER_PAGE); 562 | } 563 | 564 | private void OpenSystemReferenceCode() 565 | { 566 | AddCodePage?.Invoke(CodeManager.SYSTEM_REFERENCE_PAGE); 567 | } 568 | 569 | private void OpenCustomReferenceCode() 570 | { 571 | AddCodePage?.Invoke(CodeManager.CUSTOM_REFERENCE_PAGE); 572 | } 573 | 574 | private void SetSubMethod() 575 | { 576 | AddSubMethod?.Invoke(); 577 | } 578 | 579 | private void RemoveSub() 580 | { 581 | if (listViewMethods.FocusedItem != null) 582 | { 583 | string strMethodName = listViewMethods.FocusedItem.Text; 584 | RemoveSubMethod?.Invoke(strMethodName); 585 | } 586 | } 587 | 588 | private void OpenSub() 589 | { 590 | if (listViewMethods.FocusedItem != null) 591 | { 592 | string strMethodName = listViewMethods.FocusedItem.Text; 593 | OpenSubMethod?.Invoke(strMethodName); 594 | } 595 | } 596 | 597 | private void SaveAsMainCode() 598 | { 599 | SaveAsCode?.Invoke(); 600 | } 601 | 602 | private void SaveCurrentPageCode() 603 | { 604 | SaveCode?.Invoke(CurrentCodePage); 605 | } 606 | 607 | private void SaveAllPagesCode() 608 | { 609 | SaveAllCode?.Invoke(); 610 | } 611 | 612 | private void ClearCurrentPageCode() 613 | { 614 | ClearCode?.Invoke(); 615 | } 616 | 617 | private void SetDefaultHeaderPage() 618 | { 619 | SetDefaultHeader?.Invoke(); 620 | } 621 | 622 | private void SetDefaultSystemReferencePage() 623 | { 624 | SetDefaultSystemReference?.Invoke(); 625 | } 626 | 627 | private void Compile() 628 | { 629 | CompileCode?.Invoke(); 630 | } 631 | 632 | private void Preview() 633 | { 634 | AddCodePage?.Invoke(CodeManager.GENERATE_PAGE); 635 | } 636 | 637 | private void RunCode() 638 | { 639 | StartCode?.Invoke(); 640 | } 641 | 642 | private void PauseRunningCode() 643 | { 644 | PauseCode?.Invoke(); 645 | } 646 | 647 | private void StopRunningCode() 648 | { 649 | StopCode?.Invoke(); 650 | } 651 | 652 | private void CloseSpecifiedEditPage(TabPage tabPage) 653 | { 654 | if (tabPage == null) 655 | return; 656 | CloseSpecifiedPage?.Invoke(tabPage.Name); 657 | } 658 | 659 | private void CloseOtherEditPages(TabPage tabPage) 660 | { 661 | if (tabPage == null) 662 | return; 663 | CloseOtherPages?.Invoke(tabPage.Name); 664 | } 665 | 666 | private void CloseAllEditPages() 667 | { 668 | CloseAllPages?.Invoke(); 669 | } 670 | 671 | private void OpenSubMethodPage() 672 | { 673 | if (listViewMethods.FocusedItem != null) 674 | { 675 | string strMethodName = listViewMethods.FocusedItem.Text; 676 | OpenSubMethod?.Invoke(strMethodName); 677 | } 678 | } 679 | 680 | private void MoveCustomReferenceLibrary() 681 | { 682 | MoveCustomReference?.Invoke(); 683 | } 684 | 685 | private void ObtainCustomReferenceLibrary() 686 | { 687 | ObtainCustomReference?.Invoke(); 688 | } 689 | } 690 | } 691 | -------------------------------------------------------------------------------- /Presenters/CodeEditorPresenter.cs: -------------------------------------------------------------------------------- 1 | using CommonLibrary.CodeSystem.Controls; 2 | using CommonLibrary.CodeSystem.Presenters.Interfaces; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.IO; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | using System.Windows.Forms; 10 | 11 | namespace CommonLibrary.CodeSystem.Presenters 12 | { 13 | public class CodeEditorPresenter 14 | { 15 | private const string MODIFY_FLAG = "*"; 16 | 17 | private ICodeEditor m_view; 18 | private CustomReferenceForm m_customReferenceForm = null; 19 | 20 | public CodeEditorPresenter(ICodeEditor view) 21 | { 22 | m_view = view; 23 | 24 | m_view.ClearCode += ClearCode; 25 | m_view.OpenCode += OpenCode; 26 | m_view.SaveAsCode += SaveAsCode; 27 | m_view.SaveCode += SaveCode; 28 | m_view.SaveAllCode += SaveAllCode; 29 | m_view.CompileCode += CompileCode; 30 | m_view.StopCode += StopCode; 31 | m_view.PauseCode += PauseCode; 32 | m_view.StartCode += StartCode; 33 | m_view.PreviousSearch += PreviousSearch; 34 | m_view.NextSearch += NextSearch; 35 | m_view.AddCodePage += AddCodePage; 36 | m_view.AddSubMethod += AddSubMethod; 37 | m_view.RemoveSubMethod += RemoveSubMethod; 38 | m_view.OpenSubMethod += OpenSubMethod; 39 | m_view.CloseSpecifiedPage += CloseSpecifiedPage; 40 | m_view.CloseOtherPages += CloseOtherPages; 41 | m_view.CloseAllPages += CloseAllPages; 42 | m_view.SetDefaultHeader += SetDefaultHeader; 43 | m_view.SetDefaultSystemReference += SetDefaultSystemReference; 44 | m_view.MoveCustomReference += MoveCustomReference; 45 | m_view.ObtainCustomReference += ObtainCustomReference; 46 | } 47 | 48 | private void ObtainCustomReference() 49 | { 50 | if (m_customReferenceForm == null) 51 | { 52 | m_customReferenceForm = new CustomReferenceForm(); 53 | } 54 | m_customReferenceForm.Show(); 55 | m_customReferenceForm.Focus(); 56 | } 57 | 58 | private void MoveCustomReference() 59 | { 60 | using (OpenFileDialog ofd = new OpenFileDialog()) 61 | { 62 | ofd.Filter = "动态库|*.dll"; 63 | ofd.Multiselect = true; 64 | if (ofd.ShowDialog() == DialogResult.OK) 65 | { 66 | string strMessage = string.Empty; 67 | foreach (string strFileName in ofd.SafeFileNames) 68 | { 69 | string strNewDLLFullPath = Path.Combine(Application.StartupPath, strFileName); 70 | try 71 | { 72 | if (File.Exists(strNewDLLFullPath)) 73 | { 74 | File.Copy(ofd.FileName, strNewDLLFullPath, true); 75 | strMessage += $"{strFileName} 成功\r\n"; 76 | } 77 | else 78 | { 79 | strNewDLLFullPath = Path.Combine(CodeManager.Instance.ThirdPartyDLLDirectory, strFileName); 80 | File.Copy(ofd.FileName, strNewDLLFullPath, true); 81 | strMessage += $"{strFileName} 成功 \r\n"; 82 | } 83 | } 84 | catch (Exception ex) 85 | { 86 | strMessage += $"{strFileName} 失败: {ex.Message}\r\n"; 87 | } 88 | } 89 | strMessage = $"增加引用动态库\r\n{strMessage}"; 90 | ShowMessage(strMessage); 91 | } 92 | } 93 | } 94 | 95 | private void SaveAsCode() 96 | { 97 | using (SaveFileDialog ofd = new SaveFileDialog()) 98 | { 99 | ofd.Filter = "脚本文件|*.main"; 100 | if (ofd.ShowDialog() == DialogResult.OK) 101 | { 102 | if (m_view.MainCodePage != null) 103 | { 104 | using (StreamWriter sw = new StreamWriter(ofd.FileName)) 105 | { 106 | CodeEditPage codeEditPage = m_view.MainCodePage.Controls[0] as CodeEditPage; 107 | if (codeEditPage != null) 108 | { 109 | sw.Write(codeEditPage.Text); 110 | CodeManager.Instance.MainCodeFullPath = ofd.FileName; 111 | CodeManager.Instance.SaveHistoryCodePath(); 112 | m_view.CodePathLabel.Text = $"当前脚本: {CodeManager.Instance.MainCodeFullPath}"; 113 | RemoveModifyFlag(m_view.MainCodePage); 114 | ShowMessage($"{ofd.FileName}保存成功."); 115 | } 116 | } 117 | } 118 | } 119 | } 120 | } 121 | 122 | private void SetDefaultSystemReference() 123 | { 124 | if (m_view.CurrentCodePage != null) 125 | { 126 | if (m_view.CurrentCodePage.Name.Equals(CodeManager.SYSTEM_REFERENCE_PAGE, StringComparison.OrdinalIgnoreCase)) 127 | { 128 | CodeEditPage codeEditPage = m_view.CurrentCodePage.Controls[0] as CodeEditPage; 129 | if (codeEditPage != null) 130 | { 131 | codeEditPage.Text = CodeManager.DEFAULT_SYSTEM_REFERENCE; 132 | ShowMessage("恢复默认系统Reference文件."); 133 | } 134 | } 135 | } 136 | } 137 | 138 | private void SetDefaultHeader() 139 | { 140 | if (m_view.CurrentCodePage != null) 141 | { 142 | if (m_view.CurrentCodePage.Name.Equals(CodeManager.HEADER_PAGE, StringComparison.OrdinalIgnoreCase)) 143 | { 144 | CodeEditPage codeEditPage = m_view.CurrentCodePage.Controls[0] as CodeEditPage; 145 | if (codeEditPage != null) 146 | { 147 | codeEditPage.Text = CodeManager.DEFAULT_HEADER; 148 | ShowMessage("恢复默认Header代码."); 149 | } 150 | } 151 | } 152 | } 153 | 154 | private void SaveAllCode() 155 | { 156 | for (int i = 0; i < m_view.TabControlCode.TabPages.Count; i++) 157 | { 158 | TabPage tabPage = m_view.TabControlCode.TabPages[i]; 159 | SaveCode(tabPage); 160 | } 161 | } 162 | 163 | private void CloseAllPages() 164 | { 165 | int nTabPageCount = m_view.TabControlCode.TabPages.Count; 166 | string strMessage = string.Empty; 167 | for (int i = 0; i < nTabPageCount; i++) 168 | { 169 | if (m_view.TabControlCode.TabPages[i].Text.EndsWith(MODIFY_FLAG)) 170 | { 171 | strMessage += $"{m_view.TabControlCode.TabPages[i].Name}\r\n"; 172 | } 173 | } 174 | if (!string.IsNullOrEmpty(strMessage)) 175 | { 176 | strMessage = $"是否保存对以下各项的更改?\r\n{strMessage}"; 177 | switch (MessageBox.Show(strMessage, "脚本编辑", MessageBoxButtons.YesNoCancel)) 178 | { 179 | case DialogResult.Cancel: 180 | return; 181 | case DialogResult.Yes: 182 | SaveAllCode(); 183 | break; 184 | case DialogResult.No: 185 | break; 186 | } 187 | } 188 | for (int i = nTabPageCount - 1; i > 0; i--) 189 | { 190 | m_view.TabControlCode.TabPages.RemoveAt(i); 191 | } 192 | } 193 | 194 | private void CloseOtherPages(string strMethodName) 195 | { 196 | int nTabPageCount = m_view.TabControlCode.TabPages.Count; 197 | string strMessage = string.Empty; 198 | for (int i = 0; i < nTabPageCount; i++) 199 | { 200 | if (m_view.TabControlCode.TabPages[i].Name.Equals(strMethodName, StringComparison.OrdinalIgnoreCase)) 201 | continue; 202 | if (m_view.TabControlCode.TabPages[i].Text.EndsWith(MODIFY_FLAG)) 203 | { 204 | strMessage += $"{m_view.TabControlCode.TabPages[i].Name}\r\n"; 205 | } 206 | } 207 | if (!string.IsNullOrEmpty(strMessage)) 208 | { 209 | strMessage = $"是否保存对以下各项的更改?\r\n{strMessage}"; 210 | switch (MessageBox.Show(strMessage, "脚本编辑", MessageBoxButtons.YesNoCancel)) 211 | { 212 | case DialogResult.Cancel: 213 | return; 214 | case DialogResult.Yes: 215 | { 216 | for (int i = 0; i < nTabPageCount; i++) 217 | { 218 | if (m_view.TabControlCode.TabPages[i].Name.Equals(strMethodName, StringComparison.OrdinalIgnoreCase)) 219 | continue; 220 | SaveCode(m_view.TabControlCode.TabPages[i]); 221 | } 222 | } 223 | break; 224 | case DialogResult.No: 225 | break; 226 | } 227 | } 228 | for (int i = nTabPageCount - 1; i > 0; i--) 229 | { 230 | if (m_view.TabControlCode.TabPages[i].Name.Equals(strMethodName, StringComparison.OrdinalIgnoreCase)) 231 | continue; 232 | m_view.TabControlCode.TabPages.RemoveAt(i); 233 | } 234 | } 235 | 236 | private void CloseSpecifiedPage(string strMethodName) 237 | { 238 | if (strMethodName.Equals(CodeManager.MAIN_PAGE, StringComparison.OrdinalIgnoreCase)) 239 | return; 240 | if (m_view.TabControlCode.TabPages.IndexOfKey(strMethodName) != -1) 241 | { 242 | string strMessage = string.Empty; 243 | if (m_view.TabControlCode.TabPages[strMethodName].Text.EndsWith(MODIFY_FLAG)) 244 | { 245 | strMessage += $"是否保存对以下各项的更改?\r\n{m_view.TabControlCode.TabPages[strMethodName].Name}\r\n"; 246 | switch (MessageBox.Show(strMessage, "脚本编辑", MessageBoxButtons.YesNoCancel)) 247 | { 248 | case DialogResult.Cancel: 249 | return; 250 | case DialogResult.Yes: 251 | SaveCode(m_view.TabControlCode.TabPages[strMethodName]); 252 | break; 253 | case DialogResult.No: 254 | break; 255 | } 256 | } 257 | m_view.TabControlCode.TabPages.RemoveByKey(strMethodName); 258 | ShowMessage($"关闭{strMethodName}."); 259 | } 260 | } 261 | 262 | private void OpenSubMethod(string strMethodName) 263 | { 264 | AddCodePage(strMethodName); 265 | ShowMessage($"打开{strMethodName}."); 266 | } 267 | 268 | private void RemoveSubMethod(string strMethodName) 269 | { 270 | if (MessageBox.Show($"是否删除该Sub代码{strMethodName}", "代码编辑", MessageBoxButtons.YesNo) == DialogResult.Yes) 271 | { 272 | if (IsExistMethodCodePage(strMethodName)) 273 | { 274 | m_view.TabControlCode.TabPages.RemoveByKey(strMethodName); 275 | } 276 | m_view.ListViewMethod.Items.RemoveByKey(strMethodName); 277 | CodeManager.Instance.SubMethods.Remove(strMethodName); 278 | try 279 | { 280 | File.Delete(Path.Combine(CodeManager.Instance.CodePartDirectory, strMethodName)); 281 | } 282 | catch 283 | { 284 | } 285 | ShowMessage($"删除{strMethodName}."); 286 | } 287 | } 288 | 289 | private void AddSubMethod() 290 | { 291 | using (RenameForm renameForm = new RenameForm()) 292 | { 293 | if (renameForm.ShowDialog() == DialogResult.OK) 294 | { 295 | if (!string.IsNullOrEmpty(renameForm.MethodName)) 296 | { 297 | string strMethodName = renameForm.MethodName; 298 | strMethodName += strMethodName.EndsWith(CodeManager.SUB_SUFFIX) ? string.Empty : CodeManager.SUB_SUFFIX; 299 | if (CodeManager.Instance.SubMethods.Contains(strMethodName)) 300 | return; 301 | m_view.ListViewMethod.Items.Add(strMethodName, strMethodName, null); 302 | string strDirectory = CodeManager.Instance.CodePartDirectory; 303 | if (!Directory.Exists(strDirectory)) 304 | { 305 | Directory.CreateDirectory(strDirectory); 306 | } 307 | string strFullPath = Path.Combine(strDirectory, strMethodName); 308 | using (StreamWriter sw = new StreamWriter(strFullPath)) 309 | { 310 | switch (renameForm.SubType) 311 | { 312 | case SubType.Method: 313 | { 314 | sw.WriteLine($"private void {strMethodName.Replace(CodeManager.SUB_SUFFIX, string.Empty)}()"); 315 | sw.WriteLine("{"); 316 | sw.WriteLine(""); 317 | sw.WriteLine("}"); 318 | } 319 | break; 320 | case SubType.Class: 321 | { 322 | sw.WriteLine($"private class {strMethodName.Replace(CodeManager.SUB_SUFFIX, string.Empty)}"); 323 | sw.WriteLine("{"); 324 | sw.WriteLine(""); 325 | sw.WriteLine("}"); 326 | } 327 | break; 328 | default: 329 | break; 330 | } 331 | } 332 | CodeManager.Instance.SubMethods.Add(strMethodName); 333 | AddCodePage(strMethodName); 334 | ShowMessage($"增加{strMethodName}."); 335 | } 336 | } 337 | } 338 | } 339 | 340 | private void AddCodePage(string strMethodName) 341 | { 342 | if (string.IsNullOrEmpty(strMethodName)) 343 | return; 344 | 345 | if (!IsExistMethodCodePage(strMethodName)) 346 | { 347 | string strFullPath; 348 | if (strMethodName.Equals(CodeManager.MAIN_PAGE, StringComparison.OrdinalIgnoreCase)) 349 | { 350 | strFullPath = CodeManager.Instance.MainCodeFullPath; 351 | } 352 | else 353 | { 354 | strFullPath = Path.Combine(CodeManager.Instance.CodePartDirectory, strMethodName); 355 | } 356 | CodeEditPage codeEditPage = new CodeEditPage(); 357 | LoadCode(codeEditPage, strFullPath); 358 | if (strMethodName.Equals(CodeManager.GENERATE_PAGE, StringComparison.OrdinalIgnoreCase)) 359 | { 360 | codeEditPage.ReadOnly = true; 361 | } 362 | codeEditPage.CanEdit = true; 363 | TabPage tabPage = new TabPage(); 364 | tabPage.Name = strMethodName; 365 | tabPage.Text = strMethodName; 366 | codeEditPage.CodeChanged += () => 367 | { 368 | SetModifyFlag(tabPage); 369 | }; 370 | tabPage.Controls.Add(codeEditPage); 371 | m_view.TabControlCode.TabPages.Add(tabPage); 372 | m_view.TabControlCode.SelectedTab = tabPage; 373 | } 374 | else 375 | { 376 | if (m_view.TabControlCode.TabPages.IndexOfKey(strMethodName) != -1) 377 | { 378 | TabPage tabPage = m_view.TabControlCode.TabPages[strMethodName]; 379 | m_view.TabControlCode.SelectedTab = tabPage; 380 | if (strMethodName.Equals(CodeManager.GENERATE_PAGE, StringComparison.OrdinalIgnoreCase)) 381 | { 382 | CodeEditPage codeEditPage = tabPage.Controls[0] as CodeEditPage; 383 | string strFullPath = Path.Combine(CodeManager.Instance.CodePartDirectory, strMethodName); 384 | codeEditPage.ReadOnly = false; 385 | LoadCode(codeEditPage, strFullPath); 386 | codeEditPage.ReadOnly = true; 387 | } 388 | } 389 | } 390 | } 391 | 392 | private void NextSearch() 393 | { 394 | SearchManager.Find(true); 395 | } 396 | 397 | private void PreviousSearch() 398 | { 399 | SearchManager.Find(false); 400 | } 401 | 402 | private void StartCode() 403 | { 404 | SaveAllCode(); 405 | if (CodeManager.Instance.RunCode()) 406 | { 407 | ShowMessage("开始运行."); 408 | } 409 | else 410 | { 411 | ShowMessage(CodeManager.Instance.GetErrors()); 412 | } 413 | } 414 | 415 | private void PauseCode() 416 | { 417 | CodeManager.Instance.PauseCode(); 418 | ShowMessage("暂停运行."); 419 | } 420 | 421 | private void StopCode() 422 | { 423 | CodeManager.Instance.StopCode(); 424 | ShowMessage("停止运行."); 425 | } 426 | 427 | private void CompileCode() 428 | { 429 | SaveAllCode(); 430 | CodeManager.Instance.Compile(); 431 | AddCodePage(CodeManager.GENERATE_PAGE); 432 | ShowMessage(CodeManager.Instance.GetErrors()); 433 | } 434 | 435 | private void SaveCode(TabPage tabPage) 436 | { 437 | if (tabPage != null) 438 | { 439 | string strFullPath; 440 | string strSavePath = tabPage.Name; 441 | if (strSavePath.Equals(CodeManager.GENERATE_PAGE, StringComparison.OrdinalIgnoreCase)) 442 | return; 443 | if (strSavePath.Equals(CodeManager.MAIN_PAGE, StringComparison.OrdinalIgnoreCase)) 444 | { 445 | strFullPath = CodeManager.Instance.MainCodeFullPath; 446 | } 447 | else 448 | { 449 | string strDirectory = CodeManager.Instance.CodePartDirectory; 450 | if (!Directory.Exists(strDirectory)) 451 | { 452 | Directory.CreateDirectory(strDirectory); 453 | } 454 | strFullPath = Path.Combine(strDirectory, strSavePath); 455 | } 456 | using (StreamWriter sw = new StreamWriter(strFullPath)) 457 | { 458 | CodeEditPage codeEditPage = tabPage.Controls[0] as CodeEditPage; 459 | if (codeEditPage != null) 460 | { 461 | sw.Write(codeEditPage.Text); 462 | RemoveModifyFlag(tabPage); 463 | ShowMessage($"{strSavePath}保存成功."); 464 | } 465 | } 466 | } 467 | } 468 | 469 | private void OpenCode() 470 | { 471 | if (m_view.MainCodePage == null) 472 | return; 473 | string strMessage = string.Empty; 474 | if (m_view.MainCodePage.Text.EndsWith(MODIFY_FLAG)) 475 | { 476 | strMessage += $"是否保存对以下各项的更改?\r\n{m_view.MainCodePage.Name}\r\n"; 477 | switch (MessageBox.Show(strMessage, "脚本编辑", MessageBoxButtons.YesNoCancel)) 478 | { 479 | case DialogResult.Cancel: 480 | return; 481 | case DialogResult.Yes: 482 | SaveCode(m_view.MainCodePage); 483 | break; 484 | case DialogResult.No: 485 | break; 486 | } 487 | } 488 | using (OpenFileDialog ofd = new OpenFileDialog()) 489 | { 490 | ofd.Filter = "脚本文件|*.main"; 491 | if (ofd.ShowDialog() == DialogResult.OK) 492 | { 493 | if (m_view.MainCodePage != null) 494 | { 495 | CodeEditPage codeEditPage = m_view.MainCodePage.Controls[0] as CodeEditPage; 496 | LoadCode(codeEditPage, ofd.FileName); 497 | codeEditPage.CanEdit = true; 498 | CodeManager.Instance.MainCodeFullPath = ofd.FileName; 499 | CodeManager.Instance.SaveHistoryCodePath(); 500 | m_view.CodePathLabel.Text = $"当前脚本: {CodeManager.Instance.MainCodeFullPath}"; 501 | RemoveModifyFlag(m_view.MainCodePage); 502 | } 503 | } 504 | } 505 | } 506 | 507 | private void ClearCode() 508 | { 509 | if (m_view.CurrentCodePage != null) 510 | { 511 | CodeEditPage codeEditPage = m_view.CurrentCodePage.Controls[0] as CodeEditPage; 512 | if (codeEditPage != null) 513 | { 514 | codeEditPage.Text = string.Empty; 515 | ShowMessage("清空代码."); 516 | } 517 | } 518 | } 519 | 520 | private bool IsExistMethodCodePage(string strMethodName) 521 | { 522 | return m_view.TabControlCode.TabPages.IndexOfKey(strMethodName) != -1; 523 | } 524 | 525 | private void ShowMessage(string strMessage) 526 | { 527 | m_view.MessageTextBox.AppendText($"{DateTime.Now}: {strMessage}\r\n"); 528 | } 529 | 530 | private void LoadCode(CodeEditPage codeEditPage, string strCodeFullPath) 531 | { 532 | if (codeEditPage == null || !File.Exists(strCodeFullPath)) 533 | return; 534 | try 535 | { 536 | codeEditPage.CanEdit = false; 537 | codeEditPage.Text = File.ReadAllText(strCodeFullPath); 538 | } 539 | catch 540 | { 541 | } 542 | } 543 | 544 | private void SetModifyFlag(TabPage tabPage) 545 | { 546 | if (!tabPage.Text.EndsWith(MODIFY_FLAG)) 547 | { 548 | tabPage.Text += MODIFY_FLAG; 549 | } 550 | } 551 | 552 | private void RemoveModifyFlag(TabPage tabPage) 553 | { 554 | if (tabPage.Text.EndsWith(MODIFY_FLAG)) 555 | { 556 | tabPage.Text = tabPage.Text.Remove(tabPage.Text.Length - 1); 557 | } 558 | } 559 | } 560 | } 561 | --------------------------------------------------------------------------------