System.Console.SetWindowPosition 方法

方法描述

设置控制台窗口相对于屏幕缓冲区的位置。

语法定义(C# System.Console.SetWindowPosition 方法 的用法)

public static void SetWindowPosition(
	int left,
	int top
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
left System-Int32 控制台窗口左上角的列位置。
top System-Int32 控制台窗口左上角的行位置。
返回值 void

提示和注释

操作系统窗口显示控制台窗口,而控制台窗口显示屏幕缓冲区的一部分。 SetWindowPosition 方法影响控制台窗口相对于屏幕缓冲区的位置,但不影响操作系统窗口相对于桌面的位置。

控制台窗口和操作系统窗口通常互不影响。 但是,如果屏幕缓冲区无法显示在控制台窗口的当前边界内,操作系统会自动在操作系统窗口中添加滚动条。 这样,移动操作系统窗口滚动条就会影响控制台窗口的位置,而通过 SetWindowPosition 方法移动控制台窗口会影响操作系统窗口滚动条的位置。

System.Console.SetWindowPosition 方法例子

网格模式有助于查看控制台窗口相对于屏幕缓冲区的移动。

// This example demonstrates the Console.WindowLeft and
//                               Console.WindowTop properties.
using System;
using System.Text;
using System.IO;
//
class Sample 
{
    public static int saveBufferWidth;
    public static int saveBufferHeight;
    public static int saveWindowHeight;
    public static int saveWindowWidth;
    public static bool saveCursorVisible;
//
    public static void Main() 
    {
    string m1 = "1) Press the cursor keys to move the console window.\n" +
                "2) Press any key to begin. When you're finished...\n" +
                "3) Press the Escape key to quit.";
    string g1 = "+----";
    string g2 = "|    ";
    string grid1;
    string grid2;
    StringBuilder sbG1 = new StringBuilder();
    StringBuilder sbG2 = new StringBuilder();
    ConsoleKeyInfo cki;
    int y;
//
    try 
    {
    saveBufferWidth  = Console.BufferWidth;
    saveBufferHeight = Console.BufferHeight;
    saveWindowHeight = Console.WindowHeight;
    saveWindowWidth  = Console.WindowWidth;
    saveCursorVisible = Console.CursorVisible;
//
    Console.Clear();
    Console.WriteLine(m1);
    Console.ReadKey(true);

// Set the smallest possible window size before setting the buffer size.
    Console.SetWindowSize(1, 1);
    Console.SetBufferSize(80, 80);
    Console.SetWindowSize(40, 20);

// Create grid lines to fit the buffer. (The buffer width is 80, but
// this same technique could be used with an arbitrary buffer width.)
    for (y = 0; y < Console.BufferWidth/g1.Length; y++)
        {
        sbG1.Append(g1);
        sbG2.Append(g2);
        }
    sbG1.Append(g1, 0, Console.BufferWidth%g1.Length);
    sbG2.Append(g2, 0, Console.BufferWidth%g2.Length);
    grid1 = sbG1.ToString();
    grid2 = sbG2.ToString();

    Console.CursorVisible = false;
    Console.Clear();
    for (y = 0; y < Console.BufferHeight-1; y++)
        {
        if (y%3 == 0)
            Console.Write(grid1);
        else
            Console.Write(grid2);
        }

    Console.SetWindowPosition(0, 0);
    do
        {
        cki = Console.ReadKey(true);
        switch (cki.Key) 
            {
            case ConsoleKey.LeftArrow:
                if (Console.WindowLeft > 0) 
                    Console.SetWindowPosition(
                            Console.WindowLeft-1, Console.WindowTop);
                break;
            case ConsoleKey.UpArrow:
                if (Console.WindowTop > 0) 
                    Console.SetWindowPosition(
                            Console.WindowLeft, Console.WindowTop-1);
                break;
            case ConsoleKey.RightArrow:
                if (Console.WindowLeft < (Console.BufferWidth-Console.WindowWidth)) 
                    Console.SetWindowPosition(
                            Console.WindowLeft+1, Console.WindowTop);
                break;
            case ConsoleKey.DownArrow:
                if (Console.WindowTop < (Console.BufferHeight-Console.WindowHeight)) 
                    Console.SetWindowPosition(
                            Console.WindowLeft, Console.WindowTop+1);
                break;
            }
        } 
    while (cki.Key != ConsoleKey.Escape);  // end do-while
    } // end try
    catch (IOException e) 
        {
        Console.WriteLine(e.Message);
        }
    finally 
        {
        Console.Clear();
        Console.SetWindowSize(1, 1);
        Console.SetBufferSize(saveBufferWidth, saveBufferHeight);
        Console.SetWindowSize(saveWindowWidth, saveWindowHeight);
        Console.CursorVisible = saveCursorVisible;
        }
    } // end Main
} // end Sample
/*
This example produces results similar to the following:

1) Press the cursor keys to move the console window.
2) Press any key to begin. When you're finished...
3) Press the Escape key to quit.

...

+----+----+----+-
|    |    |    |
|    |    |    |
+----+----+----+-
|    |    |    |
|    |    |    |
+----+----+----+-

*/

异常

异常 异常描述
ArgumentOutOfRangeException
  • left 或 top 小于零。
  • left + WindowWidth 大于 BufferWidth。
  • top + WindowHeight 大于 BufferHeight。
SecurityException 该用户没有执行此操作的权限。
IOException 发生了 I/O 错误。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。