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.WindowTop和Console.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
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 是相对于缓冲区的.
WindowLeft and WindowTop are relative to the buffer.
要移动实际的控制台窗口,这篇文章有一个很好的例子.
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();
}
}
}
这篇关于在屏幕左下角放置一个小控制台窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在屏幕左下角放置一个小控制台窗口?
基础教程推荐
- 全局 ASAX - 获取服务器名称 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
