问题描述
classEditSomeRichText:Window{protectedoverridevoidOnPreviewTextInput(System.Windows.Input.TextCompositionEventArgse){if(e.ControlText.Length>0){if(e.ControlText[0]=='x0F'){OpenFileDialogdialog=newOpenFileDialog();//...}elseif(e.ControlText[0]=='x13'){SaveFileDialogdialog=newSaveFileDialog();//...}}base.OnPreviewTextInput(e);}}
当窗口上按下Ctrl+o,会打开打开文件的窗口;按下Ctrl+s,打开保存文件的窗口。但是我奇怪'x0F'和'x13'是什么意思?为什么会匹配到ctrl和o、s?
解决方案
本帖最后由 gqqnb 于 2011-12-10 23:29:04 编辑
解决方案二:
usingSystem;usingSystem.Windows;usingSystem.Windows.Input;usingSystem.Windows.Documents;usingMicrosoft.Win32;usingSystem.Windows.Controls;usingSystem.Windows.Media;usingSystem.IO;namespacesk.FileTest{publicclassFileTest:Window{RichTextBoxtxtbox;stringstrFilter="DocumentFiles(*.txt)|*.txt|AllFiles(*.*)|*.*";[STAThread]publicstaticvoidMain(){Applicationapp=newApplication();app.Run(newFileTest());}FileTest(){Title="FileTest";txtbox=newRichTextBox();txtbox.VerticalScrollBarVisibility=ScrollBarVisibility.Auto;Content=txtbox;txtbox.Focus();}protectedoverridevoidOnPreviewTextInput(TextCompositionEventArgse){if(e.ControlText.Length>0&&e.ControlText[0]=='/x0F'){OpenFileDialogdlg=newOpenFileDialog();dlg.CheckFileExists=true;dlg.Filter=strFilter;if((bool)dlg.ShowDialog(this)){FlowDocumentflow=txtbox.Document;TextRangerange=newTextRange(flow.ContentStart,flow.ContentEnd);Streamstrm=null;try{strm=newFileStream(dlg.FileName,FileMode.Open);range.Load(strm,DataFormats.Text);}catch(Exceptionexc){MessageBox.Show(exc.Message,Title);}finally{if(strm!=null)strm.Close();}}e.Handled=true;}if(e.ControlText.Length>0&&e.ControlText[0]=='/x13'){SaveFileDialogdlg=newSaveFileDialog();dlg.Filter=strFilter;if((bool)dlg.ShowDialog(this)){FlowDocumentflow=txtbox.Document;TextRangerange=newTextRange(flow.ContentStart,flow.ContentEnd);Streamstrm=null;try{strm=newFileStream(dlg.FileName,FileMode.Create);range.Save(strm,DataFormats.Text);}catch(Exceptionexc){MessageBox.Show(exc.Message,Title);}finally{if(strm!=null)strm.Close();}}e.Handled=true;}base.OnPreviewTextInput(e);}}}
解决方案三:
引用1楼hefeng_aspnet的回复:
usingSystem;usingSystem.Windows;usingSystem.Windows.Input;usingSystem.Windows.Documents;usingMicrosoft.Win32;usingSystem.Windows.Controls;usingSystem.Window……
错了,《WPF编程指南》里命名空间的名称就是EditSomeRichText