RichTextBox的使用
WPF里面雖然很多形式上跟Winform一樣,但是控件的使用上面還是會(huì)有很多詫異。RichTextBox就是一個(gè)例子,是的,在WPF里面對(duì)這個(gè)控件可以做很多Winform很難做的效果出來(lái)。
比如在對(duì)RichTextBox插入圖片,winform時(shí)代除了用復(fù)制粘貼這種借助剪貼板的差勁方法之外就是要重寫(xiě)和自定義RichTextBox控件了。這就需要高超的編程能力了。但在WPF里面,只需要加幾個(gè)代碼就能搞定了。
在XAML里面添加圖片到RichTextBox可以如下所示:
??????? <RichTextBox HorizontalAlignment="Left" Margin="90,12,0,0" Name="richTextBox1">
??????????? <RichTextBox.Document>
??????????????? <FlowDocument Focusable="True" LineHeight="5">
??????????????????? <Paragraph x:Name="gara">??????????????????????
??????????????????????? 文字區(qū)域
????????????????????????<Image Source="D:\1342892_10.jpg" Focusable="True" Height="50" Stretch="Uniform" />???????????????????????
????????????????????????文字區(qū)域???????????????????????
??????????????????????? <Run Text="文字區(qū)域文字區(qū)域"></Run>
??????????????????????? <Run Text="文字區(qū)域"></Run>
??????????????????? </Paragraph>
??????????????????? <Paragraph x:Name="gara1">??????????????????????
??????????????????????? <Run Text="文字區(qū)域"></Run>
??????????????????????? <Run Text="文字區(qū)域"></Run>
??????????????????? </Paragraph>???????????????????
??????????????? </FlowDocument>
??????????? </RichTextBox.Document>
??????? </RichTextBox>
?
這樣就往控件里面添加了圖片了。
備注:FlowDocument里面的LineHeight?屬性是文字段落的間距。默認(rèn)間距很大,所以這里調(diào)整一下!
?
當(dāng)然,這樣未必能夠完全滿足要求,因?yàn)橛袝r(shí)候我們需要在程序運(yùn)行的時(shí)候點(diǎn)擊按鈕選取圖片進(jìn)行添加。代碼如下:
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());
??????????????? /* 調(diào)整圖片大小
??????????????? 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里了,是不是很簡(jiǎn)單呢!
?
原文在此:http://blogs.msdn.com/jfoscoding/archive/2006/01/14/512825.aspx?這里僅整理出其中的知識(shí)點(diǎn):
1. 取得已被選中的內(nèi)容:
(1)使用?RichTextBox.Document.Selection屬性
(2)訪問(wèn)RichTextBox.Document.Blocks屬性的“blocks”中的Text
2. 在XAML中增加內(nèi)容給RichTextBox:
<RichTextBox IsSpellCheckEnabled="True">
?? <FlowDocument>
??????? <Paragraph>
<!-- 這里加上你的內(nèi)容 -->
????????? This is a richTextBox. I can <Bold>Bold</Bold>, <Italic>Italicize</Italic>, <Hyperlink>Hyperlink stuff</Hyperlink> right in my document.
??????? </Paragraph>
?? </FlowDocument>
</RichTextBox>
3. 縮短段間距,類(lèi)似<BR>,而不是<P>
方法是使用Style定義段間距:
??? <RichTextBox>
??????? <RichTextBox.Resources>
??????????<Style TargetType="{x:Type Paragraph}">
????????????<Setter Property="Margin" Value="0"/>
????????? </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>
????? </RichTextBox>
{
??? 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的內(nèi)容:
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. 將文件中的內(nèi)容加載為RichTextBox的內(nèi)容
??????? 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的內(nèi)容保存為文件:
??????? 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. 做個(gè)簡(jiǎn)單的編輯器:
? <!-- Window1.xaml -->
? <DockPanel>
??? <Menu DockPanel.Dock="Top">
????? <MenuItem Header="_File">
??????? <MenuItem Header="_Open File" Click="OnOpenFile"/>
??????? <MenuItem Header="_Save" Click="OnSaveFile"/>
??????? <Separator/>
??????? <MenuItem Header="E_xit" Click="OnExit"/>
????? </MenuItem>??????
??? </Menu>
??? <RichTextBox Name="richTextBox1"></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);
??????????? }
??????? }心中時(shí)常裝有一盤(pán)人生的大棋,天作棋盤(pán),星作棋子,在斗轉(zhuǎn)星移中,只有不斷地搏擊人生,人生才有意義,生命才能彰顯光輝,才能收獲一分永恒。?
轉(zhuǎn)載于:https://www.cnblogs.com/jhxk/articles/2281119.html
總結(jié)
以上是生活随笔為你收集整理的RichTextBox的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 使用Android拍照程序报setPar
- 下一篇: Prism4文档翻译(第四章 第一部分)