免费网站建设福州移动电子商务网站建设研究
web/
2025/9/26 22:18:13/
文章来源:
免费网站建设福州,移动电子商务网站建设研究,wordpress网站前端,哈尔滨市建设工程交易网WPF里面虽然很多形式上跟Winform一样#xff0c;但是控件的使用上面还是会有很多诧异。RichTextBox就是一个例子#xff0c;是的#xff0c;在WPF里面对这个控件可以做很多Winform很难做的效果出来。比如在对RichTextBox插入图片#xff0c;winform时代除了用复制粘贴这种借…WPF里面虽然很多形式上跟Winform一样但是控件的使用上面还是会有很多诧异。RichTextBox就是一个例子是的在WPF里面对这个控件可以做很多Winform很难做的效果出来。比如在对RichTextBox插入图片winform时代除了用复制粘贴这种借助剪贴板的差劲方法之外就是要重写和自定义RichTextBox控件了。这就需要高超的编程能力了。但在WPF里面只需要加几个代码就能搞定了。在XAML里面添加图片到RichTextBox可以如下所示 RichTextBox HorizontalAlignmentLeft Margin90,12,0,0 NamerichTextBox1 RichTextBox.Document FlowDocument FocusableTrue LineHeight5 Paragraph x:Namegara 文字区域 Image SourceD:\1342892_10.jpg FocusableTrue Height50 StretchUniform / 文字区域 Run Text文字区域文字区域/Run Run Text文字区域/Run /Paragraph Paragraph x:Namegara1 Run Text文字区域/Run Run Text文字区域/Run /Paragraph /FlowDocument /RichTextBox.Document /RichTextBox 这样就往控件里面添加了图片了。备注FlowDocument里面的LineHeight 属性是文字段落的间距。默认间距很大所以这里调整一下 当然这样未必能够完全满足要求因为有时候我们需要在程序运行的时候点击按钮选取图片进行添加。代码如下private void AddJPG_Click(object sender, RoutedEventArgs e) { string filepath ; string filename ; OpenFileDialog openfilejpg new OpenFileDialog(); openfilejpg.Filter jpg图片(*.jpg)|*.jpg|gif图片(*.gif)|*.gif; openfilejpg.FilterIndex 0; openfilejpg.RestoreDirectory true; openfilejpg.Multiselect false; if (openfilejpg.ShowDialog() true) { filepath openfilejpg.FileName; Image img new Image(); BitmapImage bImg new BitmapImage(); img.IsEnabled true; bImg.BeginInit(); bImg.UriSource new Uri(filepath, UriKind.Relative); bImg.EndInit(); img.Source bImg; //MessageBox.Show(bImg.Width.ToString() , bImg.Height.ToString()); /* 调整图片大小 if (bImg.Height 100 || bImg.Width 100) { img.Height bImg.Height * 0.2; img.Width bImg.Width * 0.2; }*/ img.Stretch Stretch.Uniform; //图片缩放模式 new InlineUIContainer(img, richTextBox1.Selection.Start); //插入图片到选定位置 } }这样就插入了一张图片到RichTextBox里了是不是很简单呢 原文在此http://blogs.msdn.com/jfoscoding/archive/2006/01/14/512825.aspx 这里仅整理出其中的知识点1. 取得已被选中的内容(1)使用 RichTextBox.Document.Selection属性(2)访问RichTextBox.Document.Blocks属性的“blocks”中的Text2. 在XAML中增加内容给RichTextBox:RichTextBox IsSpellCheckEnabledTrue FlowDocument Paragraph!-- 这里加上你的内容 -- This is a richTextBox. I can BoldBold/Bold, ItalicItalicize/Italic, HyperlinkHyperlink stuff/Hyperlink right in my document. /Paragraph /FlowDocument/RichTextBox3. 缩短段间距,类似BR,而不是P方法是使用Style定义段间距: RichTextBox RichTextBox.Resources Style TargetType{x:Type Paragraph} Setter PropertyMargin Value0/ /Style /RichTextBox.Resources FlowDocument Paragraph This is my first paragraph... see how there is... /Paragraph Paragraph a no space anymore between it and the second paragraph? /Paragraph /FlowDocument /RichTextBox4. 从文件中读出纯文本文件后放进RichTextBox或直接将文本放进RichTextBox中:private void LoadTextFile(RichTextBox richTextBox, string filename){ richTextBox.Document.Blocks.Clear(); using (StreamReader streamReader File.OpenText(filename)) { Paragraph paragraph new Paragraph(); paragraph.Text streamReader.ReadToEnd(); richTextBox.Document.Blocks.Add(paragraph); }}private void LoadText(RichTextBox richTextBox, string txtContent){ richTextBox.Document.Blocks.Clear(); Paragraph paragraph new Paragraph(); paragraph.Text txtContent; richTextBox.Document.Blocks.Add(paragraph);}5. 取得指定RichTextBox的内容private string GetText(RichTextBox richTextBox) { TextRange textRange new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd); return textRange.Text;}6. 将RTF (rich text format)放到RichTextBox中private static void LoadRTF(string rtf, RichTextBox richTextBox) { if (string.IsNullOrEmpty(rtf)) { throw new ArgumentNullException(); } TextRange textRange new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd); using (MemoryStream rtfMemoryStream new MemoryStream()) { using (StreamWriter rtfStreamWriter new StreamWriter(rtfMemoryStream)) { rtfStreamWriter.Write(rtf); rtfStreamWriter.Flush(); rtfMemoryStream.Seek(0, SeekOrigin.Begin);//Load the MemoryStream into TextRange ranging from start to end of RichTextBox. textRange.Load(rtfMemoryStream, DataFormats.Rtf); } } }7. 将文件中的内容加载为RichTextBox的内容 private static void LoadFile(string filename, RichTextBox richTextBox) { if (string.IsNullOrEmpty(filename)) { throw new ArgumentNullException(); } if (!File.Exists(filename)) { throw new FileNotFoundException(); } using (FileStream stream File.OpenRead(filename)) { TextRange documentTextRange new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd); string dataFormat DataFormats.Text; string ext System.IO.Path.GetExtension(filename); if (String.Compare(ext, .xaml,true) 0) { dataFormat DataFormats.Xaml; } else if (String.Compare(ext, .rtf, true) 0) { dataFormat DataFormats.Rtf; } documentTextRange.Load(stream, dataFormat); } }8. 将RichTextBox的内容保存为文件 private static void SaveFile(string filename, RichTextBox richTextBox) { if (string.IsNullOrEmpty(filename)) { throw new ArgumentNullException(); } using (FileStream stream File.OpenWrite(filename)) { TextRange documentTextRange new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd); string dataFormat DataFormats.Text; string ext System.IO.Path.GetExtension(filename); if (String.Compare(ext, .xaml, true) 0) { dataFormat DataFormats.Xaml; } else if (String.Compare(ext, .rtf, true) 0) { dataFormat DataFormats.Rtf; } documentTextRange.Save(stream, dataFormat); } }9. 做个简单的编辑器 !-- Window1.xaml -- DockPanel Menu DockPanel.DockTop MenuItem Header_File MenuItem Header_Open File ClickOnOpenFile/ MenuItem Header_Save ClickOnSaveFile/ Separator/ MenuItem HeaderE_xit ClickOnExit/ /MenuItem /Menu RichTextBox NamerichTextBox1/RichTextBox /DockPanel // Window1.xaml.cs private void OnExit(object sender, EventArgs e) { this.Close(); } private void OnOpenFile(object sender, EventArgs e) { Microsoft.Win32.OpenFileDialog ofd new Microsoft.Win32.OpenFileDialog(); ofd.Filter Text Files (*.txt; *.xaml; *.rtf)|*.txt;*.xaml;*.rtf; ofd.Multiselect false; if (ofd.ShowDialog() true) { LoadFile(ofd.SafeFileName, richTextBox1); } } private void OnSaveFile(object sender, EventArgs e) { Microsoft.Win32.SaveFileDialog sfd new Microsoft.Win32.SaveFileDialog(); sfd.Filter Text Files (*.txt; *.xaml; *.rtf)|*.txt;*.xaml;*.rtf; if (sfd.ShowDialog() true) { SaveFile(sfd.SafeFileName, richTextBox1); } }心中时常装有一盘人生的大棋,天作棋盘,星作棋子,在斗转星移中,只有不断地搏击人生,人生才有意义,生命才能彰显光辉,才能收获一分永恒。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/82414.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!