在屏幕左下角放置一个小控制台窗口?

Position a small console window to the bottom left of the screen?(在屏幕左下角放置一个小控制台窗口?)

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

问题描述

正如标题所说,我想将它定位在屏幕的左下角.这是我到目前为止的代码:

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

推荐答案

可以使用System.Console<的Console.WindowTopConsole.WindowWidth/code> 类来设置控制台窗口的位置.

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

这里是 MSDN 上的一个例子

Here is an example on MSDN

BufferHeightBufferWidth 属性获取/设置要显示的行数和列数.

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

WindowHeightWindowWidth 属性必须始终分别小于 BufferHeightBufferWidth.

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.

WindowLeftWindowTop 是相对于缓冲区的.

WindowLeft and WindowTop are relative to the buffer.

要移动实际的控制台窗口,这篇文章有一个很好的例子.

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

我使用了您的一些代码和 CodeProject 示例中的代码.您可以在一个函数中设置窗口位置和大小.无需再次设置 Console.WindowHeightConsole.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();
        }
    }

}

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

本文标题为:在屏幕左下角放置一个小控制台窗口?

基础教程推荐