更新CMD中现有文本的颜色?

发布于 2025-01-22 02:00:45 字数 110 浏览 1 评论 0原文

我正在使用2D数组在命令行中显示sudoku网格,我想更改用户通过箭头键选择的数字的颜色。除了突出显示部分,我还有一切。

有没有办法在2D数组中更改所选数字的颜色? 还是还有其他更有效的方法?

I am using a 2D array to display Sudoku grids in command line and I want to change the colour of a number which is selected by the user through arrow keys. I have everything except the highlighting part.

Is there a way to change the colour of a selected number in a 2d array?
Or would there be some other, more efficient way of doing that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

潦草背影 2025-01-29 02:00:46

如果您想要这样的效果,

“

然后您必须在图纸例程中处理它。

上面的示例是用

public class Matrix
{
    int[,] data;

    public Matrix(int rows, int columns)
    {
        Rows = rows;
        Columns = columns;
        data = new int[rows, columns];
    }

    internal int[,] Data => data;

    public int Rows { get; }
    public int Columns { get; }

    public ref int this[int row, int col]
        => ref data[row, col];

    public void DrawConsole(int rowCurrent, int colCurrent)
    {
        var fg = Console.ForegroundColor;
        var bk = Console.BackgroundColor;
        var curX = Console.CursorLeft;
        var curY = Console.CursorTop;

        const int width = 7;

        for (int i = 0; i < Rows; i++)
        {
            for (int j = 0; j < Columns; j++)
            {
                Console.SetCursorPosition(width * j + 3, i * 2 + 2);
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.BackgroundColor = ConsoleColor.Black;
                Console.Write(" ");
                if (i == rowCurrent && j == colCurrent)
                {
                    Console.ForegroundColor = ConsoleColor.Black;
                    Console.BackgroundColor = ConsoleColor.DarkCyan;
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.DarkBlue;
                    Console.BackgroundColor = ConsoleColor.Gray;
                }
                Console.Write(CenteredText(this[i, j].ToString(), width-1));
            }
        }

        Console.SetCursorPosition(curX, curY);
        Console.ForegroundColor = fg;
        Console.BackgroundColor = bk;
    }

    public static string CenteredText(string text, int width)
    {
        if (text.Length >= width) return text;
        int spaces = width- text.Length;
        return text.PadRight(spaces / 2 + text.Length).PadLeft(width);
    }
}
class Program
{
    static void Main(string[] args)
    {
        var grid = new Matrix(9, 9);
        int index = 0;
        for (int i = 0; i < grid.Rows; i++)
        {
            for (int j = 0; j < grid.Columns; j++)
            {
                grid[i, j] = ++index;
            }
        }

        grid.DrawConsole(3, 6);
    }
}

完成的,如果(i == rowcurrent&amp;&amp;&amp;&amp;&amp; j == colcurrent) line> line分支以突出显示的单元格在写出文本之前更改显示颜色。

If you want an effect like this

scr1

then you have to handle it in the drawing routine.

The example above is done with

public class Matrix
{
    int[,] data;

    public Matrix(int rows, int columns)
    {
        Rows = rows;
        Columns = columns;
        data = new int[rows, columns];
    }

    internal int[,] Data => data;

    public int Rows { get; }
    public int Columns { get; }

    public ref int this[int row, int col]
        => ref data[row, col];

    public void DrawConsole(int rowCurrent, int colCurrent)
    {
        var fg = Console.ForegroundColor;
        var bk = Console.BackgroundColor;
        var curX = Console.CursorLeft;
        var curY = Console.CursorTop;

        const int width = 7;

        for (int i = 0; i < Rows; i++)
        {
            for (int j = 0; j < Columns; j++)
            {
                Console.SetCursorPosition(width * j + 3, i * 2 + 2);
                Console.ForegroundColor = ConsoleColor.Gray;
                Console.BackgroundColor = ConsoleColor.Black;
                Console.Write(" ");
                if (i == rowCurrent && j == colCurrent)
                {
                    Console.ForegroundColor = ConsoleColor.Black;
                    Console.BackgroundColor = ConsoleColor.DarkCyan;
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.DarkBlue;
                    Console.BackgroundColor = ConsoleColor.Gray;
                }
                Console.Write(CenteredText(this[i, j].ToString(), width-1));
            }
        }

        Console.SetCursorPosition(curX, curY);
        Console.ForegroundColor = fg;
        Console.BackgroundColor = bk;
    }

    public static string CenteredText(string text, int width)
    {
        if (text.Length >= width) return text;
        int spaces = width- text.Length;
        return text.PadRight(spaces / 2 + text.Length).PadLeft(width);
    }
}
class Program
{
    static void Main(string[] args)
    {
        var grid = new Matrix(9, 9);
        int index = 0;
        for (int i = 0; i < grid.Rows; i++)
        {
            for (int j = 0; j < grid.Columns; j++)
            {
                grid[i, j] = ++index;
            }
        }

        grid.DrawConsole(3, 6);
    }
}

The if (i == rowCurrent && j == colCurrent) line branches off for the highlighted cell changing the display colors before writing the text out.

回忆追雨的时光 2025-01-29 02:00:45

谢谢大家的输入,我找到了解决方案:

  • 获取项目的位置要突出显示
  • 功能,再次
  • 在2D阵列中添加if语句以进行循环,并传递坐标。
  • 呼叫显示 它再次回到它的外面

并不高效,看起来很丑陋,但效果很好。

抱歉,我无法共享代码。

Thank you all for your input, I have found the solution:

  • Get position of the item to be highlighted
  • Call the Display function again
  • Add an if statement in the 2D array's for loop and pass it the coordinates
  • Change the color in the if statement and change it right back again outside of it

Its not efficient and looks ugly but it works well.

Sorry I couldn't share the code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文