Console.SetCursorPosition(Int32, Int32) 메서드

정의

커서의 위치를 설정합니다.

public:
 static void SetCursorPosition(int left, int top);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static void SetCursorPosition (int left, int top);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
[System.Runtime.Versioning.UnsupportedOSPlatform("android")]
[System.Runtime.Versioning.UnsupportedOSPlatform("ios")]
[System.Runtime.Versioning.UnsupportedOSPlatform("tvos")]
public static void SetCursorPosition (int left, int top);
public static void SetCursorPosition (int left, int top);
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
static member SetCursorPosition : int * int -> unit
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("android")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("ios")>]
[<System.Runtime.Versioning.UnsupportedOSPlatform("tvos")>]
static member SetCursorPosition : int * int -> unit
static member SetCursorPosition : int * int -> unit
Public Shared Sub SetCursorPosition (left As Integer, top As Integer)

매개 변수

left
Int32

커서의 열 위치입니다. 열은 0부터 시작하여 왼쪽부터 오른쪽까지 번호가 지정됩니다.

top
Int32

커서의 행 위치입니다. 행은 0부터 시작하여 위에서 아래로 번호가 지정됩니다.

특성

예외

left 또는 top가 0보다 작습니다.

또는

leftBufferWidth보다 크거나 같습니다.

또는

topBufferHeight보다 크거나 같은 경우

사용자에게 이 작업을 수행할 권한이 없습니다.

I/O 오류가 발생했습니다.

예제

이 예제에서는 CursorLeft 속성 및 CursorTop 메서드와 SetCursorPosition Clear 속성을 보여 줍니다. 이 예제에서는 "+", "|" 및 "-" 문자열의 조합을 사용하여 5자 x 5자 직사각형을 그리기 위해 다음 쓰기가 발생할 위치를 결정하는 커서를 배치합니다. 다른 문자열의 조합을 사용하여 더 적은 수의 단계로 사각형을 그릴 수 있습니다.

// This example demonstrates the 
//     Console.CursorLeft and 
//     Console.CursorTop properties, and the
//     Console.SetCursorPosition and 
//     Console.Clear methods.
using namespace System;
int origRow;
int origCol;
void WriteAt( String^ s, int x, int y )
{
   try
   {
      Console::SetCursorPosition( origCol + x, origRow + y );
      Console::Write( s );
   }
   catch ( ArgumentOutOfRangeException^ e ) 
   {
      Console::Clear();
      Console::WriteLine( e->Message );
   }

}

int main()
{
   
   // Clear the screen, then save the top and left coordinates.
   Console::Clear();
   origRow = Console::CursorTop;
   origCol = Console::CursorLeft;
   
   // Draw the left side of a 5x5 rectangle, from top to bottom.
   WriteAt( "+", 0, 0 );
   WriteAt( "|", 0, 1 );
   WriteAt( "|", 0, 2 );
   WriteAt( "|", 0, 3 );
   WriteAt( "+", 0, 4 );
   
   // Draw the bottom side, from left to right.
   WriteAt( "-", 1, 4 ); // shortcut: WriteAt("---", 1, 4)
   WriteAt( "-", 2, 4 ); // ...
   WriteAt( "-", 3, 4 ); // ...
   WriteAt( "+", 4, 4 );
   
   // Draw the right side, from bottom to top.
   WriteAt( "|", 4, 3 );
   WriteAt( "|", 4, 2 );
   WriteAt( "|", 4, 1 );
   WriteAt( "+", 4, 0 );
   
   // Draw the top side, from right to left.
   WriteAt( "-", 3, 0 ); // shortcut: WriteAt("---", 1, 0)
   WriteAt( "-", 2, 0 ); // ...
   WriteAt( "-", 1, 0 ); // ...
   
   //
   WriteAt( "All done!", 0, 6 );
   Console::WriteLine();
}

/*
This example produces the following results:

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

All done!

*/
// This example demonstrates the
//     Console.CursorLeft and
//     Console.CursorTop properties, and the
//     Console.SetCursorPosition and
//     Console.Clear methods.
using System;

class Sample
{
    protected static int origRow;
    protected static int origCol;

    protected static void WriteAt(string s, int x, int y)
    {
    try
        {
        Console.SetCursorPosition(origCol+x, origRow+y);
        Console.Write(s);
        }
    catch (ArgumentOutOfRangeException e)
        {
        Console.Clear();
        Console.WriteLine(e.Message);
        }
    }

    public static void Main()
    {
// Clear the screen, then save the top and left coordinates.
    Console.Clear();
    origRow = Console.CursorTop;
    origCol = Console.CursorLeft;

// Draw the left side of a 5x5 rectangle, from top to bottom.
    WriteAt("+", 0, 0);
    WriteAt("|", 0, 1);
    WriteAt("|", 0, 2);
    WriteAt("|", 0, 3);
    WriteAt("+", 0, 4);

// Draw the bottom side, from left to right.
    WriteAt("-", 1, 4); // shortcut: WriteAt("---", 1, 4)
    WriteAt("-", 2, 4); // ...
    WriteAt("-", 3, 4); // ...
    WriteAt("+", 4, 4);

// Draw the right side, from bottom to top.
    WriteAt("|", 4, 3);
    WriteAt("|", 4, 2);
    WriteAt("|", 4, 1);
    WriteAt("+", 4, 0);

// Draw the top side, from right to left.
    WriteAt("-", 3, 0); // shortcut: WriteAt("---", 1, 0)
    WriteAt("-", 2, 0); // ...
    WriteAt("-", 1, 0); // ...
//
    WriteAt("All done!", 0, 6);
    Console.WriteLine();
    }
}
/*
This example produces the following results:

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

All done!

*/
// This example demonstrates the
//     Console.CursorLeft and
//     Console.CursorTop properties, and the
//     Console.SetCursorPosition and
//     Console.Clear methods.
open System

// Clear the screen, then save the top and left coordinates.
Console.Clear()
let origRow = Console.CursorTop
let origCol = Console.CursorLeft

let writeAt s x y =
    try
        Console.SetCursorPosition(origCol + x, origRow + y)
        printfn $"%s{s}"
    with :? ArgumentOutOfRangeException as e ->
        Console.Clear()
        printfn $"{e.Message}"

// Draw the left side of a 5x5 rectangle, from top to bottom.
writeAt "+" 0 0
writeAt "|" 0 1
writeAt "|" 0 2
writeAt "|" 0 3
writeAt "+" 0 4

// Draw the bottom side, from left to right.
writeAt "-" 1 4 // shortcut: writeAt "---", 1, 4)
writeAt "-" 2 4 // ...
writeAt "-" 3 4 // ...
writeAt "+" 4 4

// Draw the right side, from bottom to top.
writeAt "|" 4 3
writeAt "|" 4 2
writeAt "|" 4 1
writeAt "+" 4 0

// Draw the top side, from right to left.
writeAt "-" 3 0 // shortcut: writeAt "---", 1, 0)
writeAt "-" 2 0 // ...
writeAt "-" 1 0 // ...

writeAt "All done!" 0 6
printfn ""


// This example produces the following results:
//
// +---+
// |   |
// |   |
// |   |
// +---+
//
// All done!
' This example demonstrates the 
'     Console.CursorLeft and 
'     Console.CursorTop properties, and the
'     Console.SetCursorPosition and 
'     Console.Clear methods.
Class Sample
   Protected Shared origRow As Integer
   Protected Shared origCol As Integer
   
   Protected Shared Sub WriteAt(s As String, x As Integer, y As Integer)
      Try
         Console.SetCursorPosition(origCol + x, origRow + y)
         Console.Write(s)
      Catch e As ArgumentOutOfRangeException
         Console.Clear()
         Console.WriteLine(e.Message)
      End Try
   End Sub
   
   Public Shared Sub Main()
      ' Clear the screen, then save the top and left coordinates.
      Console.Clear()
      origRow = Console.CursorTop
      origCol = Console.CursorLeft
      
      ' Draw the left side of a 5x5 rectangle, from top to bottom.
      WriteAt("+", 0, 0)
      WriteAt("|", 0, 1)
      WriteAt("|", 0, 2)
      WriteAt("|", 0, 3)
      WriteAt("+", 0, 4)
      
      ' Draw the bottom side, from left to right.
      WriteAt("-", 1, 4) ' shortcut: WriteAt("---", 1, 4)
      WriteAt("-", 2, 4) ' ...
      WriteAt("-", 3, 4) ' ...
      WriteAt("+", 4, 4)
      
      ' Draw the right side, from bottom to top.
      WriteAt("|", 4, 3)
      WriteAt("|", 4, 2)
      WriteAt("|", 4, 1)
      WriteAt("+", 4, 0)
      
      ' Draw the top side, from right to left.
      WriteAt("-", 3, 0) ' shortcut: WriteAt("---", 1, 0)
      WriteAt("-", 2, 0) ' ...
      WriteAt("-", 1, 0) ' ...
      '
      WriteAt("All done!", 0, 6)
      Console.WriteLine()
   End Sub
End Class
'
'This example produces the following results:
'
'+---+
'|   |
'|   |
'|   |
'+---+
'
'All done!
'

설명

이 메서드를 SetCursorPosition 사용하여 콘솔 창에서 다음 쓰기 작업을 시작할 위치를 지정합니다. 지정된 커서 위치가 현재 콘솔 창에 표시되는 영역 밖에 있으면 창 원본이 자동으로 변경되어 커서가 표시됩니다.

콘솔 창에 문자를 쓸 때마다 커서가 자동으로 다음 문자 위치로 이동합니다. 커서가 콘솔 창의 오른쪽 아래 문자 위치에 있는 경우 다음 쓰기 작업을 수행하면 콘솔 창이 스크롤되어 커서가 계속 표시됩니다. 콘솔 창이 스크롤되도록 하지 않고 오른쪽 아래 문자 위치에 문자를 쓰려면 메서드를 사용하여 MoveBufferArea 문자를 해당 위치로 이동합니다.

적용 대상