在屏幕的左下方放置一个小的控制台窗口? [英] Position a small console window to the bottom left of the screen?

查看:63
本文介绍了在屏幕的左下方放置一个小的控制台窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所述,我要将其放置在屏幕的左下角。这是我到目前为止的代码:

As the title says, I want to position it to the bottom left corner of the screen. Here's the code I have so far:

    Console.WindowWidth = 50
    Console.WindowHeight = 3
    Console.BufferWidth = 50
    Console.BufferHeight = 3
    Console.BackgroundColor = ConsoleColor.Black
    Console.ForegroundColor = ConsoleColor.DarkMagenta
    Console.Title = "My Title"
    Console.WriteLine("")
    Console.Write(" Press any key to close this window ...")

    Console.ReadKey()


推荐答案

您可以使用 Console.WindowTop 和<$ c $ System.Console 类的 Console.WindowWidth 来设置控制台窗口的位置。

You can use Console.WindowTop and Console.WindowWidth of the System.Console class to set the location of the console window.

此处是MSDN上的示例

BufferHeight BufferWidth 属性获取/设置要显示的行数和列数。

The BufferHeight and BufferWidth property gets/sets the number of rows and columns to be displayed.

WindowHeight WindowWidth 属性必须始终小于 BufferHeight BufferWidth

WindowHeight and WindowWidth properties must always be less than BufferHeight and BufferWidth respectively.

WindowLeft 必须小于 BufferWidth-WindowWidth WindowTop 必须小于 BufferHeight-WindowHeight

WindowLeft must be less than BufferWidth - WindowWidth and WindowTop must be less than BufferHeight - WindowHeight.

WindowLeft WindowTop 相对于缓冲区。

要移动实际的控制台窗口,请这篇文章就是一个很好的例子。

To move the actual console window, this article has a good example.

我已经使用了一些代码和来自CodeProject示例的代码。您可以在一个功能中同时设置窗口的位置和大小。无需再次设置 Console.WindowHeight Console.WindowWidth 。这是我班级的样子:

I have used some of your code and code from the CodeProject sample. You can set window location and size both in a single function. No need to set Console.WindowHeight and Console.WindowWidth again. This is how my class looks:

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();
    }


    /// <summary>
    /// Sets the console window location and size in pixels
    /// </summary>
    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();
        }
    }

}

这篇关于在屏幕的左下方放置一个小的控制台窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆