WPF学习-文本编辑器(部分功能的设计)
2012-04-24 09:57
369 查看
1.文件-新建:检查文本中的内容是否为null,不为null,提示是否将当前内容保存,如果需要保存(MessageBoxResult.Yes),则保存。
表用系统自带的类SaveFileDialog,实现文本流的读入读出。
2.文件-退出:和File-New功能类似,在退出应用程序时,先询问是否保存当前文本。这里的提示框MessageBox使用的是MessageBoxButton.YesNoCancel,可以取消退出。
3.文件-打开:使用系统自带的OpenFileDialog类,与上面的SaveFileDialog类似,不多说了。
4.工具-查找和替换,InputGestureText="Ctrl+F",设置快捷键,这里调用了另一个findAndReplace窗口,frw.Owner = this,拥有这窗口的使用权限。
下面详细讲解findAndReplace窗口。
(1)查找
查找MainWindow窗口中的txtData.Text,需要在findAndReplace窗口中获得MainWindow窗口的权限,MainWindow win = (MainWindow)this.Owner;
这里需要注意的是区分大小写,bool bCase = chkCase.IsChecked == true ? true : false; 通过StringComparison设置大小写,CurrentCulture为区分大小写,CurrentCultureIgnoreCase不区分。
查找的基本思想:设置pos开始的位置,通过txtData.Text.IndexOf(txtFind.Text, start, com) 找到查找的内容,根据StringComparison设置CurrentCulture为需要大小写,CurrentCultureIgnoreCase忽略大小写,最后将start前移到下一次查找的位置,start = pos + txtFind.Text.Length,方便下一次查找。
(2)替换
查找过后进行替换,替换前先将start后移,返回到查找内容的开始位置start = pos- txtFind.Text.Length,替换结束后将start前移到下一次查找的位置,start = pos + txtFind.Text.Length。
(3)全部替换
private void btnReplaceAll_Click(object sender, RoutedEventArgs e)
{
pos = 0;
start = 0;
MainWindow win = (MainWindow)this.Owner;
string strFind = win.txtData.Text;
bool bCase = chkCase.IsChecked == true ? true : false;
StringComparison com = bCase == true ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase;
while ((pos = strFind.IndexOf(txtFind.Text, start, com)) != -1)
{
win.txtData.Select(pos, txtFind.Text.Length);
win.txtData.Focus();
win.txtData.SelectedText = txtReplace.Text;
start = pos + txtFind.Text.Length;
}
}
5.工具-自动换行:使用TextWrapping。
6.工具-拼写错误
结束语:主要功能介绍完成,不足就是文字功能部分没有完成。
表用系统自带的类SaveFileDialog,实现文本流的读入读出。
private void NewCmdExecuted(object sender, ExecutedRoutedEventArgs e) { if (txtData.Text != null) { MessageBoxResult result= MessageBox.Show("create new document, save yes or not", "create new", MessageBoxButton.YesNo); if (result == MessageBoxResult.Yes) { SaveFileDialog saveDlg = new SaveFileDialog(); saveDlg.Filter = "Text Files | *.txt"; if (true == saveDlg.ShowDialog()) { File.WriteAllText(saveDlg.FileName, txtData.Text); } } else { txtData.Text = null; } } }
2.文件-退出:和File-New功能类似,在退出应用程序时,先询问是否保存当前文本。这里的提示框MessageBox使用的是MessageBoxButton.YesNoCancel,可以取消退出。
private void FileExit_Click(object sender, RoutedEventArgs e) { MessageBoxResult result = MessageBox.Show("save before exit, yes or not", "exit", MessageBoxButton.YesNoCancel); if (result == MessageBoxResult.Yes) { SaveFileDialog saveDlg = new SaveFileDialog(); saveDlg.Filter = "Text Files | *.txt"; if (true == saveDlg.ShowDialog()) { File.WriteAllText(saveDlg.FileName, txtData.Text); } } else if (result == MessageBoxResult.No) { this.Close(); } }
3.文件-打开:使用系统自带的OpenFileDialog类,与上面的SaveFileDialog类似,不多说了。
private void OpenCmdExecuted(object sender, ExecutedRoutedEventArgs e) { OpenFileDialog openDlg = new OpenFileDialog(); openDlg.Filter = "Text Files | *.txt"; if (true == openDlg.ShowDialog()) { string dataFromFile = File.ReadAllText(openDlg.FileName); txtData.Text = dataFromFile; } }
4.工具-查找和替换,InputGestureText="Ctrl+F",设置快捷键,这里调用了另一个findAndReplace窗口,frw.Owner = this,拥有这窗口的使用权限。
private void findAndReplace(object sender, RoutedEventArgs e) { findAndReplace frw = new findAndReplace(); frw.ShowInTaskbar = false; frw.Title = "查找替换对话框"; frw.Owner = this; frw.Show(); }
下面详细讲解findAndReplace窗口。
(1)查找
查找MainWindow窗口中的txtData.Text,需要在findAndReplace窗口中获得MainWindow窗口的权限,MainWindow win = (MainWindow)this.Owner;
这里需要注意的是区分大小写,bool bCase = chkCase.IsChecked == true ? true : false; 通过StringComparison设置大小写,CurrentCulture为区分大小写,CurrentCultureIgnoreCase不区分。
查找的基本思想:设置pos开始的位置,通过txtData.Text.IndexOf(txtFind.Text, start, com) 找到查找的内容,根据StringComparison设置CurrentCulture为需要大小写,CurrentCultureIgnoreCase忽略大小写,最后将start前移到下一次查找的位置,start = pos + txtFind.Text.Length,方便下一次查找。
private void btnFindNext_Click(object sender, RoutedEventArgs e) { MainWindow win = (MainWindow)this.Owner; string strFind = win.txtData.Text; bool bCase = chkCase.IsChecked == true ? true : false; StringComparison com=bCase==true?StringComparison.CurrentCulture:StringComparison.CurrentCultureIgnoreCase; if ((pos = strFind.IndexOf(txtFind.Text, start, com)) != -1) { win.txtData.Select(pos, txtFind.Text.Length); win.txtData.Focus(); start = pos + txtFind.Text.Length; } else { pos = 0; start = 0; } }
(2)替换
查找过后进行替换,替换前先将start后移,返回到查找内容的开始位置start = pos- txtFind.Text.Length,替换结束后将start前移到下一次查找的位置,start = pos + txtFind.Text.Length。
private void btnReplace_Click(object sender, RoutedEventArgs e) { start = pos- txtFind.Text.Length; MainWindow win = (MainWindow)this.Owner; //获取MainWindow的访问权限 string strFind = win.txtData.Text; bool bCase = chkCase.IsChecked == true ? true : false; StringComparison com = bCase == true ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase; if ((pos = strFind.IndexOf(txtFind.Text, start, com)) != -1) { win.txtData.Select(pos, txtFind.Text.Length); win.txtData.Focus(); win.txtData.SelectedText = txtReplace.Text; start = pos + txtFind.Text.Length; } else { pos=0; start=0; } }
(3)全部替换
private void btnReplaceAll_Click(object sender, RoutedEventArgs e)
{
pos = 0;
start = 0;
MainWindow win = (MainWindow)this.Owner;
string strFind = win.txtData.Text;
bool bCase = chkCase.IsChecked == true ? true : false;
StringComparison com = bCase == true ? StringComparison.CurrentCulture : StringComparison.CurrentCultureIgnoreCase;
while ((pos = strFind.IndexOf(txtFind.Text, start, com)) != -1)
{
win.txtData.Select(pos, txtFind.Text.Length);
win.txtData.Focus();
win.txtData.SelectedText = txtReplace.Text;
start = pos + txtFind.Text.Length;
}
}
5.工具-自动换行:使用TextWrapping。
private void menuAutoWrap_Click(object sender, RoutedEventArgs e) { if (menuAutoWrap.IsChecked) { menuAutoWrap.IsChecked = false; txtData.TextWrapping = TextWrapping.WrapWithOverflow; } }
6.工具-拼写错误
private void ToolsSpellingHins_Click(object sender, RoutedEventArgs e) { string spellingHints = string.Empty; SpellingError error = txtData.GetSpellingError(txtData.CaretIndex); if (error != null) { foreach (string s in error.Suggestions) { spellingHints += string.Format("{0}\n", s); } lblSpellingHints.Content = spellingHints; expanderSpelling.IsExpanded = true; } }
结束语:主要功能介绍完成,不足就是文字功能部分没有完成。
相关文章推荐
- WPF:从WPF Diagram Designer Part 3学习如何给设计器增加连接线功能
- WPF:从WPF Diagram Designer Part 3学习如何给设计器增加连接线功能
- WPF学习-文本编辑器(界面的<xaml>设计)
- Tomcat学习2_Tomcat 系统架构与设计模式,第 2 部分: 设计模式分析
- seaJs学习笔记之seaJs实现webQQ部分功能2
- WPF:从WPF Diagram Designer Part 4学习分组、对齐、排序、序列化和常用功能
- 走进WPF系列学习之一 如何建立、设计、执行与部署WPF应用程序
- JavaScript学习笔记:用JavaScript部分小功能的实现
- WPF:从WPF Diagram Designer Part 4学习分组、对齐、排序、序列化和常用功能
- Visual Studio 2008 WPF设计器智能提示(Intellisense)、大纲显示功能无效的解决方案(转)
- 漫步Facebook开源C++库Folly之string类设计(散列、字符串、向量、内存分配、位处理等,小部分是对现有标准库和Boost库功能上的补充,大部分都是基于性能的需求而“重新制造轮子”)
- seaJs学习笔记之seaJs实现webQQ部分功能1
- WPF学习系列 MVVM设计模式 二 简单的增删改
- wpf学习笔记-数据绑定功能总结
- 学习设计模式笔记(二)部分总结
- WPF学习日志1---DataGrid绑定与修改更新(数据List & 数据库)[部分代码有摘录其他文章]
- mysql学习day03(事物、视图、索引、部分数据库设计)
- 转:如何学习SQL(第四部分:DBMS扩展功能与SQL高级话题)