Console.SetCursorPosition メソッド
アセンブリ: mscorlib (mscorlib.dll 内)



コンソール ウィンドウにおける次の書き込み操作の開始位置を指定するには、SetCursorPosition メソッドを使用します。指定されたカーソル位置がコンソール ウィンドウで現在表示されている領域の外にある場合、カーソルが表示領域内に入るようにウィンドウの原点が自動的に変更されます。
1 つの文字がコンソール ウィンドウに出力されるたび、カーソルが自動的に次の文字位置に移動されます。カーソルがコンソール ウィンドウの右下に達した場合、次の書き込み操作時にコンソール ウィンドウがスクロールされ、カーソルが表示領域に来るように調整されます。右下の文字位置に出力する場合でも、コンソール ウィンドウがスクロールしないようにするには、MoveBufferArea メソッドを使用して、文字をその位置に移動します。

CursorLeft プロパティと CursorTop プロパティ、および SetCursorPosition メソッドと Clear メソッドの使用例を次に示します。この例では、次のテキストの書き込み位置となるカーソルを適宜配置することによって、"+"、"|"、"-" の文字列を使いながら 5 文字 × 5 文字の四角形を描画しています。他の文字列を組み合わせれば、より少ないステップ数で四角形を描画できます。
' This example demonstrates the ' Console.CursorLeft and ' Console.CursorTop properties, and the ' Console.SetCursorPosition and ' Console.Clear methods. Imports System 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 'WriteAt 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 'Main End Class 'Sample ' '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. 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. import 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.get_Message()); } } //WriteAt public static void main(String[] args) { // Clear the screen, then save the top and left coordinates. Console.Clear(); origRow = Console.get_CursorTop(); origCol = Console.get_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(); } //main } //Sample /* This example produces the following results: +---+ | | | | | | +---+ All done! */


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からConsole.SetCursorPosition メソッドを検索する場合は、下記のリンクをクリックしてください。

- Console.SetCursorPosition メソッドのページへのリンク