文本编辑器设计
Posted 一身千寻瀑
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文本编辑器设计相关的知识,希望对你有一定的参考价值。
1. 开发环境
开发语言:C#
开发平台:Visual Studio2015
2. 程序的功能
本程序实现了一种简易文本编辑器,其具有如下功能:
1) 具有简洁明确的图形界面;
2) 文本基本编辑功如插入、删除等,文本内容的查找、替换等功能;
3) 正确保存文本到指定位置;
4) 编辑器可显示文本总行数;
5) 打开指定路径下的文本文件;
6) 文本可设置是否自动换行。
3. 代码清单
界面设计
MainWindow.xaml //文本编辑窗口
<Window x:Class="MyTextEditor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyTextEditor"
mc:Ignorable="d"
Title="文本编辑器"Height="356" Width="531">
<Grid>
<DockPanel Margin="0,0,0,27">
<ToolBarTray DockPanel.Dock="top" Background="White">
<ToolBar Margin="0,0,-517,0" Background="White">
<Menu DockPanel.Dock="Top" HorizontalAlignment="Left" Background="White" BorderBrush="Black">
<MenuItem Header="文件">
<MenuItem Command="Open"/>
<MenuItem Header="保存" Command="New"/>
<Separator/>
<MenuItem Header="退出" Click="FileExit_Click"/>
</MenuItem>
<MenuItem Header="工具">
<MenuItem Header="查找替换" InputGestureText="Ctrl+F" Click="findAndReplace"/>
<MenuItem Name="menuAutoWrap" Header="自动换行" IsChecked="True" Click="menuAutoWrap_Click"/>
</MenuItem>
<MenuItem Header="编辑">
<MenuItemCommand="Cut"/>
<MenuItem Command="Copy"/>
<MenuItem Command="Paste"/>
<MenuItem Command="Delete"/>
<MenuItem Command="SelectAll"/>
</MenuItem>
</Menu>
</ToolBar>
</ToolBarTray>
<TextBox x:Name="textBox1" Height="265" ScrollViewer.HorizontalScrollBarVisibility="Auto" AcceptsReturn="True" ScrollViewer.VerticalScrollBarVisibility="Visible" TextWrapping="Wrap" VerticalAlignment="Top" Width="515" TextChanged="countLines"/>
</DockPanel>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="27" Margin="0,298,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="523"IsEnabled="False"/>
</Grid>
<Window.CommandBindings>
<CommandBinding Command="New" Executed="NewCmdExecuted"/>
<CommandBinding Command="Open" Executed="OpenCmdExecuted"/>
<CommandBinding Command="Cut" Executed="CutCmdExecuted"/>
<CommandBinding Command="Copy" Executed="CopyCmdExecuted"/>
<CommandBinding Command="Paste" Executed="PasteCmdExecuted"/>
<CommandBinding Command="Delete" Executed="DeleteCmdExecuted"/>
<CommandBinding Command="SelectAll" Executed="SelectAllCmdExecuted"/>
</Window.CommandBindings>
</Window>
FindAndReplace.xaml//查找替换窗口
<Window x:Class="MyTextEditor.FindAndReplace"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyTextEditor"
mc:Ignorable="d"
Title="查找 /替换" Height="199.633" Width="300">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="153*"/>
<ColumnDefinition Width="139*"/>
</Grid.ColumnDefinitions>
<Label x:Name="label" Content="查找:" HorizontalAlignment="Left" Margin="10,33,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="71,35,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"Grid.ColumnSpan="2"/>
<Button x:Name="button" Content="查找下一个" HorizontalAlignment="Left" Margin="54.382,34,0,0" VerticalAlignment="Top" Width="75"Height="23" Grid.Column="1" Click="button_Click"/>
<Label x:Name="label1" Content="替换为:" HorizontalAlignment="Left" VerticalAlignment="Top"Margin="10,73,0,0"/>
<TextBox x:Name="textBox_1" HorizontalAlignment="Left" Height="23" Margin="71,73,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"Grid.ColumnSpan="2"/>
<Button x:Name="button1" Content="替换" HorizontalAlignment="Left" Margin="54.382,73,0,0" VerticalAlignment="Top" Width="75"Height="23" Grid.Column="1" Click="button1_Click"/>
<Button x:Name="button2" Content="全部替换" HorizontalAlignment="Left" Margin="54,101,0,0" VerticalAlignment="Top" Width="75"Height="23" Grid.Column="1" Click="button2_Click"/>
<CheckBox x:Name="checkBox" Content="区分大小写" HorizontalAlignment="Left" Margin="71,101,0,0" VerticalAlignment="Top"IsChecked="True" Grid.ColumnSpan="2" Width="120"/>
</Grid>
</Window>
程序功能实现
MainWindow.cs
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MyTextEditor
///<summary>
/// MainWindow.xaml的交互逻辑
///</summary>
public partial class MainWindow : Window
publicMainWindow()
InitializeComponent();
this.WindowStartupLocation= System.Windows.WindowStartupLocation.CenterScreen;
private voidNewCmdExecuted(object sender, ExecutedRoutedEventArgs e) //保存文本到指定路径
if(textBox1.Text != null)
MessageBoxResultresult =MessageBox.Show("是否保存文本文件?","保存",MessageBoxButton.YesNo);
if(result == MessageBoxResult.Yes)
SaveFileDialogsaveDlg =new SaveFileDialog();
saveDlg.Filter = "Text Files | *.txt"; //只能保存成TXT文本文件
if (true ==saveDlg.ShowDialog())
File.WriteAllText(saveDlg.FileName,textBox1.Text);
else
textBox1.Text = null;
private voidOpenCmdExecuted(object sender, ExecutedRoutedEventArgs e) //打开指定路径文本
OpenFileDialogopenDlg =new OpenFileDialog();
openDlg.Filter = "Text Files | *.txt";//只能打开TXT文本文件
if (true ==openDlg.ShowDialog())
stringdataFromFile =File.ReadAllText(openDlg.FileName);
textBox1.Text = dataFromFile;
private voidSelectAllCmdExecuted(object sender, ExecutedRoutedEventArgs e)
private voidDeleteCmdExecuted(object sender, ExecutedRoutedEventArgs e)
private voidCutCmdExecuted(object sender, ExecutedRoutedEventArgs e)
private voidCopyCmdExecuted(object sender, ExecutedRoutedEventArgs e)
private voidPasteCmdExecuted(object sender, ExecutedRoutedEventArgs e)
private voidfindAndReplace(object sender, RoutedEventArgs e) //查找替换窗口
FindAndReplace fr=new FindAndReplace();
fr.Owner = this;
fr.ShowInTaskbar = false;
fr.Show();
private voidmenuAutoWrap_Click(object sender, RoutedEventArgs e) //自动换行
if(menuAutoWrap.IsChecked ==true)
textBox1.TextWrapping = TextWrapping.WrapWithOverflow;
menuAutoWrap.IsChecked = false;
if(menuAutoWrap.IsChecked ==false)
textBox1.TextWrapping = TextWrapping.NoWrap;
menuAutoWrap.IsChecked = true;
private voidFileExit_Click(object sender, RoutedEventArgs e) //退出
this.NewCmdExecuted(null,null);
App.Current.Shutdown();
private voidcountLines(object sender, RoutedEventArgs e) //显示文本行数
this.textBox.Text= "文本当前行数:" + this.textBox1.LineCount.ToString();
FindAndReplace.cs//查找和替换功能
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace MyTextEditor
///<summary>
/// FindAndReplace.xaml的交互逻辑
///</summary>
public partial class FindAndReplace : Window
public intstart, pos;
publicFindAndReplace()
InitializeComponent();
this.WindowStartupLocation= System.Windows.WindowStartupLocation.CenterOwner;
private voidbutton_Click(object sender, RoutedEventArgs e) //查找
MainWindow win= (MainWindow)this.Owner;
stringstrFind = win.textBox1.Text;
boolbCase = checkBox.IsChecked ==true ? true : false;
StringComparison com= bCase ==true ? StringComparison.CurrentCulture :StringComparison.CurrentCultureIgnoreCase;
if((pos = strFind.IndexOf(textBox.Text, start, com)) != -1)
win.textBox1.Select(pos,textBox.Text.Length);
win.textBox1.Focus();
start = pos +textBox.Text.Length;
else
pos = 0;
start = 0;
private voidbutton1_Click(object sender, RoutedEventArgs e) //替换
MainWindow win= (MainWindow)this.Owner;
stringstrFind = win.textBox1.Text;
boolbCase = checkBox.IsChecked ==true ? true : false;
StringComparison com= bCase ==true ? StringComparison.CurrentCulture :StringComparison.CurrentCultureIgnoreCase;
if((pos = strFind.IndexOf(textBox.Text, start, com)) != -1)
win.textBox1.Select(pos,textBox.Text.Length);
win.textBox1.Focus();
win.textBox1.SelectedText =textBox_1.Text;
start = pos +textBox.Text.Length;
else
pos = 0;
start = 0;
private voidbutton2_Click(object sender, RoutedEventArgs e) //全部替换
pos = 0;
start = 0;
MainWindow win= (MainWindow)this.Owner;
stringstrFind = win.textBox1.Text;
boolbCase = checkBox.IsChecked ==true ? true : false;
StringComparison com= bCase ==true ? StringComparison.CurrentCulture :StringComparison.CurrentCultureIgnoreCase;
while((pos = strFind.IndexOf(textBox.Text, start, com)) != -1)
win.textBox1.Select(pos,textBox.Text.Length);
win.textBox1.Focus();
win.textBox1.SelectedText =textBox_1.Text;
start = pos + textBox.Text.Length;
-----------------------------
实现效果图
其他效果图及文档请进入个人网站www.ycxblog.com下载查看。
---------------------------------------------------------------------------------------------------
END
2016/06/07
以上是关于文本编辑器设计的主要内容,如果未能解决你的问题,请参考以下文章