我目前正在尝试将富文本框中的文本保存到RTF文件。我让它工作,所以我可以将文本保存到带有彩色文本的 rtf 文件中。但是,我也想在文档中保留框的背景颜色以供查看。
只要我可以保留所有颜色编码的文本,并在文档中显示适当的背景颜色,我就可以将数据保存到 rtf 或 html 中。
以下是我用来保存为带有颜色编码文本的 RTF 文件的代码。
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog saveFile1 = new SaveFileDialog();
// Initialize the SaveFileDialog to specify the RTF extension for the file.
saveFile1.DefaultExt = "*.rtf";
saveFile1.Filter = "RTF Files|*.rtf";
// Determine if the user selected a file name from the saveFileDialog.
if (saveFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK && saveFile1.FileName.Length > 0)
{
// Save the contents of the RichTextBox into the file.
richtextbox.SaveFile(saveFile1.FileName, RichTextBoxStreamType.RichText);
}
}
编辑:我发现以下代码可以将带有我的背景的文本突出显示到 RTF 文件中。目前这有效,但我仍然想就更好的解决方案发表意见。
将以下代码放在保存文件对话框打开语句之前。
richtextbox.SelectAll();
richtextbox.SelectionBackColor = richtextbox.BackColor;
richtextbox.DeselectAll();
到目前为止,
我的解决方案是在保存文件对话框之前执行以下操作:
richtextbox.SelectAll();
richtextbox.SelectionBackColor = richtextbox.BackColor;
richtextbox.DeselectAll();