RTF Color Table 遍历所有颜色

RTF Color Table looping through all colors

我有一个 RichTextBox。我已将 RTF 格式(主要是颜色 table)添加到此 RichTextBox。当我第一次向其追加文本时,它循环遍历颜色 table.

的所有*颜色

*它从应用颜色 0 开始,然后是颜色 1,然后是颜色 2,依此类推,直到颜色 table 中的所有颜色都已应用,或者如果正在输出的文本已经具有其中一种颜色 - 在在这种情况下,它会停止此 "looping" 并按预期继续。例如,请参见屏幕截图。

代码如下:

    private void populateColorCodeDictionary() {
        startRTFString = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0;}}" +
        @"{\colortbl 
            ;

            \red0\green0\blue0;
            \red170\green0\blue0;
            \red0\green170\blue0;
            \red128\green128\blue0;
            \red0\green0\blue128;
            \red128\green0\blue128;
            \red0\green128\blue128;
            \red127\green127\blue127;

            \red85\green85\blue85;
            \red255\green0\blue0;
            \red0\green255\blue0;
            \red255\green255\blue0;
            \red0\green0\blue255;
            \red255\green0\blue255;
            \red0\green255\blue255;
            \red255\green255\blue255;
        }";

        colorCodeDictionary.Add("\x1b[0;30m", @"\cf1");
        colorCodeDictionary.Add("\x1b[0;31m", @"\cf2");
        colorCodeDictionary.Add("\x1b[0;32m", @"\cf3");
        colorCodeDictionary.Add("\x1b[0;33m", @"\cf4");
        colorCodeDictionary.Add("\x1b[0;34m", @"\cf5");
        colorCodeDictionary.Add("\x1b[0;35m", @"\cf6");
        colorCodeDictionary.Add("\x1b[0;36m", @"\cf7");
        colorCodeDictionary.Add("\x1b[0;37m", @"\cf8");

        colorCodeDictionary.Add("\x1b[1;30m", @"\cf9");
        colorCodeDictionary.Add("\x1b[1;31m", @"\cf10");
        colorCodeDictionary.Add("\x1b[1;32m", @"\cf11");
        colorCodeDictionary.Add("\x1b[1;33m", @"\cf12");
        colorCodeDictionary.Add("\x1b[1;34m", @"\cf13");
        colorCodeDictionary.Add("\x1b[1;35m", @"\cf14");
        colorCodeDictionary.Add("\x1b[1;36m", @"\cf15");
        colorCodeDictionary.Add("\x1b[1;37m", @"\cf16");

        /*
        \x1b[0;30m  =   cf1     =   black
        \x1b[0;31m  =   cf2     =   red
        \x1b[0;32m  =   cf3     =   green
        \x1b[0;33m  =   cf4     =   brown
        \x1b[0;34m  =   cf5     =   blue
        \x1b[0;35m  =   cf6     =   purple
        \x1b[0;36m  =   cf7     =   cyan
        \x1b[0;37m  =   cf8     =   gray

        \x1b[1;30m  =   cf9     =   darkGray
        \x1b[1;31m  =   cf10    =   light Red
        \x1b[1;32m  =   cf11    =   light green
        \x1b[1;33m  =   cf12    =   yellow
        \x1b[1;34m  =   cf13    =   light blue
        \x1b[1;35m  =   cf14    =   indigo
        \x1b[1;36m  =   cf15    =   light cyan
        \x1b[1;37m  =   cf16    =   white
        */
    }

上述方法设置了变量。最有趣的部分是 startRTFString 变量。

    private void updateOutputWindow(string text) {
        string newText = string.Empty;

        if (InvokeRequired) {
            Invoke(new MethodInvoker(delegate () {
                updateOutputWindow(text);
            }));
        }
        else {
            newText = startRTFString;
            newText += rtb_outputWindow.Rtf;
            newText += replaceAnsiColorCodes(text);

            rtb_outputWindow.Rtf = newText;
        }
    }

上述方法将文本输出到 RichTextBox。

    private string replaceAnsiColorCodes(string inData) {
        string returnString = inData;

        foreach (KeyValuePair<string, string> entry in colorCodeDictionary) {
            returnString = returnString.Replace(entry.Key, entry.Value);
        }

        returnString = returnString.Replace("\r", @"\line");               //Newline
        returnString = returnString.Replace("\x1b[0;1m", "");            //Bold

        returnString = returnString.Replace("\x1b[0m", @"\cf16 ");      //Reset

        return returnString;
    }

上述方法将ANSI代码转换为RTF颜色代码。 (以及换行符和粗体。我选择将粗体设置为无,截至目前。)

    for (int i = 0; i < 15; i++) {
        updateOutputWindow("\x1b[0mline" + i.ToString());
    }

以上只是应用程序启动后我 运行 的一个小循环。它用于测试目的,可以在以下屏幕截图中看到:

以上是问题截图。如您所见,前 15 行文本都有不同的颜色。它们都应该具有相同的默认颜色(在我的例子中是白色。)完成 "looping" 后,它会继续按预期工作;为文本应用正确的颜色。

需要注意的是,第一行(line0)上面的行是空的。颜色 table 以黑色开头,所以第 0 行应该是黑色,第 1 行应该是暗红色。不知道为什么不遵守自己的规则。

这是我的另一个测试 运行:

    for (int i = 0; i < 15; i++) {
        updateOutputWindow("\x1b[0;34mline" + i.ToString());  //This time I changed the color to be dark blue
    }

如您所见:"looping" 发生直到它 运行 变成相同的颜色,并且它一直保持该颜色。

我不知道为什么会这样,也不知道是什么原因造成的。我的猜测是我以某种方式搞砸了 RTF "script"。有谁知道解决方案? (我不希望任何这种颜色 table 循环发生。我希望它输出默认颜色,除非存在颜色代码 - 在这种情况下我希望显示该颜色。)

编辑: 我将这段代码添加到方法 updateOutputWindow MessageBox.Show(newText); 中。下面是结果截图:

正如您从上面的屏幕截图中看到的那样,RichTextBox 已经就位了某种 "default RTF code"。 -这个默认代码被添加到我的 "custom RTF code" 之上。不过,它似乎不会影响颜色 table。 (除非这是解决手头问题的原因,否则它肯定会干扰,但会以一种非常具体的一次性方式。)

编辑 #2:如果我继续 运行 这种方法一遍又一遍,则不会为每个额外的 运行 添加 RTF 代码。它被添加一次(在最顶部),然后不再添加。我认为这是一件好事,我相信这是由 RichTextBoxes 本机处理 RTF 代码的方式造成的。

您正在将终端代码(或您称之为 ANSI 代码)转换为 RTF 格式。您提供给 updateOutputWindow 的文本包含 \x1b[0;34m,但在 updateOutputWindow 中没有该终端代码的 Replace 行。你有类似的东西,但不完全是那样。结果,终端代码现在是 RTF 的一部分,所以谁知道会发生什么。必须处理所有终端代码(替换或删除)。

此外,以下代码对我来说似乎很奇怪:

newText = startRTFString;
newText += rtb_outputWindow.Rtf;
newText += replaceAnsiColorCodes(text);

rtb_outputWindow.Rtf = newText;

似乎 rtb_outputWindow.Rtf 会随着每次函数调用而增长,每次都会添加新的 startRTFString。我建议使用以下代码代替上面的代码(myStuff 是一个 属性,如 startRTFString):

myStuff = myStuff + replaceAnsiColorCodes(text);
rtb_outputWindow.Rtf = startRTFString + myStuff;