Вы можете использовать Console.WindowTop
и Console.WindowWidth
класса System.Console
для установки местоположения окна консоли.
Здесь - пример на MSDN
Свойство BufferHeight
и BufferWidth
получает / задает количество строк и столбцов для отображения.
WindowHeight
и WindowWidth
должны быть всегда меньше BufferHeight
и BufferWidth
соответственно.
WindowLeft
должно быть меньше BufferWidth - WindowWidth
и WindowTop
должно быть меньше BufferHeight - WindowHeight
.
WindowLeft
и WindowTop
относятся к буфере.
Чтобы переместить фактическое окно консоли, этот статья имеет хороший пример.
Я использовал некоторые из вашего кода и кода из примера CodeProject. Вы можете установить местоположение и размер окна как в одной функции. Не нужно снова устанавливать Console.WindowHeight
и Console.WindowWidth
. Вот как выглядит мой класс:
class Program
{
const int SWP_NOZORDER = 0x4;
const int SWP_NOACTIVATE = 0x10;
[DllImport("kernel32")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter,
int x, int y, int cx, int cy, int flags);
static void Main(string[] args)
{
Console.WindowWidth = 50;
Console.WindowHeight = 3;
Console.BufferWidth = 50;
Console.BufferHeight = 3;
Console.BackgroundColor = ConsoleColor.Black;
Console.ForegroundColor = ConsoleColor.DarkMagenta;
var screen = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
var width = screen.Width;
var height = screen.Height;
SetWindowPosition(100, height - 300, 500, 100);
Console.Title = "My Title";
Console.WriteLine("");
Console.Write(" Press any key to close this window ...");
Console.ReadKey();
}
///
/// Sets the console window location and size in pixels
///
public static void SetWindowPosition(int x, int y, int width, int height)
{
SetWindowPos(Handle, IntPtr.Zero, x, y, width, height, SWP_NOZORDER | SWP_NOACTIVATE);
}
public static IntPtr Handle
{
get
{
//Initialize();
return GetConsoleWindow();
}
}
}