![](/img/trans.png)
[英]Save Background Color when saving RichTextBox to rtf or html file?
[英]Richtextbox with color and save,load the richtextbox with color into a rtf file in wpf c#
如何使用 wpf 和 c# 使富文本框支持顏色並將富文本框內容保存在 rtf 文件中? 我希望用戶可以 select 在richtextbox 中輸入一個單詞並給它一個特殊的字體顏色。 並且用戶可以將具有特殊顏色的richtextbox保存到一個.rtf文件中並將其加載回具有特殊顏色的richtextbox。
在這里,您有一些東西可以幫助您入門:
XAML:
<Window x:Class="WpfApp11.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp11"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="File path: "/>
<TextBox x:Name="filePathBox" Width="300" Margin="5,0,0,0"/>
</StackPanel>
<Button x:Name="open" Click="open_Click" Content="Open" Width="300" Margin="0,5,0,0" HorizontalAlignment="Left"/>
<TextBlock Text="Document" Margin="0,5,0,0"/>
<RichTextBox x:Name="richTextBox" Margin="0,5,0,0"/>
<Button x:Name="addColor" Click="addColor_Click" Content="Select and press this button to add color to text" Width="300" Margin="0,5,0,0" HorizontalAlignment="Left"/>
<Button x:Name="save" Click="save_Click" Content="Save" Width="300" Margin="0,5,0,0" HorizontalAlignment="Left"/>
<TextBlock Text="Status"/>
<TextBlock x:Name="statusBox" Width="300" Margin="0,5,0,0" HorizontalAlignment="Left"/>
</StackPanel>
</Grid>
</Window>
隱藏代碼 (xaml.cs)
using System;
using System.IO;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
namespace WpfApp11
{
public partial class MainWindow : Window
{
public MainWindow()
{
DataContext = this;
InitializeComponent();
}
private void addColor_Click(object sender, RoutedEventArgs e)
{
richTextBox.Selection.ApplyPropertyValue(ForegroundProperty, new SolidColorBrush(Colors.Red));
}
private void open_Click(object sender, RoutedEventArgs e)
{
try
{
TextRange range;
FileStream fStream;
string filename = filePathBox.Text;
if (File.Exists(filename))
{
range = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
using (fStream = new FileStream(filename, FileMode.OpenOrCreate))
{
range.Load(fStream, DataFormats.Rtf);
fStream.Close();
}
statusBox.Text = "File opened.";
}
else
{
statusBox.Text = "File new.";
}
}
catch (Exception excep)
{
statusBox.Text = excep.Message;
}
}
private void save_Click(object sender, RoutedEventArgs e)
{
try
{
TextRange range;
FileStream fStream;
range = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
string filename = filePathBox.Text;
using (fStream = new FileStream(filename, FileMode.Create))
{
range.Save(fStream, DataFormats.Rtf);
fStream.Close();
}
statusBox.Text = "File saved.";
}
catch (Exception excep)
{
statusBox.Text = excep.Message;
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.