【问题标题】:Is there a way to set the console window to a fixed max/min value有没有办法将控制台窗口设置为固定的最大值/最小值
【发布时间】:2019-10-08 15:45:57
【问题描述】:

目前我正在确定某些单词在控制台中的位置,在输入 Enter 键后,我总是将光标放在下一个单词上,但我遇到的问题是,如果我更改窗口的大小,我存储的单词坐标不再正确。

foreach (var item in splittedTxt)
            {
                if (wordToFind == item)
                {
                    var cursorPositionTop = Console.CursorTop;
                    var cursorPositionLeft = Console.CursorLeft;
                    Console.BackgroundColor = ConsoleColor.Blue;
                    Console.ForegroundColor = ConsoleColor.White;
                    wordFound = true;


                    foundWordPositions.Add(new CursorPosition(cursorPositionTop, cursorPositionLeft));
                }
                Console.Write(item);
                if (wordFound) // reset color
                {
                    Console.BackgroundColor = ConsoleColor.Black;
                    Console.ForegroundColor = ConsoleColor.Gray;
                    wordFound = false;


                }

                Console.Write(" ");
            }

//Sets the cursor to my found results
foreach (var item in foundWordPositions)
        {
            Console.SetCursorPosition(item.CursorLeftPosition, item.CursorTopPosition);
            Console.ReadLine();
        }

【问题讨论】:

    标签: c# console max min


    【解决方案1】:

    这将是一种 hacky 方式来实现您正在寻找的东西,但我不确定如何实现这一目标:

      private void CheckAndResetWindowSize(){
          if(Console.WindowHeight != 200|| Console.WindowWidth != 400) {
              Console.SetWindowSize(400, 200);
          }
      }
    

    这将允许用户能够编辑窗口的大小,但它会自动调整回原来的大小。

    让我知道这是否有帮助。

    【讨论】:

    • 我理论上需要一个事件来执行这个方法并且控制台没有事件。
    【解决方案2】:

    您可以使用 user32-API 中的 DeleteMenu() 函数来禁用调整控制台窗口的大小。检查thisthis 链接以了解如何使用它。您的问题的完整解决方案可以在here找到。

    【讨论】:

      猜你喜欢
      • 2021-07-23
      • 1970-01-01
      • 1970-01-01
      • 2019-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多