0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

cscの作法 その575

Posted at

概要

cscの作法、調べてみた。
コンソールアプリ、見つけたので、やってみた。

参考にしたページ

写真

image.png

サンプルコード




using System;
using System.Windows.Forms;

class program {
	static int ShowMenu(string title, string arrow, string[] items) {
		if (items == null || items.Length <= 0) 
			throw new ArgumentException("メニューの項目が指定されていません。");
		int idx = 0;
		int cursorTop = Math.Min(Console.CursorTop, Console.BufferHeight - Console.WindowHeight) + 1;
		bool isSelected = false;
		Console.CursorVisible = false;
		ScrollWindowTop();
		Console.WriteLine(title);
		do
		{
			Console.SetCursorPosition(0, cursorTop);
			for (int i = 0; i < items.Length; i++)
			{
				ClearCurrentCursorPosition();
				Console.WriteLine(i == idx ? String.Format("{0} {1}", arrow, items[i]) : String.Format(" {0}", items[i]));
			}
			switch (Console.ReadKey(true).Key)
			{
			case ConsoleKey.UpArrow:
				if (idx > 0) 
					idx--;
			break;
			case ConsoleKey.DownArrow:
				if (idx < items.Length - 1) 
					idx++;
			break;
			case ConsoleKey.Escape:
				idx = - 1;
				isSelected = true;
			break;
			case ConsoleKey.Enter:
				isSelected = true;
			break;
			}
		} while (!isSelected);
		Console.CursorVisible = true;
		return idx;
	}
	static void ScrollWindowTop() {
		int windowHeight = Console.WindowHeight - 1;
		for (int i = 0; i < windowHeight; i++) 
			Console.WriteLine();
		Console.SetWindowPosition(0, 0);
		Console.SetCursorPosition(0, 0);
	}
	static void ClearCurrentCursorPosition() {
		Console.SetCursorPosition(0, Console.CursorTop);
		Console.Write(" ".PadRight(Console.BufferWidth));
		Console.SetCursorPosition(0, Console.CursorTop);
	}
	[STAThread()]
	static void Main() {
		Console.WriteLine("プログラム ランチャー");
		string[] items = new string[] { 
			"chrome  ", 
			"メモ帳  ", 
			"電卓  ", 
			"スケッチアップ  ", 
			"エクスプローラー  "
		};
		int result = ShowMenu("▼ 項目を選択してください。", "→", items);
		Console.WriteLine();
		Console.WriteLine(result < 0 ? "▼ キャンセルされました。" : String.Format("{0} が選択されました。", items[result]));
		MessageBox.Show(result.ToString());
	}
}





以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?