简体   繁体   English

如何保存富文本框的背景颜色及其其他内容

[英]How can I save the background color of a rich textbox along with its other contents

I want to be able to save the contents of a rich text box along with the color of the background all into an RTF file.我希望能够将富文本框的内容与背景颜色一起保存到 RTF 文件中。 I am currently using the save dialog method:我目前正在使用保存对话框方法:

private void asRTFToolStripMenuItem_Click(object sender, EventArgs e)
{
    SaveFileDialog saveFile1 = new SaveFileDialog();
    saveFile1.DefaultExt = "*.rtf";
    saveFile1.Filter = "RTF Files|*.rtf|TXT Files|*.txt";
    if (saveFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
        saveFile1.FileName.Length > 0)
    {
        telep.SaveFile(saveFile1.FileName, RichTextBoxStreamType.RichText);
    }
}

Is there anything I can add to acomidate my needs?我可以添加什么来满足我的需求吗? Regards问候

OK, I have figured a way to save the background color.好的,我想出了一种保存背景颜色的方法。 It's pretty bad, but it does what it does这很糟糕,但它做了它做的事

On the save button click but before the save file dialog, do this:在保存按钮上单击但在保存文件对话框之前,执行以下操作:

telep.SelectAll();
            telep.SelectionBackColor = telep.BackColor;
            telep.DeselectAll();

then on the load button click, and after the load file dialog, do this:然后在加载按钮上单击,在加载文件对话框之后,执行以下操作:

telep.SelectAll();
            telep.BackColor = telep.SelectionBackColor;
            telep.DeselectAll();

All this does is it highlights the text in the same color as the rich textbox then saves.所有这一切都是它以与富文本框相同的颜色突出显示文本,然后保存。 And After loading, It changes the Rich text box back color to the one of the highlighted text.加载后,它将富文本框的背景颜色更改为突出显示的文本之一。

it depends if the background color was assigned to the control or to the text.这取决于背景颜色是分配给控件还是文本。 In the first case no way so you should make sure instead of setting such color to the control property you assign it to the text itself.在第一种情况下没有办法,所以您应该确保不要将这种颜色设置为控件属性,而是将其分配给文本本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM