/// <summary> /// 处理打开文档或插入文档 /// </summary> /// <param name="IsInsertFile"></param> public void LoadOrInsertFile(bool IsInsertFile, string FileName) { try { if (File.Exists(FileName) == false) { return; } string FileExt = System.IO.Path.GetExtension(FileName).ToUpper(); //文件扩展名 TextRange range = null; if (IsInsertFile) { range = _rtf.Selection; } else { range = new TextRange(_rtf.Document.ContentStart, _rtf.Document.ContentEnd); //用户输入了些东西 if (MySuperEditorHelper.IsPrintableString(range.Text)) { MessageBoxResult ret = MessageBox.Show("保存旧文档吗?", "保存文档", MessageBoxButton.YesNo); if (ret == MessageBoxResult.Yes) { SaveToFile(true); } } CurFileName = FileName; } //打开文件 using (FileStream fs = new FileStream(FileName, FileMode.Open)) { switch (FileExt) { case ".XAMLPACKAGE": range.Load(fs, DataFormats.XamlPackage); break; case ".XAML": range.Load(fs, DataFormats.Xaml); break; case ".RTF": range.Load(fs, DataFormats.Rtf); break; case ".TXT": default: //由于TextRange对象不能直接装入Unicode字符,所以,采用其他方法进行处理 range.Text = MySuperEditorHelper.LoadStringFromTextFile(fs); break; } } } catch (Exception ex) { throw ex; } }
private void SynchronzieFontFamilies() { Object obj = null; //显示当前字体 if (RichTextBox1.Selection.IsEmpty) //如果当前没选中文字 { TextRange nearestChar = rtfManager.GetPrevChar(RichTextBox1.CaretPosition); if ((nearestChar != null) && MySuperEditorHelper.IsTwoByteChineseChar(nearestChar.Text)) //是汉字,获取离光标最近字符的字体 obj = nearestChar.GetPropertyValue(TextElement.FontFamilyProperty); else //不是汉字,或者文档为空直接调用RichTextBox默认功能实现 obj = RichTextBox1.Selection.GetPropertyValue(TextElement.FontFamilyProperty); } else //如果当前选中文字 obj = RichTextBox1.Selection.GetPropertyValue(TextElement.FontFamilyProperty); if (obj is FontFamily) //设置字体下拉框选项为当前字体 { String FontName = MySuperEditorHelper.GetLocaliteFontName((FontFamily)obj); //此句居然会引发TabControl的SelectionChanged事件?真是诡异! cboFontFamilies.Text = FontName; } }
private void OnPaste(object sender, ExecutedRoutedEventArgs e) { //提取剪贴板数据 IDataObject dataObject = Clipboard.GetDataObject(); //检查数据是否为设备无关位图 if (dataObject != null && dataObject.GetData(DataFormats.Dib) != null) { //将Dib转换为Bitmap Stream imageData = Clipboard.GetDataObject().GetData(DataFormats.Dib) as Stream; System.Drawing.Bitmap bmp = MySuperEditorHelper.CreateBitmapFromDib(imageData); //创建WPF的Image控件,并将其插入到当前光标位置 System.Windows.Controls.Image imageControl = new System.Windows.Controls.Image(); imageControl.Source = MySuperEditorHelper.BitmapToImageSource(bmp); imageControl.Width = bmp.Width; imageControl.Height = bmp.Height; //如果当前有选中的,则先清空它,之后再插入图片,这样一来,就实现了“替换”的效果 if (String.IsNullOrEmpty(RichTextBox1.Selection.Text) == false) { RichTextBox1.Selection.Text = ""; } InlineUIContainer container = new InlineUIContainer(imageControl, RichTextBox1.Selection.Start); } else { RichTextBox1.Paste(); } if (Clipboard.ContainsData(DataFormats.Bitmap)) { //如果粘贴的是图片,则设置为左对齐(因为默认情况下,粘贴图片是居中对齐) EditingCommands.AlignLeft.Execute(null, RichTextBox1); TextRange selection = RichTextBox1.Selection; //取消选中状态,光标移动到最后 selection.Select(selection.End, selection.End); } }
/// <summary> /// 新建文档 /// </summary> public void NewDocument() { try { TextRange range = new TextRange(_rtf.Document.ContentStart, _rtf.Document.ContentEnd); if (MySuperEditorHelper.IsPrintableString(range.Text)) { MessageBoxResult ret = MessageBox.Show("保存旧文档吗?", "保存文档", MessageBoxButton.YesNo); if (ret == MessageBoxResult.Yes) { SaveToFile(true); } } range.Text = ""; CurFileName = "无标题"; } catch (Exception ex) { throw ex; } }
private void SynchronzieFontFamilies() { Object obj = null; //显示当前字体 if (RichTextBox1.Selection.IsEmpty) //如果当前没选中文字 { TextRange nearestChar = rtfManager.GetPrevChar(RichTextBox1.CaretPosition); if ((nearestChar != null) && MySuperEditorHelper.IsTwoByteChineseChar(nearestChar.Text)) { //是汉字,获取离光标最近字符的字体 obj = nearestChar.GetPropertyValue(TextElement.FontFamilyProperty); } else { //不是汉字,或者文档为空直接调用RichTextBox默认功能实现 obj = RichTextBox1.Selection.GetPropertyValue(TextElement.FontFamilyProperty); } } else //如果当前选中文字 { obj = RichTextBox1.Selection.GetPropertyValue(TextElement.FontFamilyProperty); } }
/// <summary> /// 使用一个列表框显示系统字体 /// </summary> /// <param name="lst"></param> private void LoadSystemFontsToListBox(ComboBox cbo) { foreach (FontFamily family in Fonts.SystemFontFamilies) cbo.Items.Add(MySuperEditorHelper.GetLocaliteFontName(family)); }