英語で読む

次の方法で共有


Console.SetWindowPosition(Int32, Int32) メソッド

定義

画面バッファーに対するコンソール ウィンドウの相対位置を設定します。

C#
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public static void SetWindowPosition (int left, int top);
C#
public static void SetWindowPosition (int left, int top);

パラメーター

left
Int32

コンソール ウィンドウの左上隅の列位置。

top
Int32

コンソール ウィンドウの左上隅の行位置。

属性

例外

left または top が 0 未満です。

または

left + WindowWidthBufferWidth より大きくなっています。

または

top + WindowHeightBufferHeight より大きくなっています。

ユーザーにこの操作を実行するアクセス許可がありません。

I/O エラーが発生しました。

現在のオペレーティング システムは Windows ではありません。

次の例では、、、WindowWidthWindowHeight、、BufferWidthBufferHeightおよびCursorVisibleプロパティ、および 、、SetBufferSizeおよびReadKeyメソッドをSetWindowPositionWindowLeftWindowTopします。 この例では、画面バッファーの幅に基づいて画面バッファーにグリッド パターンを描画します。 次に、上方向キー、下方向キー、左方向キー、または右方向キーのどちらが押されたかに応じて、コンソール ウィンドウを移動します。 グリッド パターンは、画面バッファーに対するコンソール ウィンドウの動きを確認するのに役立ちます。

C#
// 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.

...

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

*/

注釈

オペレーティング システム ウィンドウにコンソール ウィンドウが表示され、コンソール ウィンドウに画面バッファーの一部が表示されます。 このメソッドは SetWindowPosition 、画面バッファーに対するコンソール ウィンドウの位置に影響しますが、デスクトップに対するオペレーティング システム ウィンドウの位置には影響しません。

通常、コンソールとオペレーティング システムのウィンドウは相互に影響しません。 ただし、コンソール ウィンドウの現在の境界に画面バッファーを表示できない場合、オペレーティング システムはオペレーティング システム ウィンドウにスクロール バーを自動的に追加します。 その場合、オペレーティング システム ウィンドウのスクロール バーの移動はコンソール ウィンドウの位置に影響し、メソッドを使用 SetWindowPosition してコンソール ウィンドウを移動すると、オペレーティング システム ウィンドウのスクロール バーの位置に影響します。

適用対象

製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1