日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

RichTextBox的使用

發(fā)布時間:2025/3/21 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 RichTextBox的使用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

WPF里面雖然很多形式上跟Winform一樣,但是控件的使用上面還是會有很多詫異。RichTextBox就是一個例子,是的,在WPF里面對這個控件可以做很多Winform很難做的效果出來。

比如在對RichTextBox插入圖片,winform時代除了用復(fù)制粘貼這種借助剪貼板的差勁方法之外就是要重寫和自定義RichTextBox控件了。這就需要高超的編程能力了。但在WPF里面,只需要加幾個代碼就能搞定了。

在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)然,這樣未必能夠完全滿足要求,因為有時候我們需要在程序運行的時候點擊按鈕選取圖片進(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里了,是不是很簡單呢!

?

原文在此:http://blogs.msdn.com/jfoscoding/archive/2006/01/14/512825.aspx?

這里僅整理出其中的知識點:
1. 取得已被選中的內(nèi)容:
(1)使用?RichTextBox.Document.Selection屬性
(2)訪問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. 縮短段間距,類似<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>

4. 從文件中讀出純文本文件后放進(jìn)RichTextBox或直接將文本放進(jìn)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的內(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. 做個簡單的編輯器:
? <!-- 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);
??????????? }
??????? }
心中時常裝有一盤人生的大棋,天作棋盤,星作棋子,在斗轉(zhuǎn)星移中,只有不斷地搏擊人生,人生才有意義,生命才能彰顯光輝,才能收獲一分永恒。?


轉(zhuǎn)載于:https://www.cnblogs.com/jhxk/articles/2281119.html

總結(jié)

以上是生活随笔為你收集整理的RichTextBox的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。