正如标题所说,我想把它放在屏幕的左下角。这是我到目前为止的代码:
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()
答案 0 :(得分:13)
注意:尽管名称相同,设置System.Console
class的Console.WindowLeft
和Console.WindowTop
不会更改窗口的屏幕上的位置。
相反,他们将窗口的可见部分相对于(可能更大的)窗口缓冲区定位 - 您不能使用类型System.Console
来更改控制台窗口在屏幕上的位置 - 您需要使用Windows API 。
以下是完整控制台应用程序的代码,将自己的窗口放置在屏幕的左下角,尊重任务栏的位置 < /强>
注意:
应该使用多显示器设置 - 将窗口定位在(大部分)显示的特定显示器(显示器,屏幕)上 - 但我还没有亲自验证了它。
仅通过P / Invoke声明使用Windows API函数,从而无需引用WinForms程序集(System.Windows.Forms
),这在中通常不需要控制台应用程序。
您将看到代码的很大一部分用于P / Invoke签名(声明),以便与本机Windows API连接;这些都是从pinvoke.net
相比较,Main()
方法中的实际代码较短。
如果从Visual Studio中的控制台应用程序项目编译以下代码并从cmd.exe
控制台窗口(命令提示符)运行生成的可执行文件,则该控制台窗口应移至左下角(包含屏幕)。
}
处放置一个断点,并在执行暂停时,将Alt-Tab置于控制台窗口以验证其位置。using System;
using System.Runtime.InteropServices; // To enable P/Invoke signatures.
public static class PositionConsoleWindowDemo
{
// P/Invoke declarations.
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
static extern IntPtr MonitorFromWindow(IntPtr hwnd, uint dwFlags);
const int MONITOR_DEFAULTTOPRIMARY = 1;
[DllImport("user32.dll")]
static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFO lpmi);
[StructLayout(LayoutKind.Sequential)]
struct MONITORINFO
{
public uint cbSize;
public RECT rcMonitor;
public RECT rcWork;
public uint dwFlags;
public static MONITORINFO Default
{
get { var inst= new MONITORINFO(); inst.cbSize = (uint)Marshal.SizeOf(inst); return inst; }
}
}
[StructLayout(LayoutKind.Sequential)]
struct RECT
{
public int Left, Top, Right, Bottom;
}
[StructLayout(LayoutKind.Sequential)]
struct POINT
{
public int x, y;
}
[DllImport("user32.dll", SetLastError = true)]
static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
[DllImport("user32.dll", SetLastError = true)]
static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref WINDOWPLACEMENT lpwndpl);
const uint SW_RESTORE= 9;
[StructLayout(LayoutKind.Sequential)]
struct WINDOWPLACEMENT
{
public uint Length;
public uint Flags;
public uint ShowCmd;
public POINT MinPosition;
public POINT MaxPosition;
public RECT NormalPosition;
public static WINDOWPLACEMENT Default
{
get
{
var instance = new WINDOWPLACEMENT();
instance.Length = (uint) Marshal.SizeOf(instance);
return instance;
}
}
}
public static void Main()
{
// Get this console window's hWnd (window handle).
IntPtr hWnd = GetConsoleWindow();
// Get information about the monitor (display) that the window is (mostly) displayed on.
// The .rcWork field contains the monitor's work area, i.e., the usable space excluding
// the taskbar (and "application desktop toolbars" - see https://msdn.microsoft.com/en-us/library/windows/desktop/ms724947(v=vs.85).aspx)
var mi = MONITORINFO.Default;
GetMonitorInfo(MonitorFromWindow(hWnd, MONITOR_DEFAULTTOPRIMARY), ref mi);
// Get information about this window's current placement.
var wp = WINDOWPLACEMENT.Default;
GetWindowPlacement(hWnd, ref wp);
// Calculate the window's new position: lower left corner.
// !! Inexplicably, on W10, work-area coordinates (0,0) appear to be (7,7) pixels
// !! away from the true edge of the screen / taskbar.
int fudgeOffset = 7;
wp.NormalPosition = new RECT() {
Left = -fudgeOffset,
Top = mi.rcWork.Bottom - (wp.NormalPosition.Bottom - wp.NormalPosition.Top),
Right = (wp.NormalPosition.Right - wp.NormalPosition.Left),
Bottom = fudgeOffset + mi.rcWork.Bottom
};
// Place the window at the new position.
SetWindowPlacement(hWnd, ref wp);
}
}
答案 1 :(得分:0)
您可以使用Console.WindowTop
类的Console.WindowWidth
和System.Console
来设置控制台窗口的位置。
Here是MSDN上的一个示例
BufferHeight
和BufferWidth
属性获取/设置要显示的行数和列数。
WindowHeight
和WindowWidth
属性必须始终分别小于BufferHeight
和BufferWidth
。
WindowLeft
必须小于BufferWidth - WindowWidth
且WindowTop
必须小于BufferHeight - WindowHeight
。
WindowLeft
和WindowTop
相对于缓冲区。
要移动实际的控制台窗口,this文章有一个很好的例子。
我使用了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();
}
/// <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();
}
}
}
答案 2 :(得分:-2)