fasdfsa commited on
Commit
ae59d0c
·
1 Parent(s): 9efa605

add WpfEditor

Browse files
.gitattributes CHANGED
@@ -1,6 +1,7 @@
1
  *.7z filter=lfs diff=lfs merge=lfs -text
2
  data/img/*.txt filter=lfs diff=lfs merge=lfs -text
3
  src/WeChatOcrCpp/wco_data/**/* filter=lfs diff=lfs merge=lfs -text
 
4
  *.ico filter=lfs diff=lfs merge=lfs -text
5
  *.pgm filter=lfs diff=lfs merge=lfs -text
6
  *.exe filter=lfs diff=lfs merge=lfs -text
 
1
  *.7z filter=lfs diff=lfs merge=lfs -text
2
  data/img/*.txt filter=lfs diff=lfs merge=lfs -text
3
  src/WeChatOcrCpp/wco_data/**/* filter=lfs diff=lfs merge=lfs -text
4
+ *.aps filter=lfs diff=lfs merge=lfs -text
5
  *.ico filter=lfs diff=lfs merge=lfs -text
6
  *.pgm filter=lfs diff=lfs merge=lfs -text
7
  *.exe filter=lfs diff=lfs merge=lfs -text
imradv3.sln CHANGED
@@ -15,62 +15,102 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "imgui", "3rdparty\imgui\img
15
  EndProject
16
  Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WeChatOcrCpp", "src\WeChatOcrCpp\WeChatOcrCpp.vcxproj", "{47E51CB9-6A07-4DF0-A458-6AD4C9D18A07}"
17
  EndProject
 
 
18
  Global
19
  GlobalSection(SolutionConfigurationPlatforms) = preSolution
 
20
  Debug|x64 = Debug|x64
21
  Debug|x86 = Debug|x86
 
22
  Release|x64 = Release|x64
23
  Release|x86 = Release|x86
24
  EndGlobalSection
25
  GlobalSection(ProjectConfigurationPlatforms) = postSolution
 
 
26
  {36ACB059-3EF6-45DD-B61D-B0B2ADCD1F8B}.Debug|x64.ActiveCfg = Debug|x64
27
  {36ACB059-3EF6-45DD-B61D-B0B2ADCD1F8B}.Debug|x64.Build.0 = Debug|x64
28
  {36ACB059-3EF6-45DD-B61D-B0B2ADCD1F8B}.Debug|x86.ActiveCfg = Debug|Win32
29
  {36ACB059-3EF6-45DD-B61D-B0B2ADCD1F8B}.Debug|x86.Build.0 = Debug|Win32
 
 
30
  {36ACB059-3EF6-45DD-B61D-B0B2ADCD1F8B}.Release|x64.ActiveCfg = Release|x64
31
  {36ACB059-3EF6-45DD-B61D-B0B2ADCD1F8B}.Release|x64.Build.0 = Release|x64
32
  {36ACB059-3EF6-45DD-B61D-B0B2ADCD1F8B}.Release|x86.ActiveCfg = Release|Win32
33
  {36ACB059-3EF6-45DD-B61D-B0B2ADCD1F8B}.Release|x86.Build.0 = Release|Win32
 
 
34
  {90456857-17A4-4A7A-9DC3-F6FC57B7BAF3}.Debug|x64.ActiveCfg = Debug|x64
35
  {90456857-17A4-4A7A-9DC3-F6FC57B7BAF3}.Debug|x64.Build.0 = Debug|x64
36
  {90456857-17A4-4A7A-9DC3-F6FC57B7BAF3}.Debug|x86.ActiveCfg = Debug|Win32
37
  {90456857-17A4-4A7A-9DC3-F6FC57B7BAF3}.Debug|x86.Build.0 = Debug|Win32
 
 
38
  {90456857-17A4-4A7A-9DC3-F6FC57B7BAF3}.Release|x64.ActiveCfg = Release|x64
39
  {90456857-17A4-4A7A-9DC3-F6FC57B7BAF3}.Release|x64.Build.0 = Release|x64
40
  {90456857-17A4-4A7A-9DC3-F6FC57B7BAF3}.Release|x86.ActiveCfg = Release|Win32
41
  {90456857-17A4-4A7A-9DC3-F6FC57B7BAF3}.Release|x86.Build.0 = Release|Win32
 
 
42
  {4754BE14-F57E-4CCF-B84B-368B56937109}.Debug|x64.ActiveCfg = Debug|x64
43
  {4754BE14-F57E-4CCF-B84B-368B56937109}.Debug|x64.Build.0 = Debug|x64
44
  {4754BE14-F57E-4CCF-B84B-368B56937109}.Debug|x86.ActiveCfg = Debug|Win32
45
  {4754BE14-F57E-4CCF-B84B-368B56937109}.Debug|x86.Build.0 = Debug|Win32
 
 
46
  {4754BE14-F57E-4CCF-B84B-368B56937109}.Release|x64.ActiveCfg = Release|x64
47
  {4754BE14-F57E-4CCF-B84B-368B56937109}.Release|x64.Build.0 = Release|x64
48
  {4754BE14-F57E-4CCF-B84B-368B56937109}.Release|x86.ActiveCfg = Release|Win32
49
  {4754BE14-F57E-4CCF-B84B-368B56937109}.Release|x86.Build.0 = Release|Win32
 
 
50
  {E15C71C1-901B-4D92-8CB0-5877EF7FF7F7}.Debug|x64.ActiveCfg = Debug|x64
51
  {E15C71C1-901B-4D92-8CB0-5877EF7FF7F7}.Debug|x64.Build.0 = Debug|x64
52
  {E15C71C1-901B-4D92-8CB0-5877EF7FF7F7}.Debug|x86.ActiveCfg = Debug|Win32
53
  {E15C71C1-901B-4D92-8CB0-5877EF7FF7F7}.Debug|x86.Build.0 = Debug|Win32
 
 
54
  {E15C71C1-901B-4D92-8CB0-5877EF7FF7F7}.Release|x64.ActiveCfg = Release|x64
55
  {E15C71C1-901B-4D92-8CB0-5877EF7FF7F7}.Release|x64.Build.0 = Release|x64
56
  {E15C71C1-901B-4D92-8CB0-5877EF7FF7F7}.Release|x86.ActiveCfg = Release|Win32
57
  {E15C71C1-901B-4D92-8CB0-5877EF7FF7F7}.Release|x86.Build.0 = Release|Win32
 
 
58
  {08DE3CE9-D0C0-4426-AAA6-1ADA88494BE3}.Debug|x64.ActiveCfg = Debug|x64
59
  {08DE3CE9-D0C0-4426-AAA6-1ADA88494BE3}.Debug|x64.Build.0 = Debug|x64
60
  {08DE3CE9-D0C0-4426-AAA6-1ADA88494BE3}.Debug|x86.ActiveCfg = Debug|Win32
61
  {08DE3CE9-D0C0-4426-AAA6-1ADA88494BE3}.Debug|x86.Build.0 = Debug|Win32
 
 
62
  {08DE3CE9-D0C0-4426-AAA6-1ADA88494BE3}.Release|x64.ActiveCfg = Release|x64
63
  {08DE3CE9-D0C0-4426-AAA6-1ADA88494BE3}.Release|x64.Build.0 = Release|x64
64
  {08DE3CE9-D0C0-4426-AAA6-1ADA88494BE3}.Release|x86.ActiveCfg = Release|Win32
65
  {08DE3CE9-D0C0-4426-AAA6-1ADA88494BE3}.Release|x86.Build.0 = Release|Win32
 
 
66
  {47E51CB9-6A07-4DF0-A458-6AD4C9D18A07}.Debug|x64.ActiveCfg = Debug|x64
67
  {47E51CB9-6A07-4DF0-A458-6AD4C9D18A07}.Debug|x64.Build.0 = Debug|x64
68
  {47E51CB9-6A07-4DF0-A458-6AD4C9D18A07}.Debug|x86.ActiveCfg = Debug|Win32
69
  {47E51CB9-6A07-4DF0-A458-6AD4C9D18A07}.Debug|x86.Build.0 = Debug|Win32
 
 
70
  {47E51CB9-6A07-4DF0-A458-6AD4C9D18A07}.Release|x64.ActiveCfg = Release|x64
71
  {47E51CB9-6A07-4DF0-A458-6AD4C9D18A07}.Release|x64.Build.0 = Release|x64
72
  {47E51CB9-6A07-4DF0-A458-6AD4C9D18A07}.Release|x86.ActiveCfg = Release|Win32
73
  {47E51CB9-6A07-4DF0-A458-6AD4C9D18A07}.Release|x86.Build.0 = Release|Win32
 
 
 
 
 
 
 
 
 
 
 
 
74
  EndGlobalSection
75
  GlobalSection(SolutionProperties) = preSolution
76
  HideSolutionNode = FALSE
 
15
  EndProject
16
  Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WeChatOcrCpp", "src\WeChatOcrCpp\WeChatOcrCpp.vcxproj", "{47E51CB9-6A07-4DF0-A458-6AD4C9D18A07}"
17
  EndProject
18
+ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WpfEditor", "src\WpfEditor\WpfEditor.csproj", "{A995A5F1-6A44-463F-9284-A3F058575206}"
19
+ EndProject
20
  Global
21
  GlobalSection(SolutionConfigurationPlatforms) = preSolution
22
+ Debug|Any CPU = Debug|Any CPU
23
  Debug|x64 = Debug|x64
24
  Debug|x86 = Debug|x86
25
+ Release|Any CPU = Release|Any CPU
26
  Release|x64 = Release|x64
27
  Release|x86 = Release|x86
28
  EndGlobalSection
29
  GlobalSection(ProjectConfigurationPlatforms) = postSolution
30
+ {36ACB059-3EF6-45DD-B61D-B0B2ADCD1F8B}.Debug|Any CPU.ActiveCfg = Debug|x64
31
+ {36ACB059-3EF6-45DD-B61D-B0B2ADCD1F8B}.Debug|Any CPU.Build.0 = Debug|x64
32
  {36ACB059-3EF6-45DD-B61D-B0B2ADCD1F8B}.Debug|x64.ActiveCfg = Debug|x64
33
  {36ACB059-3EF6-45DD-B61D-B0B2ADCD1F8B}.Debug|x64.Build.0 = Debug|x64
34
  {36ACB059-3EF6-45DD-B61D-B0B2ADCD1F8B}.Debug|x86.ActiveCfg = Debug|Win32
35
  {36ACB059-3EF6-45DD-B61D-B0B2ADCD1F8B}.Debug|x86.Build.0 = Debug|Win32
36
+ {36ACB059-3EF6-45DD-B61D-B0B2ADCD1F8B}.Release|Any CPU.ActiveCfg = Release|x64
37
+ {36ACB059-3EF6-45DD-B61D-B0B2ADCD1F8B}.Release|Any CPU.Build.0 = Release|x64
38
  {36ACB059-3EF6-45DD-B61D-B0B2ADCD1F8B}.Release|x64.ActiveCfg = Release|x64
39
  {36ACB059-3EF6-45DD-B61D-B0B2ADCD1F8B}.Release|x64.Build.0 = Release|x64
40
  {36ACB059-3EF6-45DD-B61D-B0B2ADCD1F8B}.Release|x86.ActiveCfg = Release|Win32
41
  {36ACB059-3EF6-45DD-B61D-B0B2ADCD1F8B}.Release|x86.Build.0 = Release|Win32
42
+ {90456857-17A4-4A7A-9DC3-F6FC57B7BAF3}.Debug|Any CPU.ActiveCfg = Debug|x64
43
+ {90456857-17A4-4A7A-9DC3-F6FC57B7BAF3}.Debug|Any CPU.Build.0 = Debug|x64
44
  {90456857-17A4-4A7A-9DC3-F6FC57B7BAF3}.Debug|x64.ActiveCfg = Debug|x64
45
  {90456857-17A4-4A7A-9DC3-F6FC57B7BAF3}.Debug|x64.Build.0 = Debug|x64
46
  {90456857-17A4-4A7A-9DC3-F6FC57B7BAF3}.Debug|x86.ActiveCfg = Debug|Win32
47
  {90456857-17A4-4A7A-9DC3-F6FC57B7BAF3}.Debug|x86.Build.0 = Debug|Win32
48
+ {90456857-17A4-4A7A-9DC3-F6FC57B7BAF3}.Release|Any CPU.ActiveCfg = Release|x64
49
+ {90456857-17A4-4A7A-9DC3-F6FC57B7BAF3}.Release|Any CPU.Build.0 = Release|x64
50
  {90456857-17A4-4A7A-9DC3-F6FC57B7BAF3}.Release|x64.ActiveCfg = Release|x64
51
  {90456857-17A4-4A7A-9DC3-F6FC57B7BAF3}.Release|x64.Build.0 = Release|x64
52
  {90456857-17A4-4A7A-9DC3-F6FC57B7BAF3}.Release|x86.ActiveCfg = Release|Win32
53
  {90456857-17A4-4A7A-9DC3-F6FC57B7BAF3}.Release|x86.Build.0 = Release|Win32
54
+ {4754BE14-F57E-4CCF-B84B-368B56937109}.Debug|Any CPU.ActiveCfg = Debug|x64
55
+ {4754BE14-F57E-4CCF-B84B-368B56937109}.Debug|Any CPU.Build.0 = Debug|x64
56
  {4754BE14-F57E-4CCF-B84B-368B56937109}.Debug|x64.ActiveCfg = Debug|x64
57
  {4754BE14-F57E-4CCF-B84B-368B56937109}.Debug|x64.Build.0 = Debug|x64
58
  {4754BE14-F57E-4CCF-B84B-368B56937109}.Debug|x86.ActiveCfg = Debug|Win32
59
  {4754BE14-F57E-4CCF-B84B-368B56937109}.Debug|x86.Build.0 = Debug|Win32
60
+ {4754BE14-F57E-4CCF-B84B-368B56937109}.Release|Any CPU.ActiveCfg = Release|x64
61
+ {4754BE14-F57E-4CCF-B84B-368B56937109}.Release|Any CPU.Build.0 = Release|x64
62
  {4754BE14-F57E-4CCF-B84B-368B56937109}.Release|x64.ActiveCfg = Release|x64
63
  {4754BE14-F57E-4CCF-B84B-368B56937109}.Release|x64.Build.0 = Release|x64
64
  {4754BE14-F57E-4CCF-B84B-368B56937109}.Release|x86.ActiveCfg = Release|Win32
65
  {4754BE14-F57E-4CCF-B84B-368B56937109}.Release|x86.Build.0 = Release|Win32
66
+ {E15C71C1-901B-4D92-8CB0-5877EF7FF7F7}.Debug|Any CPU.ActiveCfg = Debug|x64
67
+ {E15C71C1-901B-4D92-8CB0-5877EF7FF7F7}.Debug|Any CPU.Build.0 = Debug|x64
68
  {E15C71C1-901B-4D92-8CB0-5877EF7FF7F7}.Debug|x64.ActiveCfg = Debug|x64
69
  {E15C71C1-901B-4D92-8CB0-5877EF7FF7F7}.Debug|x64.Build.0 = Debug|x64
70
  {E15C71C1-901B-4D92-8CB0-5877EF7FF7F7}.Debug|x86.ActiveCfg = Debug|Win32
71
  {E15C71C1-901B-4D92-8CB0-5877EF7FF7F7}.Debug|x86.Build.0 = Debug|Win32
72
+ {E15C71C1-901B-4D92-8CB0-5877EF7FF7F7}.Release|Any CPU.ActiveCfg = Release|x64
73
+ {E15C71C1-901B-4D92-8CB0-5877EF7FF7F7}.Release|Any CPU.Build.0 = Release|x64
74
  {E15C71C1-901B-4D92-8CB0-5877EF7FF7F7}.Release|x64.ActiveCfg = Release|x64
75
  {E15C71C1-901B-4D92-8CB0-5877EF7FF7F7}.Release|x64.Build.0 = Release|x64
76
  {E15C71C1-901B-4D92-8CB0-5877EF7FF7F7}.Release|x86.ActiveCfg = Release|Win32
77
  {E15C71C1-901B-4D92-8CB0-5877EF7FF7F7}.Release|x86.Build.0 = Release|Win32
78
+ {08DE3CE9-D0C0-4426-AAA6-1ADA88494BE3}.Debug|Any CPU.ActiveCfg = Debug|x64
79
+ {08DE3CE9-D0C0-4426-AAA6-1ADA88494BE3}.Debug|Any CPU.Build.0 = Debug|x64
80
  {08DE3CE9-D0C0-4426-AAA6-1ADA88494BE3}.Debug|x64.ActiveCfg = Debug|x64
81
  {08DE3CE9-D0C0-4426-AAA6-1ADA88494BE3}.Debug|x64.Build.0 = Debug|x64
82
  {08DE3CE9-D0C0-4426-AAA6-1ADA88494BE3}.Debug|x86.ActiveCfg = Debug|Win32
83
  {08DE3CE9-D0C0-4426-AAA6-1ADA88494BE3}.Debug|x86.Build.0 = Debug|Win32
84
+ {08DE3CE9-D0C0-4426-AAA6-1ADA88494BE3}.Release|Any CPU.ActiveCfg = Release|x64
85
+ {08DE3CE9-D0C0-4426-AAA6-1ADA88494BE3}.Release|Any CPU.Build.0 = Release|x64
86
  {08DE3CE9-D0C0-4426-AAA6-1ADA88494BE3}.Release|x64.ActiveCfg = Release|x64
87
  {08DE3CE9-D0C0-4426-AAA6-1ADA88494BE3}.Release|x64.Build.0 = Release|x64
88
  {08DE3CE9-D0C0-4426-AAA6-1ADA88494BE3}.Release|x86.ActiveCfg = Release|Win32
89
  {08DE3CE9-D0C0-4426-AAA6-1ADA88494BE3}.Release|x86.Build.0 = Release|Win32
90
+ {47E51CB9-6A07-4DF0-A458-6AD4C9D18A07}.Debug|Any CPU.ActiveCfg = Debug|x64
91
+ {47E51CB9-6A07-4DF0-A458-6AD4C9D18A07}.Debug|Any CPU.Build.0 = Debug|x64
92
  {47E51CB9-6A07-4DF0-A458-6AD4C9D18A07}.Debug|x64.ActiveCfg = Debug|x64
93
  {47E51CB9-6A07-4DF0-A458-6AD4C9D18A07}.Debug|x64.Build.0 = Debug|x64
94
  {47E51CB9-6A07-4DF0-A458-6AD4C9D18A07}.Debug|x86.ActiveCfg = Debug|Win32
95
  {47E51CB9-6A07-4DF0-A458-6AD4C9D18A07}.Debug|x86.Build.0 = Debug|Win32
96
+ {47E51CB9-6A07-4DF0-A458-6AD4C9D18A07}.Release|Any CPU.ActiveCfg = Release|x64
97
+ {47E51CB9-6A07-4DF0-A458-6AD4C9D18A07}.Release|Any CPU.Build.0 = Release|x64
98
  {47E51CB9-6A07-4DF0-A458-6AD4C9D18A07}.Release|x64.ActiveCfg = Release|x64
99
  {47E51CB9-6A07-4DF0-A458-6AD4C9D18A07}.Release|x64.Build.0 = Release|x64
100
  {47E51CB9-6A07-4DF0-A458-6AD4C9D18A07}.Release|x86.ActiveCfg = Release|Win32
101
  {47E51CB9-6A07-4DF0-A458-6AD4C9D18A07}.Release|x86.Build.0 = Release|Win32
102
+ {A995A5F1-6A44-463F-9284-A3F058575206}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
103
+ {A995A5F1-6A44-463F-9284-A3F058575206}.Debug|Any CPU.Build.0 = Debug|Any CPU
104
+ {A995A5F1-6A44-463F-9284-A3F058575206}.Debug|x64.ActiveCfg = Debug|Any CPU
105
+ {A995A5F1-6A44-463F-9284-A3F058575206}.Debug|x64.Build.0 = Debug|Any CPU
106
+ {A995A5F1-6A44-463F-9284-A3F058575206}.Debug|x86.ActiveCfg = Debug|Any CPU
107
+ {A995A5F1-6A44-463F-9284-A3F058575206}.Debug|x86.Build.0 = Debug|Any CPU
108
+ {A995A5F1-6A44-463F-9284-A3F058575206}.Release|Any CPU.ActiveCfg = Release|Any CPU
109
+ {A995A5F1-6A44-463F-9284-A3F058575206}.Release|Any CPU.Build.0 = Release|Any CPU
110
+ {A995A5F1-6A44-463F-9284-A3F058575206}.Release|x64.ActiveCfg = Debug|Any CPU
111
+ {A995A5F1-6A44-463F-9284-A3F058575206}.Release|x64.Build.0 = Debug|Any CPU
112
+ {A995A5F1-6A44-463F-9284-A3F058575206}.Release|x86.ActiveCfg = Release|Any CPU
113
+ {A995A5F1-6A44-463F-9284-A3F058575206}.Release|x86.Build.0 = Release|Any CPU
114
  EndGlobalSection
115
  GlobalSection(SolutionProperties) = preSolution
116
  HideSolutionNode = FALSE
imradv3.vcxproj CHANGED
@@ -18,9 +18,6 @@
18
  <Platform>x64</Platform>
19
  </ProjectConfiguration>
20
  </ItemGroup>
21
- <ItemGroup>
22
- <ResourceCompile Include="src\app.rc" />
23
- </ItemGroup>
24
  <ItemGroup>
25
  <ClInclude Include="src\binding_eval.h" />
26
  <ClInclude Include="src\binding_input.h" />
@@ -80,10 +77,6 @@
80
  <ClCompile Include="src\ui_table_cols.cpp" />
81
  <ClCompile Include="src\utils.cpp" />
82
  </ItemGroup>
83
- <ItemGroup>
84
- <Image Include="src\icon.ico" />
85
- <Image Include="src\icon.png" />
86
- </ItemGroup>
87
  <PropertyGroup Label="Globals">
88
  <VCProjectVersion>17.0</VCProjectVersion>
89
  <Keyword>Win32Proj</Keyword>
 
18
  <Platform>x64</Platform>
19
  </ProjectConfiguration>
20
  </ItemGroup>
 
 
 
21
  <ItemGroup>
22
  <ClInclude Include="src\binding_eval.h" />
23
  <ClInclude Include="src\binding_input.h" />
 
77
  <ClCompile Include="src\ui_table_cols.cpp" />
78
  <ClCompile Include="src\utils.cpp" />
79
  </ItemGroup>
 
 
 
 
80
  <PropertyGroup Label="Globals">
81
  <VCProjectVersion>17.0</VCProjectVersion>
82
  <Keyword>Win32Proj</Keyword>
src/WpfEditor/App.xaml ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ <Application x:Class="WpfEditor.App"
2
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4
+ xmlns:local="clr-namespace:WpfEditor"
5
+ StartupUri="MainWindow.xaml">
6
+ <Application.Resources>
7
+
8
+ </Application.Resources>
9
+ </Application>
src/WpfEditor/App.xaml.cs ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System.Configuration;
2
+ using System.Data;
3
+ using System.Windows;
4
+
5
+ namespace WpfEditor
6
+ {
7
+ /// <summary>
8
+ /// Interaction logic for App.xaml
9
+ /// </summary>
10
+ public partial class App : Application
11
+ {
12
+ }
13
+
14
+ }
src/WpfEditor/AssemblyInfo.cs ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ using System.Windows;
2
+
3
+ [assembly: ThemeInfo(
4
+ ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
5
+ //(used if a resource is not found in the page,
6
+ // or application resource dictionaries)
7
+ ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
8
+ //(used if a resource is not found in the page,
9
+ // app, or any theme specific resource dictionaries)
10
+ )]
src/WpfEditor/MainWindow.xaml ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <Window x:Class="WpfEditor.MainWindow"
2
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6
+ xmlns:local="clr-namespace:WpfEditor"
7
+ mc:Ignorable="d"
8
+ Title="WPF Text Editor" Height="600" Width="800">
9
+ <Grid>
10
+ <Grid.RowDefinitions>
11
+ <RowDefinition Height="Auto"/>
12
+ <RowDefinition Height="*"/>
13
+ <RowDefinition Height="Auto"/>
14
+ </Grid.RowDefinitions>
15
+
16
+ <!-- 菜单栏 -->
17
+ <Menu Grid.Row="0">
18
+ <MenuItem Header="文件">
19
+ <MenuItem Header="新建" Click="NewFile_Click"/>
20
+ <MenuItem Header="打开" Click="OpenFile_Click"/>
21
+ <MenuItem Header="保存" Click="SaveFile_Click"/>
22
+ <MenuItem Header="另存为" Click="SaveAsFile_Click"/>
23
+ <Separator/>
24
+ <MenuItem Header="退出" Click="Exit_Click"/>
25
+ </MenuItem>
26
+ <MenuItem Header="编辑">
27
+ <MenuItem Header="撤销" Command="ApplicationCommands.Undo"/>
28
+ <MenuItem Header="重做" Command="ApplicationCommands.Redo"/>
29
+ <Separator/>
30
+ <MenuItem Header="剪切" Command="ApplicationCommands.Cut"/>
31
+ <MenuItem Header="复制" Command="ApplicationCommands.Copy"/>
32
+ <MenuItem Header="粘贴" Command="ApplicationCommands.Paste"/>
33
+ <Separator/>
34
+ <MenuItem Header="全选" Command="ApplicationCommands.SelectAll"/>
35
+ </MenuItem>
36
+ <MenuItem Header="视图">
37
+ <MenuItem Header="显示行号" IsCheckable="True" IsChecked="True" Click="ToggleLineNumbers_Click"/>
38
+ <MenuItem Header="显示空白字符" IsCheckable="True" IsChecked="True" Click="ToggleWhitespaces_Click"/>
39
+ <MenuItem Header="高亮当前行" IsCheckable="True" IsChecked="True" Click="ToggleHighlightCurrentLine_Click"/>
40
+ </MenuItem>
41
+ </Menu>
42
+
43
+ <!-- 编辑器 -->
44
+ <local:TextEditorControl x:Name="Editor" Grid.Row="1"
45
+ Text=""
46
+ LineNumbersVisible="True"
47
+ ShowWhitespaces="True"
48
+ HighlightCurrentLine="True"
49
+ TabSize="4"/>
50
+
51
+ <!-- 状态栏 -->
52
+ <StatusBar Grid.Row="2">
53
+ <StatusBarItem>
54
+ <TextBlock x:Name="StatusText" Text="就绪"/>
55
+ </StatusBarItem>
56
+ <Separator/>
57
+ <StatusBarItem>
58
+ <TextBlock x:Name="CursorPositionText" Text="行: 1, 列: 1"/>
59
+ </StatusBarItem>
60
+ </StatusBar>
61
+ </Grid>
62
+ </Window>
src/WpfEditor/MainWindow.xaml.cs ADDED
@@ -0,0 +1,193 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using Microsoft.Win32;
2
+ using System;
3
+ using System.IO;
4
+ using System.Windows;
5
+ using System.Windows.Controls;
6
+
7
+ namespace WpfEditor
8
+ {
9
+ public partial class MainWindow : Window
10
+ {
11
+ private string _currentFilePath = null;
12
+ private bool _textChanged = false;
13
+
14
+ public MainWindow()
15
+ {
16
+ InitializeComponent();
17
+ Editor.TextChanged += Editor_TextChanged;
18
+ Editor.CursorPositionChanged += Editor_CursorPositionChanged;
19
+ }
20
+
21
+ private void Editor_TextChanged(object sender, EventArgs e)
22
+ {
23
+ _textChanged = true;
24
+ UpdateTitle();
25
+ }
26
+
27
+ private void Editor_CursorPositionChanged(object sender, CursorPositionChangedEventArgs e)
28
+ {
29
+ CursorPositionText.Text = $"行: {e.Line + 1}, 列: {e.Column + 1}";
30
+ }
31
+
32
+ private void UpdateTitle()
33
+ {
34
+ string fileName = _currentFilePath != null ? Path.GetFileName(_currentFilePath) : "无标题";
35
+ Title = $"{fileName}{(_textChanged ? "*" : "")} - WPF Text Editor";
36
+ }
37
+
38
+ private void NewFile_Click(object sender, RoutedEventArgs e)
39
+ {
40
+ if (_textChanged)
41
+ {
42
+ var result = MessageBox.Show("当前文件已修改,是否保存?", "保存确认", MessageBoxButton.YesNoCancel);
43
+ if (result == MessageBoxResult.Cancel)
44
+ return;
45
+ if (result == MessageBoxResult.Yes)
46
+ SaveFile();
47
+ }
48
+
49
+ Editor.Text = string.Empty;
50
+ _currentFilePath = null;
51
+ _textChanged = false;
52
+ UpdateTitle();
53
+ StatusText.Text = "新建文件";
54
+ }
55
+
56
+ private void OpenFile_Click(object sender, RoutedEventArgs e)
57
+ {
58
+ if (_textChanged)
59
+ {
60
+ var result = MessageBox.Show("当前文件已修改,是否保存?", "保存确认", MessageBoxButton.YesNoCancel);
61
+ if (result == MessageBoxResult.Cancel)
62
+ return;
63
+ if (result == MessageBoxResult.Yes)
64
+ SaveFile();
65
+ }
66
+
67
+ OpenFileDialog openFileDialog = new OpenFileDialog();
68
+ openFileDialog.Filter = "文本文件 (*.txt)|*.txt|C++ 文件 (*.cpp;*.h)|*.cpp;*.h|所有文件 (*.*)|*.*";
69
+ if (openFileDialog.ShowDialog() == true)
70
+ {
71
+ try
72
+ {
73
+ Editor.Text = File.ReadAllText(openFileDialog.FileName);
74
+ _currentFilePath = openFileDialog.FileName;
75
+ _textChanged = false;
76
+ UpdateTitle();
77
+ StatusText.Text = $"已打开: {_currentFilePath}";
78
+ }
79
+ catch (Exception ex)
80
+ {
81
+ MessageBox.Show($"打开文件时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
82
+ }
83
+ }
84
+ }
85
+
86
+ private void SaveFile_Click(object sender, RoutedEventArgs e)
87
+ {
88
+ SaveFile();
89
+ }
90
+
91
+ private void SaveAsFile_Click(object sender, RoutedEventArgs e)
92
+ {
93
+ SaveFileAs();
94
+ }
95
+
96
+ private void Exit_Click(object sender, RoutedEventArgs e)
97
+ {
98
+ Close();
99
+ }
100
+
101
+ private void ToggleLineNumbers_Click(object sender, RoutedEventArgs e)
102
+ {
103
+ var menuItem = sender as MenuItem;
104
+ Editor.LineNumbersVisible = menuItem.IsChecked;
105
+ }
106
+
107
+ private void ToggleWhitespaces_Click(object sender, RoutedEventArgs e)
108
+ {
109
+ var menuItem = sender as MenuItem;
110
+ Editor.ShowWhitespaces = menuItem.IsChecked;
111
+ }
112
+
113
+ private void ToggleHighlightCurrentLine_Click(object sender, RoutedEventArgs e)
114
+ {
115
+ var menuItem = sender as MenuItem;
116
+ Editor.HighlightCurrentLine = menuItem.IsChecked;
117
+ }
118
+
119
+ private bool SaveFile()
120
+ {
121
+ if (_currentFilePath == null)
122
+ {
123
+ return SaveFileAs();
124
+ }
125
+
126
+ try
127
+ {
128
+ File.WriteAllText(_currentFilePath, Editor.Text);
129
+ _textChanged = false;
130
+ UpdateTitle();
131
+ StatusText.Text = $"已保存: {_currentFilePath}";
132
+ return true;
133
+ }
134
+ catch (Exception ex)
135
+ {
136
+ MessageBox.Show($"保存文件时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
137
+ return false;
138
+ }
139
+ }
140
+
141
+ private bool SaveFileAs()
142
+ {
143
+ SaveFileDialog saveFileDialog = new SaveFileDialog();
144
+ saveFileDialog.Filter = "文本文件 (*.txt)|*.txt|C++ 文件 (*.cpp;*.h)|*.cpp;*.h|所有文件 (*.*)|*.*";
145
+ if (saveFileDialog.ShowDialog() == true)
146
+ {
147
+ try
148
+ {
149
+ File.WriteAllText(saveFileDialog.FileName, Editor.Text);
150
+ _currentFilePath = saveFileDialog.FileName;
151
+ _textChanged = false;
152
+ UpdateTitle();
153
+ StatusText.Text = $"已保存: {_currentFilePath}";
154
+ return true;
155
+ }
156
+ catch (Exception ex)
157
+ {
158
+ MessageBox.Show($"保存文件时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
159
+ }
160
+ }
161
+ return false;
162
+ }
163
+
164
+ protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
165
+ {
166
+ if (_textChanged)
167
+ {
168
+ var result = MessageBox.Show("当前文件已修改,是否保存?", "保存确认", MessageBoxButton.YesNoCancel);
169
+ if (result == MessageBoxResult.Cancel)
170
+ {
171
+ e.Cancel = true;
172
+ return;
173
+ }
174
+ if (result == MessageBoxResult.Yes)
175
+ SaveFile();
176
+ }
177
+
178
+ base.OnClosing(e);
179
+ }
180
+ }
181
+
182
+ public class CursorPositionChangedEventArgs : EventArgs
183
+ {
184
+ public int Line { get; set; }
185
+ public int Column { get; set; }
186
+
187
+ public CursorPositionChangedEventArgs(int line, int column)
188
+ {
189
+ Line = line;
190
+ Column = column;
191
+ }
192
+ }
193
+ }
src/WpfEditor/SyntaxHighlighter.cs ADDED
@@ -0,0 +1,223 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System;
2
+ using System.Collections.Generic;
3
+ using System.Text.RegularExpressions;
4
+ using System.Windows.Media;
5
+
6
+ namespace WpfEditor
7
+ {
8
+ public class SyntaxHighlighter
9
+ {
10
+ private Dictionary<string, Brush> _keywords = new Dictionary<string, Brush>();
11
+ private List<Regex> _patterns = new List<Regex>();
12
+ private Dictionary<Regex, Brush> _patternColors = new Dictionary<Regex, Brush>();
13
+ private string _commentStart = "/*";
14
+ private string _commentEnd = "*/";
15
+ private string _lineComment = "//";
16
+
17
+ public SyntaxHighlighter()
18
+ {
19
+ // 默认配置
20
+ SetupCppHighlighting();
21
+ }
22
+
23
+ public void SetupCppHighlighting()
24
+ {
25
+ // C++ 关键字
26
+ string[] keywords = new string[] {
27
+ "auto", "break", "case", "catch", "class", "const", "continue", "default", "delete", "do", "else",
28
+ "enum", "explicit", "export", "extern", "for", "friend", "goto", "if", "inline", "mutable", "namespace",
29
+ "new", "operator", "private", "protected", "public", "register", "return", "sizeof", "static", "struct",
30
+ "switch", "template", "this", "throw", "try", "typedef", "typeid", "typename", "union", "using",
31
+ "virtual", "volatile", "while"
32
+ };
33
+
34
+ foreach (var keyword in keywords)
35
+ {
36
+ _keywords[keyword] = Brushes.Blue;
37
+ }
38
+
39
+ // 数字
40
+ _patterns.Add(new Regex(@"\b\d+(\.\d+)?([eE][+-]?\d+)?\b"));
41
+ _patternColors[_patterns[_patterns.Count - 1]] = Brushes.DarkOrange;
42
+
43
+ // 字符串
44
+ _patterns.Add(new Regex(@"""\.\*?""")); // 使用两个连续的双引号表示一个双引号字符
45
+ _patternColors[_patterns[_patterns.Count - 1]] = Brushes.Brown;
46
+ // 字符
47
+ _patterns.Add(new Regex(@"'.'|'\\.'|'\\\\'"));
48
+ _patternColors[_patterns[_patterns.Count - 1]] = Brushes.Brown;
49
+
50
+ // 注释
51
+ _commentStart = "/*";
52
+ _commentEnd = "*/";
53
+ _lineComment = "//";
54
+ }
55
+
56
+ public void Colorize(List<TextLine> lines)
57
+ {
58
+ bool inMultiLineComment = false;
59
+
60
+ for (int i = 0; i < lines.Count; i++)
61
+ {
62
+ var line = lines[i];
63
+ string lineText = new string(line.Characters.ConvertAll(c => c.Character).ToArray());
64
+
65
+ // 重置所有字符的颜色
66
+ foreach (var c in line.Characters)
67
+ {
68
+ c.IsComment = false;
69
+ c.IsKeyword = false;
70
+ c.IsString = false;
71
+ c.IsNumber = false;
72
+ c.Foreground = Brushes.Black;
73
+ }
74
+
75
+ // 处理多行注释
76
+ if (inMultiLineComment)
77
+ {
78
+ int endIndex = lineText.IndexOf(_commentEnd);
79
+ if (endIndex >= 0)
80
+ {
81
+ // 注释结束
82
+ for (int j = 0; j <= endIndex + _commentEnd.Length - 1 && j < line.Characters.Count; j++)
83
+ {
84
+ line.Characters[j].IsComment = true;
85
+ line.Characters[j].Foreground = Brushes.Green;
86
+ }
87
+ inMultiLineComment = false;
88
+
89
+ // 处理注释后的部分
90
+ if (endIndex + _commentEnd.Length < lineText.Length)
91
+ {
92
+ ColorizeLineSegment(line, endIndex + _commentEnd.Length, lineText.Length - (endIndex + _commentEnd.Length));
93
+ }
94
+ }
95
+ else
96
+ {
97
+ // 整行都是注释
98
+ for (int j = 0; j < line.Characters.Count; j++)
99
+ {
100
+ line.Characters[j].IsComment = true;
101
+ line.Characters[j].Foreground = Brushes.Green;
102
+ }
103
+ }
104
+ }
105
+ else
106
+ {
107
+ // 处理单行注释和多行注释开始
108
+ int lineCommentIndex = lineText.IndexOf(_lineComment);
109
+ int multiLineCommentIndex = lineText.IndexOf(_commentStart);
110
+
111
+ if (lineCommentIndex >= 0 && (multiLineCommentIndex < 0 || lineCommentIndex < multiLineCommentIndex))
112
+ {
113
+ // 单行注释优先
114
+ if (lineCommentIndex > 0)
115
+ {
116
+ ColorizeLineSegment(line, 0, lineCommentIndex);
117
+ }
118
+
119
+ // 注释部分
120
+ for (int j = lineCommentIndex; j < line.Characters.Count; j++)
121
+ {
122
+ line.Characters[j].IsComment = true;
123
+ line.Characters[j].Foreground = Brushes.Green;
124
+ }
125
+ }
126
+ else if (multiLineCommentIndex >= 0)
127
+ {
128
+ // 多行注释开始
129
+ if (multiLineCommentIndex > 0)
130
+ {
131
+ ColorizeLineSegment(line, 0, multiLineCommentIndex);
132
+ }
133
+
134
+ int endIndex = lineText.IndexOf(_commentEnd, multiLineCommentIndex + _commentStart.Length);
135
+ if (endIndex >= 0)
136
+ {
137
+ // 同一行内结束
138
+ for (int j = multiLineCommentIndex; j <= endIndex + _commentEnd.Length - 1 && j < line.Characters.Count; j++)
139
+ {
140
+ line.Characters[j].IsComment = true;
141
+ line.Characters[j].Foreground = Brushes.Green;
142
+ }
143
+
144
+ // 处理注释后的部分
145
+ if (endIndex + _commentEnd.Length < lineText.Length)
146
+ {
147
+ ColorizeLineSegment(line, endIndex + _commentEnd.Length, lineText.Length - (endIndex + _commentEnd.Length));
148
+ }
149
+ }
150
+ else
151
+ {
152
+ // 注释延续到下一行
153
+ for (int j = multiLineCommentIndex; j < line.Characters.Count; j++)
154
+ {
155
+ line.Characters[j].IsComment = true;
156
+ line.Characters[j].Foreground = Brushes.Green;
157
+ }
158
+ inMultiLineComment = true;
159
+ }
160
+ }
161
+ else
162
+ {
163
+ // 没有注释,正常处理整行
164
+ ColorizeLineSegment(line, 0, lineText.Length);
165
+ }
166
+ }
167
+ }
168
+ }
169
+
170
+ private void ColorizeLineSegment(TextLine line, int startIndex, int length)
171
+ {
172
+ if (length <= 0 || startIndex >= line.Characters.Count)
173
+ return;
174
+
175
+ string text = new string(line.Characters.GetRange(startIndex, Math.Min(length, line.Characters.Count - startIndex)).ConvertAll(c => c.Character).ToArray());
176
+
177
+ // 处理关键字
178
+ foreach (var keyword in _keywords.Keys)
179
+ {
180
+ int index = 0;
181
+ while ((index = text.IndexOf(keyword, index)) >= 0)
182
+ {
183
+ // 确保是完整的单词
184
+ bool isWordStart = index == 0 || !char.IsLetterOrDigit(text[index - 1]);
185
+ bool isWordEnd = index + keyword.Length == text.Length || !char.IsLetterOrDigit(text[index + keyword.Length]);
186
+
187
+ if (isWordStart && isWordEnd)
188
+ {
189
+ for (int i = 0; i < keyword.Length && i + index + startIndex < line.Characters.Count; i++)
190
+ {
191
+ line.Characters[i + index + startIndex].IsKeyword = true;
192
+ line.Characters[i + index + startIndex].Foreground = _keywords[keyword];
193
+ }
194
+ }
195
+ index += keyword.Length;
196
+ }
197
+ }
198
+
199
+ // 处理正则表达式模式
200
+ for (int i = 0; i < _patterns.Count; i++)
201
+ {
202
+ var regex = _patterns[i];
203
+ var matches = regex.Matches(text);
204
+
205
+ foreach (Match match in matches)
206
+ {
207
+ for (int j = 0; j < match.Length && j + match.Index + startIndex < line.Characters.Count; j++)
208
+ {
209
+ if (regex.ToString().Contains("\\d"))
210
+ {
211
+ line.Characters[j + match.Index + startIndex].IsNumber = true;
212
+ }
213
+ else if (regex.ToString().Contains("\\\""))
214
+ {
215
+ line.Characters[j + match.Index + startIndex].IsString = true;
216
+ }
217
+ line.Characters[j + match.Index + startIndex].Foreground = _patternColors[regex];
218
+ }
219
+ }
220
+ }
221
+ }
222
+ }
223
+ }
src/WpfEditor/TextEditorControl.cs ADDED
@@ -0,0 +1,1067 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System;
2
+ using System.Collections.Generic;
3
+ using System.Linq;
4
+ using System.Text;
5
+ using System.Windows;
6
+ using System.Windows.Controls;
7
+ using System.Windows.Input;
8
+ using System.Windows.Media;
9
+
10
+ namespace WpfEditor
11
+ {
12
+ public class TextEditorControl : Control
13
+ {
14
+ #region 依赖属性
15
+
16
+ public static readonly DependencyProperty TextProperty =
17
+ DependencyProperty.Register("Text", typeof(string), typeof(TextEditorControl),
18
+ new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnTextChanged));
19
+
20
+ public static readonly DependencyProperty IsReadOnlyProperty =
21
+ DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(TextEditorControl),
22
+ new PropertyMetadata(false));
23
+
24
+ public static readonly DependencyProperty TabSizeProperty =
25
+ DependencyProperty.Register("TabSize", typeof(int), typeof(TextEditorControl),
26
+ new PropertyMetadata(4));
27
+
28
+ public static readonly DependencyProperty LineNumbersVisibleProperty =
29
+ DependencyProperty.Register("LineNumbersVisible", typeof(bool), typeof(TextEditorControl),
30
+ new PropertyMetadata(true));
31
+
32
+ public static readonly DependencyProperty ShowWhitespacesProperty =
33
+ DependencyProperty.Register("ShowWhitespaces", typeof(bool), typeof(TextEditorControl),
34
+ new PropertyMetadata(true));
35
+
36
+ public static readonly DependencyProperty HighlightCurrentLineProperty =
37
+ DependencyProperty.Register("HighlightCurrentLine", typeof(bool), typeof(TextEditorControl),
38
+ new PropertyMetadata(true));
39
+
40
+ #endregion
41
+
42
+ #region 属性
43
+
44
+ public string Text
45
+ {
46
+ get { return (string)GetValue(TextProperty); }
47
+ set { SetValue(TextProperty, value); }
48
+ }
49
+
50
+ public bool IsReadOnly
51
+ {
52
+ get { return (bool)GetValue(IsReadOnlyProperty); }
53
+ set { SetValue(IsReadOnlyProperty, value); }
54
+ }
55
+
56
+ public int TabSize
57
+ {
58
+ get { return (int)GetValue(TabSizeProperty); }
59
+ set { SetValue(TabSizeProperty, value); }
60
+ }
61
+
62
+ public bool LineNumbersVisible
63
+ {
64
+ get { return (bool)GetValue(LineNumbersVisibleProperty); }
65
+ set { SetValue(LineNumbersVisibleProperty, value); }
66
+ }
67
+
68
+ public bool ShowWhitespaces
69
+ {
70
+ get { return (bool)GetValue(ShowWhitespacesProperty); }
71
+ set { SetValue(ShowWhitespacesProperty, value); }
72
+ }
73
+
74
+ public bool HighlightCurrentLine
75
+ {
76
+ get { return (bool)GetValue(HighlightCurrentLineProperty); }
77
+ set { SetValue(HighlightCurrentLineProperty, value); }
78
+ }
79
+
80
+ #endregion
81
+
82
+ #region 内部数据结构
83
+
84
+ private List<TextLine> _lines = new List<TextLine>();
85
+ private int _cursorLine = 0;
86
+ private int _cursorColumn = 0;
87
+ private int _selectionStartLine = -1;
88
+ private int _selectionStartColumn = -1;
89
+ private int _selectionEndLine = -1;
90
+ private int _selectionEndColumn = -1;
91
+ private List<UndoAction> _undoBuffer = new List<UndoAction>();
92
+ private int _undoIndex = -1;
93
+ private bool _textChanged = false;
94
+
95
+ #endregion
96
+
97
+ #region 构造函数
98
+
99
+ static TextEditorControl()
100
+ {
101
+ DefaultStyleKeyProperty.OverrideMetadata(typeof(TextEditorControl),
102
+ new FrameworkPropertyMetadata(typeof(TextEditorControl)));
103
+ }
104
+
105
+ public TextEditorControl()
106
+ {
107
+ this.Focusable = true;
108
+ this.FocusVisualStyle = null;
109
+ this.ClipToBounds = true;
110
+
111
+ // 初始化一个空行
112
+ _lines.Add(new TextLine());
113
+
114
+ // 注册命令绑定
115
+ CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy, OnCopy));
116
+ CommandBindings.Add(new CommandBinding(ApplicationCommands.Cut, OnCut));
117
+ CommandBindings.Add(new CommandBinding(ApplicationCommands.Paste, OnPaste));
118
+ CommandBindings.Add(new CommandBinding(ApplicationCommands.Undo, OnUndo));
119
+ CommandBindings.Add(new CommandBinding(ApplicationCommands.Redo, OnRedo));
120
+ CommandBindings.Add(new CommandBinding(ApplicationCommands.SelectAll, OnSelectAll));
121
+ }
122
+
123
+ #endregion
124
+
125
+ #region 事件处理
126
+
127
+ private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
128
+ {
129
+ var control = (TextEditorControl)d;
130
+ control.SetText((string)e.NewValue);
131
+ }
132
+
133
+ protected override void OnRender(DrawingContext drawingContext)
134
+ {
135
+ base.OnRender(drawingContext);
136
+ RenderEditor(drawingContext);
137
+ }
138
+
139
+ protected override void OnTextInput(TextCompositionEventArgs e)
140
+ {
141
+ base.OnTextInput(e);
142
+ if (!IsReadOnly)
143
+ {
144
+ InsertText(e.Text);
145
+ e.Handled = true;
146
+ }
147
+ }
148
+
149
+ protected override void OnKeyDown(KeyEventArgs e)
150
+ {
151
+ base.OnKeyDown(e);
152
+ HandleKeyDown(e);
153
+ }
154
+
155
+ protected override void OnMouseDown(MouseButtonEventArgs e)
156
+ {
157
+ base.OnMouseDown(e);
158
+ Focus();
159
+ Point position = e.GetPosition(this);
160
+ SetCursorPositionFromPoint(position);
161
+ if (Keyboard.Modifiers != ModifierKeys.Shift)
162
+ {
163
+ ClearSelection();
164
+ }
165
+ else
166
+ {
167
+ UpdateSelection();
168
+ }
169
+ e.Handled = true;
170
+ InvalidateVisual();
171
+ }
172
+
173
+ protected override void OnMouseMove(MouseEventArgs e)
174
+ {
175
+ base.OnMouseMove(e);
176
+ if (e.LeftButton == MouseButtonState.Pressed)
177
+ {
178
+ Point position = e.GetPosition(this);
179
+ SetCursorPositionFromPoint(position);
180
+ UpdateSelection();
181
+ InvalidateVisual();
182
+ }
183
+ }
184
+
185
+ #endregion
186
+
187
+ #region 命令处理
188
+
189
+ private void OnCopy(object sender, ExecutedRoutedEventArgs e)
190
+ {
191
+ if (HasSelection())
192
+ {
193
+ Clipboard.SetText(GetSelectedText());
194
+ }
195
+ e.Handled = true;
196
+ }
197
+
198
+ private void OnCut(object sender, ExecutedRoutedEventArgs e)
199
+ {
200
+ if (!IsReadOnly && HasSelection())
201
+ {
202
+ Clipboard.SetText(GetSelectedText());
203
+ DeleteSelection();
204
+ InvalidateVisual();
205
+ }
206
+ e.Handled = true;
207
+ }
208
+
209
+ private void OnPaste(object sender, ExecutedRoutedEventArgs e)
210
+ {
211
+ if (!IsReadOnly && Clipboard.ContainsText())
212
+ {
213
+ if (HasSelection())
214
+ {
215
+ DeleteSelection();
216
+ }
217
+ InsertText(Clipboard.GetText());
218
+ InvalidateVisual();
219
+ }
220
+ e.Handled = true;
221
+ }
222
+
223
+ private void OnUndo(object sender, ExecutedRoutedEventArgs e)
224
+ {
225
+ if (!IsReadOnly && CanUndo())
226
+ {
227
+ Undo();
228
+ InvalidateVisual();
229
+ }
230
+ e.Handled = true;
231
+ }
232
+
233
+ private void OnRedo(object sender, ExecutedRoutedEventArgs e)
234
+ {
235
+ if (!IsReadOnly && CanRedo())
236
+ {
237
+ Redo();
238
+ InvalidateVisual();
239
+ }
240
+ e.Handled = true;
241
+ }
242
+
243
+ private void OnSelectAll(object sender, ExecutedRoutedEventArgs e)
244
+ {
245
+ SelectAll();
246
+ InvalidateVisual();
247
+ e.Handled = true;
248
+ }
249
+
250
+ #endregion
251
+
252
+ #region 辅助方法
253
+
254
+ private void SetText(string text)
255
+ {
256
+ _lines.Clear();
257
+ _undoBuffer.Clear();
258
+ _undoIndex = -1;
259
+
260
+ if (string.IsNullOrEmpty(text))
261
+ {
262
+ _lines.Add(new TextLine());
263
+ }
264
+ else
265
+ {
266
+ var lines = text.Replace("\r\n", "\n").Replace("\r", "\n").Split('\n');
267
+ foreach (var line in lines)
268
+ {
269
+ var textLine = new TextLine();
270
+ foreach (var c in line)
271
+ {
272
+ textLine.Characters.Add(new TextCharacter(c));
273
+ }
274
+ _lines.Add(textLine);
275
+ }
276
+ }
277
+
278
+ _cursorLine = 0;
279
+ _cursorColumn = 0;
280
+ ClearSelection();
281
+ _textChanged = true;
282
+ InvalidateVisual();
283
+
284
+ // 触发文本改变事件
285
+ OnTextChanged();
286
+ }
287
+
288
+ private void RenderEditor(DrawingContext drawingContext)
289
+ {
290
+ // 绘制背景
291
+ drawingContext.DrawRectangle(Brushes.White, null, new Rect(0, 0, ActualWidth, ActualHeight));
292
+
293
+ // 计算字体度量
294
+ var typeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);
295
+ double lineHeight = FontSize * 1.2;
296
+ double defaultCharWidth = FontSize * 0.6; // 默认字符宽度(英文)
297
+
298
+ // 计算行号区域宽度
299
+ double lineNumberWidth = LineNumbersVisible ? 40 : 0;
300
+
301
+ // 绘制行号区域背景
302
+ if (LineNumbersVisible)
303
+ {
304
+ drawingContext.DrawRectangle(Brushes.LightGray, null, new Rect(0, 0, lineNumberWidth, ActualHeight));
305
+ }
306
+
307
+ double textStartX = lineNumberWidth + 5; // 文本起始X坐标
308
+
309
+ // 高亮当前行
310
+ if (HighlightCurrentLine && _cursorLine < _lines.Count)
311
+ {
312
+ drawingContext.DrawRectangle(Brushes.LightYellow, null,
313
+ new Rect(lineNumberWidth, _cursorLine * lineHeight, ActualWidth - lineNumberWidth, lineHeight));
314
+ }
315
+
316
+ // 绘制文本和行号
317
+ for (int i = 0; i < _lines.Count; i++)
318
+ {
319
+ double y = i * lineHeight;
320
+
321
+ // 绘制行号
322
+ if (LineNumbersVisible)
323
+ {
324
+ var lineNumberText = new FormattedText(
325
+ (i + 1).ToString(),
326
+ System.Globalization.CultureInfo.CurrentCulture,
327
+ FlowDirection.LeftToRight,
328
+ typeface,
329
+ FontSize,
330
+ Brushes.Gray,
331
+ VisualTreeHelper.GetDpi(this).PixelsPerDip);
332
+
333
+ drawingContext.DrawText(lineNumberText, new Point(5, y));
334
+ }
335
+
336
+ // 绘制文本
337
+ var line = _lines[i];
338
+ double x = textStartX;
339
+
340
+ for (int j = 0; j < line.Characters.Count; j++)
341
+ {
342
+ var character = line.Characters[j];
343
+
344
+ // 绘制选中区域背景
345
+ double charWidth = IsFullWidthCharacter(character.Character) ? defaultCharWidth * 2 : defaultCharWidth;
346
+
347
+ if (HasSelection() && IsPositionSelected(i, j))
348
+ {
349
+ drawingContext.DrawRectangle(Brushes.LightBlue, null, new Rect(x, y, charWidth, lineHeight));
350
+ }
351
+
352
+ // 绘制字符
353
+ var formattedText = new FormattedText(
354
+ character.Character.ToString(),
355
+ System.Globalization.CultureInfo.CurrentCulture,
356
+ FlowDirection.LeftToRight,
357
+ typeface,
358
+ FontSize,
359
+ character.Foreground,
360
+ VisualTreeHelper.GetDpi(this).PixelsPerDip);
361
+
362
+ drawingContext.DrawText(formattedText, new Point(x, y));
363
+
364
+ // 显示空白字符
365
+ if (ShowWhitespaces && character.Character == ' ')
366
+ {
367
+ drawingContext.DrawLine(
368
+ new Pen(Brushes.LightGray, 1),
369
+ new Point(x + charWidth / 2, y + lineHeight / 2),
370
+ new Point(x + charWidth / 2, y + lineHeight / 2));
371
+ }
372
+
373
+ x += charWidth;
374
+ }
375
+ }
376
+
377
+ // 绘制光标
378
+ if (IsFocused && _cursorLine < _lines.Count)
379
+ {
380
+ // 计算光标X坐标,考虑前面字符的宽度
381
+ double cursorX = textStartX;
382
+ var line = _lines[_cursorLine];
383
+ for (int j = 0; j < _cursorColumn && j < line.Characters.Count; j++)
384
+ {
385
+ cursorX += IsFullWidthCharacter(line.Characters[j].Character) ? defaultCharWidth * 2 : defaultCharWidth;
386
+ }
387
+
388
+ double cursorY = _cursorLine * lineHeight;
389
+
390
+ drawingContext.DrawLine(
391
+ new Pen(Brushes.Black, 1),
392
+ new Point(cursorX, cursorY),
393
+ new Point(cursorX, cursorY + lineHeight));
394
+ }
395
+ }
396
+
397
+ // 判断是否是全角字符(如中文、日文等)
398
+ private bool IsFullWidthCharacter(char c)
399
+ {
400
+ // 中文字符范围
401
+ if (c >= 0x4E00 && c <= 0x9FFF) return true; // CJK统一汉字
402
+ if (c >= 0x3400 && c <= 0x4DBF) return true; // CJK扩展A
403
+ if (c >= 0x20000 && c <= 0x2A6DF) return true; // CJK扩展B
404
+ if (c >= 0x2A700 && c <= 0x2B73F) return true; // CJK扩展C
405
+ if (c >= 0x2B740 && c <= 0x2B81F) return true; // CJK扩展D
406
+ if (c >= 0x2B820 && c <= 0x2CEAF) return true; // CJK扩展E
407
+ if (c >= 0xF900 && c <= 0xFAFF) return true; // CJK兼容汉字
408
+
409
+ // 日文假名等
410
+ if (c >= 0x3040 && c <= 0x309F) return true; // 平假名
411
+ if (c >= 0x30A0 && c <= 0x30FF) return true; // 片假名
412
+
413
+ // 全角标点和符号
414
+ if (c >= 0xFF00 && c <= 0xFFEF) return true; // 全角ASCII、全角标点
415
+
416
+ // 其他全角字符
417
+ if (c >= 0x3000 && c <= 0x303F) return true; // CJK符号和标点
418
+
419
+ return false;
420
+ }
421
+
422
+ private bool IsPositionSelected(int line, int column)
423
+ {
424
+ if (!HasSelection())
425
+ return false;
426
+
427
+ int startLine = Math.Min(_selectionStartLine, _selectionEndLine);
428
+ int startColumn = startLine == _selectionStartLine ? _selectionStartColumn : _selectionEndColumn;
429
+ int endLine = Math.Max(_selectionStartLine, _selectionEndLine);
430
+ int endColumn = endLine == _selectionEndLine ? _selectionEndColumn : _selectionStartColumn;
431
+
432
+ if (line < startLine || line > endLine)
433
+ return false;
434
+
435
+ if (line == startLine && line == endLine)
436
+ return column >= startColumn && column < endColumn;
437
+
438
+ if (line == startLine)
439
+ return column >= startColumn;
440
+
441
+ if (line == endLine)
442
+ return column < endColumn;
443
+
444
+ return true;
445
+ }
446
+ private void SetCursorPositionFromPoint(Point point)
447
+ {
448
+ // 计算字体度量
449
+ double lineHeight = FontSize * 1.2;
450
+ double defaultCharWidth = FontSize * 0.6; // 默认字符宽度
451
+
452
+ // 计算行号区域宽度
453
+ double lineNumberWidth = LineNumbersVisible ? 40 : 0;
454
+ double textStartX = lineNumberWidth + 5; // 文本起始X坐标
455
+
456
+ // 计算行号
457
+ int line = (int)(point.Y / lineHeight);
458
+ line = Math.Max(0, Math.Min(line, _lines.Count - 1));
459
+
460
+ // 计算列号,考虑字符宽度
461
+ double x = textStartX;
462
+ int column = 0;
463
+ var textLine = _lines[line];
464
+
465
+ // 找到光标应该在的位置
466
+ while (column < textLine.Characters.Count && x < point.X)
467
+ {
468
+ x += IsFullWidthCharacter(textLine.Characters[column].Character) ? defaultCharWidth * 2 : defaultCharWidth;
469
+ if (x > point.X)
470
+ {
471
+ // 如果点击位置在字符的前半部分,光标放在字符前面
472
+ // 如果在后半部分,光标放在字符后面
473
+ double charWidth = IsFullWidthCharacter(textLine.Characters[column].Character) ? defaultCharWidth * 2 : defaultCharWidth;
474
+ if (point.X > x - charWidth / 2)
475
+ column++;
476
+ break;
477
+ }
478
+ column++;
479
+ }
480
+
481
+ // 设置光标位置
482
+ _cursorLine = line;
483
+ _cursorColumn = column;
484
+
485
+ // 触发光标位置改变事件
486
+ OnCursorPositionChanged(_cursorLine, _cursorColumn);
487
+ }
488
+ private void HandleKeyDown(KeyEventArgs e)
489
+ {
490
+ if (IsReadOnly)
491
+ {
492
+ e.Handled = true;
493
+ return;
494
+ }
495
+
496
+ switch (e.Key)
497
+ {
498
+ case Key.Left:
499
+ if (_cursorColumn > 0)
500
+ {
501
+ _cursorColumn--;
502
+ }
503
+ else if (_cursorLine > 0)
504
+ {
505
+ _cursorLine--;
506
+ _cursorColumn = _lines[_cursorLine].Characters.Count;
507
+ }
508
+ break;
509
+
510
+ case Key.Right:
511
+ if (_cursorColumn < _lines[_cursorLine].Characters.Count)
512
+ {
513
+ _cursorColumn++;
514
+ }
515
+ else if (_cursorLine < _lines.Count - 1)
516
+ {
517
+ _cursorLine++;
518
+ _cursorColumn = 0;
519
+ }
520
+ break;
521
+
522
+ case Key.Up:
523
+ if (_cursorLine > 0)
524
+ {
525
+ _cursorLine--;
526
+ _cursorColumn = Math.Min(_cursorColumn, _lines[_cursorLine].Characters.Count);
527
+ }
528
+ break;
529
+
530
+ case Key.Down:
531
+ if (_cursorLine < _lines.Count - 1)
532
+ {
533
+ _cursorLine++;
534
+ _cursorColumn = Math.Min(_cursorColumn, _lines[_cursorLine].Characters.Count);
535
+ }
536
+ break;
537
+
538
+ case Key.Home:
539
+ _cursorColumn = 0;
540
+ break;
541
+
542
+ case Key.End:
543
+ _cursorColumn = _lines[_cursorLine].Characters.Count;
544
+ break;
545
+
546
+ case Key.Delete:
547
+ if (HasSelection())
548
+ {
549
+ DeleteSelection();
550
+ }
551
+ else if (_cursorColumn < _lines[_cursorLine].Characters.Count)
552
+ {
553
+ // 删除当前字符
554
+ _lines[_cursorLine].Characters.RemoveAt(_cursorColumn);
555
+ _textChanged = true;
556
+ OnTextChanged();
557
+ }
558
+ else if (_cursorLine < _lines.Count - 1)
559
+ {
560
+ // 合并当前行和下一行
561
+ var nextLine = _lines[_cursorLine + 1];
562
+ _lines[_cursorLine].Characters.AddRange(nextLine.Characters);
563
+ _lines.RemoveAt(_cursorLine + 1);
564
+ _textChanged = true;
565
+ OnTextChanged();
566
+ }
567
+ break;
568
+
569
+ case Key.Back:
570
+ if (HasSelection())
571
+ {
572
+ DeleteSelection();
573
+ }
574
+ else if (_cursorColumn > 0)
575
+ {
576
+ // 删除前一个字符
577
+ _cursorColumn--;
578
+ _lines[_cursorLine].Characters.RemoveAt(_cursorColumn);
579
+ _textChanged = true;
580
+ OnTextChanged();
581
+ }
582
+ else if (_cursorLine > 0)
583
+ {
584
+ // 合并当前行和上一行
585
+ _cursorColumn = _lines[_cursorLine - 1].Characters.Count;
586
+ _lines[_cursorLine - 1].Characters.AddRange(_lines[_cursorLine].Characters);
587
+ _lines.RemoveAt(_cursorLine);
588
+ _cursorLine--;
589
+ _textChanged = true;
590
+ OnTextChanged();
591
+ }
592
+ break;
593
+
594
+ case Key.Enter:
595
+ // 插入新行
596
+ var currentLine = _lines[_cursorLine];
597
+ var newLine = new TextLine();
598
+
599
+ // 将当前行光标后的字符移到新行
600
+ if (_cursorColumn < currentLine.Characters.Count)
601
+ {
602
+ var remainingChars = currentLine.Characters.GetRange(_cursorColumn, currentLine.Characters.Count - _cursorColumn);
603
+ newLine.Characters.AddRange(remainingChars);
604
+ currentLine.Characters.RemoveRange(_cursorColumn, remainingChars.Count);
605
+ }
606
+
607
+ _lines.Insert(_cursorLine + 1, newLine);
608
+ _cursorLine++;
609
+ _cursorColumn = 0;
610
+ _textChanged = true;
611
+ OnTextChanged();
612
+ break;
613
+
614
+ case Key.Tab:
615
+ // 插入Tab(空格)
616
+ for (int i = 0; i < TabSize; i++)
617
+ {
618
+ InsertText(" ");
619
+ }
620
+ break;
621
+
622
+ default:
623
+ e.Handled = false;
624
+ return;
625
+ }
626
+
627
+ // 如果按下Shift键,更新选择区域
628
+ if (Keyboard.Modifiers == ModifierKeys.Shift)
629
+ {
630
+ UpdateSelection();
631
+ }
632
+ else
633
+ {
634
+ ClearSelection();
635
+ }
636
+
637
+ // 触发光标位置改变事件
638
+ OnCursorPositionChanged(_cursorLine, _cursorColumn);
639
+
640
+ // 重绘编辑器
641
+ InvalidateVisual();
642
+
643
+ e.Handled = true;
644
+ }
645
+
646
+ private void DeleteSelection()
647
+ {
648
+ if (!HasSelection())
649
+ return;
650
+
651
+ // 确保起始位置在结束位置之前
652
+ int startLine = Math.Min(_selectionStartLine, _selectionEndLine);
653
+ int startColumn = startLine == _selectionStartLine ? _selectionStartColumn : _selectionEndColumn;
654
+ int endLine = Math.Max(_selectionStartLine, _selectionEndLine);
655
+ int endColumn = endLine == _selectionEndLine ? _selectionEndColumn : _selectionStartColumn;
656
+
657
+ if (startLine == endLine)
658
+ {
659
+ // 单行删除
660
+ int start = Math.Min(startColumn, endColumn);
661
+ int end = Math.Max(startColumn, endColumn);
662
+ var line = _lines[startLine];
663
+ if (start < line.Characters.Count)
664
+ {
665
+ int length = Math.Min(end - start, line.Characters.Count - start);
666
+ line.Characters.RemoveRange(start, length);
667
+ }
668
+ }
669
+ else
670
+ {
671
+ // 多行删除
672
+ // 保留第一行开始部分
673
+ var firstLine = _lines[startLine];
674
+ if (startColumn < firstLine.Characters.Count)
675
+ {
676
+ firstLine.Characters.RemoveRange(startColumn, firstLine.Characters.Count - startColumn);
677
+ }
678
+
679
+ // 保留最后一行结束部分
680
+ var lastLine = _lines[endLine];
681
+ List<TextCharacter> lastLineRemaining = new List<TextCharacter>();
682
+ if (endColumn < lastLine.Characters.Count)
683
+ {
684
+ lastLineRemaining = lastLine.Characters.GetRange(endColumn, lastLine.Characters.Count - endColumn);
685
+ }
686
+
687
+ // 删除中间行
688
+ _lines.RemoveRange(startLine + 1, endLine - startLine);
689
+
690
+ // 将最后一行剩余部分添加到第一行
691
+ firstLine.Characters.AddRange(lastLineRemaining);
692
+ }
693
+
694
+ // 设置光标位置到选择的开始位置
695
+ _cursorLine = startLine;
696
+ _cursorColumn = startColumn;
697
+
698
+ // 清除选择
699
+ ClearSelection();
700
+
701
+ // 标记文本已更改
702
+ _textChanged = true;
703
+
704
+ // 触发文本改变事件
705
+ OnTextChanged();
706
+
707
+ // 触发光标位置改变事件
708
+ OnCursorPositionChanged(_cursorLine, _cursorColumn);
709
+ }
710
+
711
+ private string GetSelectedText()
712
+ {
713
+ if (!HasSelection())
714
+ return string.Empty;
715
+
716
+ // 确保起始位置在结束位置之前
717
+ int startLine = Math.Min(_selectionStartLine, _selectionEndLine);
718
+ int startColumn = startLine == _selectionStartLine ? _selectionStartColumn : _selectionEndColumn;
719
+ int endLine = Math.Max(_selectionStartLine, _selectionEndLine);
720
+ int endColumn = endLine == _selectionEndLine ? _selectionEndColumn : _selectionStartColumn;
721
+
722
+ if (startLine == endLine)
723
+ {
724
+ // 单行选择
725
+ int start = Math.Min(startColumn, endColumn);
726
+ int end = Math.Max(startColumn, endColumn);
727
+ var line = _lines[startLine];
728
+ if (start < line.Characters.Count)
729
+ {
730
+ int length = Math.Min(end - start, line.Characters.Count - start);
731
+ return new string(line.Characters.GetRange(start, length).ConvertAll(c => c.Character).ToArray());
732
+ }
733
+ return string.Empty;
734
+ }
735
+ else
736
+ {
737
+ // 多行选择
738
+ StringBuilder sb = new StringBuilder();
739
+
740
+ // 第一行
741
+ var firstLine = _lines[startLine];
742
+ if (startColumn < firstLine.Characters.Count)
743
+ {
744
+ sb.Append(new string(firstLine.Characters.GetRange(startColumn, firstLine.Characters.Count - startColumn).ConvertAll(c => c.Character).ToArray()));
745
+ }
746
+ sb.Append("\n");
747
+
748
+ // 中间行
749
+ for (int i = startLine + 1; i < endLine; i++)
750
+ {
751
+ sb.Append(new string(_lines[i].Characters.ConvertAll(c => c.Character).ToArray()));
752
+ sb.Append("\n");
753
+ }
754
+
755
+ // 最后一行
756
+ var lastLine = _lines[endLine];
757
+ if (endColumn > 0 && lastLine.Characters.Count > 0)
758
+ {
759
+ sb.Append(new string(lastLine.Characters.GetRange(0, Math.Min(endColumn, lastLine.Characters.Count)).ConvertAll(c => c.Character).ToArray()));
760
+ }
761
+
762
+ return sb.ToString();
763
+ }
764
+ }
765
+
766
+ private bool HasSelection()
767
+ {
768
+ // 判断是否有选中的文本
769
+ return _selectionStartLine >= 0 && _selectionEndLine >= 0;
770
+ }
771
+
772
+ private void ClearSelection()
773
+ {
774
+ // 清除选择
775
+ _selectionStartLine = -1;
776
+ _selectionStartColumn = -1;
777
+ _selectionEndLine = -1;
778
+ _selectionEndColumn = -1;
779
+ }
780
+
781
+ private void UpdateSelection()
782
+ {
783
+ // 更新选择区域
784
+ if (_selectionStartLine < 0)
785
+ {
786
+ _selectionStartLine = _cursorLine;
787
+ _selectionStartColumn = _cursorColumn;
788
+ }
789
+ _selectionEndLine = _cursorLine;
790
+ _selectionEndColumn = _cursorColumn;
791
+ }
792
+
793
+ private void SelectAll()
794
+ {
795
+ // 全选
796
+ _selectionStartLine = 0;
797
+ _selectionStartColumn = 0;
798
+ _selectionEndLine = _lines.Count - 1;
799
+ _selectionEndColumn = _lines[_selectionEndLine].Characters.Count;
800
+ }
801
+
802
+ private bool CanUndo()
803
+ {
804
+ // 判断是否可以撤销
805
+ return _undoIndex >= 0;
806
+ }
807
+
808
+ private bool CanRedo()
809
+ {
810
+ // 判断是否可以重做
811
+ return _undoIndex < _undoBuffer.Count - 1;
812
+ }
813
+
814
+ private void Undo()
815
+ {
816
+ if (!CanUndo())
817
+ return;
818
+
819
+ var action = _undoBuffer[_undoIndex];
820
+
821
+ // 恢复光标位置到操作前
822
+ _cursorLine = action.CursorBeforeLine;
823
+ _cursorColumn = action.CursorBeforeColumn;
824
+
825
+ // 如果有添加的文本,需要删除它
826
+ if (!string.IsNullOrEmpty(action.AddedText))
827
+ {
828
+ // 设置选择区域为添加的文本
829
+ _selectionStartLine = action.AddedStartLine;
830
+ _selectionStartColumn = action.AddedStartColumn;
831
+ _selectionEndLine = action.AddedEndLine;
832
+ _selectionEndColumn = action.AddedEndColumn;
833
+
834
+ // 删除添加的文本
835
+ DeleteSelection();
836
+ }
837
+
838
+ // 如果有删除的文本,需要恢复它
839
+ if (!string.IsNullOrEmpty(action.RemovedText))
840
+ {
841
+ // 设置光标位置到删除的起始位置
842
+ _cursorLine = action.RemovedStartLine;
843
+ _cursorColumn = action.RemovedStartColumn;
844
+
845
+ // 插入被删除的文本
846
+ InsertText(action.RemovedText);
847
+
848
+ // 恢复光标位置
849
+ _cursorLine = action.CursorBeforeLine;
850
+ _cursorColumn = action.CursorBeforeColumn;
851
+ }
852
+
853
+ // 清除选择
854
+ ClearSelection();
855
+
856
+ // 更新撤销索引
857
+ _undoIndex--;
858
+
859
+ // 触发光标位置改变事件
860
+ OnCursorPositionChanged(_cursorLine, _cursorColumn);
861
+ }
862
+
863
+ private void Redo()
864
+ {
865
+ if (!CanRedo())
866
+ return;
867
+
868
+ // 更新撤销索引
869
+ _undoIndex++;
870
+
871
+ var action = _undoBuffer[_undoIndex];
872
+
873
+ // 恢复光标位置到操作前
874
+ _cursorLine = action.CursorBeforeLine;
875
+ _cursorColumn = action.CursorBeforeColumn;
876
+
877
+ // 如果有删除的文本,需要再次删除它
878
+ if (!string.IsNullOrEmpty(action.RemovedText))
879
+ {
880
+ // 设置选择区域为删除的文本
881
+ _selectionStartLine = action.RemovedStartLine;
882
+ _selectionStartColumn = action.RemovedStartColumn;
883
+ _selectionEndLine = action.RemovedEndLine;
884
+ _selectionEndColumn = action.RemovedEndColumn;
885
+
886
+ // 删除文本
887
+ DeleteSelection();
888
+ }
889
+
890
+ // 如果有添加的文本,需要再次添加它
891
+ if (!string.IsNullOrEmpty(action.AddedText))
892
+ {
893
+ // 设置光标位置到添加的起始位置
894
+ _cursorLine = action.AddedStartLine;
895
+ _cursorColumn = action.AddedStartColumn;
896
+
897
+ // 插入文本
898
+ InsertText(action.AddedText);
899
+ }
900
+
901
+ // 恢复光标位置到操作后
902
+ _cursorLine = action.CursorAfterLine;
903
+ _cursorColumn = action.CursorAfterColumn;
904
+
905
+ // 清除选择
906
+ ClearSelection();
907
+
908
+ // 触发光标位置改变事件
909
+ OnCursorPositionChanged(_cursorLine, _cursorColumn);
910
+ }
911
+
912
+ #endregion
913
+
914
+ public event EventHandler TextChanged;
915
+ public event EventHandler<CursorPositionChangedEventArgs> CursorPositionChanged;
916
+
917
+ protected virtual void OnTextChanged()
918
+ {
919
+ TextChanged?.Invoke(this, EventArgs.Empty);
920
+ }
921
+
922
+ protected virtual void OnCursorPositionChanged(int line, int column)
923
+ {
924
+ CursorPositionChanged?.Invoke(this, new CursorPositionChangedEventArgs(line, column));
925
+ }
926
+
927
+ private void InsertText(string text)
928
+ {
929
+ if (string.IsNullOrEmpty(text))
930
+ return;
931
+
932
+ // 如果有选中的文本,先删除它
933
+ if (HasSelection())
934
+ {
935
+ DeleteSelection();
936
+ }
937
+
938
+ // 记录撤销操作
939
+ var undoAction = new UndoAction
940
+ {
941
+ CursorBeforeLine = _cursorLine,
942
+ CursorBeforeColumn = _cursorColumn
943
+ };
944
+
945
+ // 处理多行文本
946
+ var lines = text.Replace("\r\n", "\n").Replace("\r", "\n").Split('\n');
947
+
948
+ if (lines.Length == 1)
949
+ {
950
+ // 单行文本
951
+ var line = _lines[_cursorLine];
952
+ var chars = lines[0].ToCharArray().Select(c => new TextCharacter(c)).ToList();
953
+ line.Characters.InsertRange(_cursorColumn, chars);
954
+ _cursorColumn += chars.Count;
955
+
956
+ // 记录添加的文本信息
957
+ undoAction.AddedText = text;
958
+ undoAction.AddedStartLine = _cursorLine;
959
+ undoAction.AddedStartColumn = _cursorColumn - chars.Count;
960
+ undoAction.AddedEndLine = _cursorLine;
961
+ undoAction.AddedEndColumn = _cursorColumn;
962
+ }
963
+ else
964
+ {
965
+ // 多行文本
966
+ var currentLine = _lines[_cursorLine];
967
+ var remainingChars = currentLine.Characters.GetRange(_cursorColumn, currentLine.Characters.Count - _cursorColumn).ToList();
968
+ currentLine.Characters.RemoveRange(_cursorColumn, remainingChars.Count);
969
+
970
+ // 添加第一行
971
+ var firstLineChars = lines[0].ToCharArray().Select(c => new TextCharacter(c)).ToList();
972
+ currentLine.Characters.AddRange(firstLineChars);
973
+
974
+ // 添加中间行
975
+ for (int i = 1; i < lines.Length - 1; i++)
976
+ {
977
+ var newLine = new TextLine();
978
+ newLine.Characters.AddRange(lines[i].ToCharArray().Select(c => new TextCharacter(c)));
979
+ _lines.Insert(_cursorLine + i, newLine);
980
+ }
981
+
982
+ // 添加最后一行和剩余字符
983
+ var lastLine = new TextLine();
984
+ lastLine.Characters.AddRange(lines[lines.Length - 1].ToCharArray().Select(c => new TextCharacter(c)));
985
+ lastLine.Characters.AddRange(remainingChars);
986
+ _lines.Insert(_cursorLine + lines.Length - 1, lastLine);
987
+
988
+ // 更新光标位置
989
+ _cursorLine += lines.Length - 1;
990
+ _cursorColumn = lastLine.Characters.Count - remainingChars.Count;
991
+
992
+ // 记录添加的文本信息
993
+ undoAction.AddedText = text;
994
+ undoAction.AddedStartLine = _cursorLine - (lines.Length - 1);
995
+ undoAction.AddedStartColumn = _cursorColumn - firstLineChars.Count;
996
+ undoAction.AddedEndLine = _cursorLine;
997
+ undoAction.AddedEndColumn = _cursorColumn;
998
+ }
999
+
1000
+ // 记录撤销操作后的光标位置
1001
+ undoAction.CursorAfterLine = _cursorLine;
1002
+ undoAction.CursorAfterColumn = _cursorColumn;
1003
+
1004
+ // 添加到撤销缓冲区
1005
+ if (_undoIndex < _undoBuffer.Count - 1)
1006
+ {
1007
+ _undoBuffer.RemoveRange(_undoIndex + 1, _undoBuffer.Count - _undoIndex - 1);
1008
+ }
1009
+ _undoBuffer.Add(undoAction);
1010
+ _undoIndex = _undoBuffer.Count - 1;
1011
+
1012
+ // 标记文本已更改
1013
+ _textChanged = true;
1014
+
1015
+ // 触发文本改变事件
1016
+ OnTextChanged();
1017
+
1018
+ // 触发光标位置改变事件
1019
+ OnCursorPositionChanged(_cursorLine, _cursorColumn);
1020
+
1021
+ // 重绘编辑器
1022
+ InvalidateVisual();
1023
+ }
1024
+
1025
+ }
1026
+
1027
+ public class TextCharacter
1028
+ {
1029
+ public char Character { get; set; }
1030
+ public Brush Foreground { get; set; } = Brushes.Black;
1031
+ public bool IsComment { get; set; } = false;
1032
+ public bool IsKeyword { get; set; } = false;
1033
+ public bool IsString { get; set; } = false;
1034
+ public bool IsNumber { get; set; } = false;
1035
+
1036
+ public TextCharacter(char c)
1037
+ {
1038
+ Character = c;
1039
+ }
1040
+ }
1041
+
1042
+ public class TextLine
1043
+ {
1044
+ public List<TextCharacter> Characters { get; set; } = new List<TextCharacter>();
1045
+ }
1046
+
1047
+ public class UndoAction
1048
+ {
1049
+ public string AddedText { get; set; } = string.Empty;
1050
+ public int AddedStartLine { get; set; }
1051
+ public int AddedStartColumn { get; set; }
1052
+ public int AddedEndLine { get; set; }
1053
+ public int AddedEndColumn { get; set; }
1054
+
1055
+ public string RemovedText { get; set; } = string.Empty;
1056
+ public int RemovedStartLine { get; set; }
1057
+ public int RemovedStartColumn { get; set; }
1058
+ public int RemovedEndLine { get; set; }
1059
+ public int RemovedEndColumn { get; set; }
1060
+
1061
+ public int CursorBeforeLine { get; set; }
1062
+ public int CursorBeforeColumn { get; set; }
1063
+ public int CursorAfterLine { get; set; }
1064
+ public int CursorAfterColumn { get; set; }
1065
+ }
1066
+
1067
+ }
src/WpfEditor/WpfEditor.csproj ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <Project Sdk="Microsoft.NET.Sdk">
2
+
3
+ <PropertyGroup>
4
+ <OutputType>WinExe</OutputType>
5
+ <TargetFramework>net8.0-windows</TargetFramework>
6
+ <Nullable>enable</Nullable>
7
+ <ImplicitUsings>enable</ImplicitUsings>
8
+ <UseWPF>true</UseWPF>
9
+ </PropertyGroup>
10
+
11
+ </Project>
src/app.aps ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:be64f6603779de4a4b6f359fab070671b55bd9cff475f682a03bc1f7b402163a
3
+ size 189560