gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 22:10:36 25 4
gpt4 key购买 nike

正如标题所说,我想把它放在屏幕的左下角。这是我到目前为止的代码:

    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.WindowLeftConsole.WindowTopSystem.Console class 不会改变窗口在屏幕上的位置
相反,它们相对于(可能更大的)窗口 缓冲区 定位窗口的可见部分 - 您不能使用类型 System.Console 来更改控制台的位置屏幕上的窗口 - 您需要为此使用 Windows API


以下是完整控制台应用程序的代码,它将自己的窗口定位在屏幕的左下角,遵循任务栏的位置.

注意:

  • 应该与多显示器设置一起工作 - 将窗口定位在它(主要)显示的特定显示器(显示器、屏幕)上 - 但我没有亲自验证

  • 仅通过 P/Invoke 声明使用 Windows API 函数,避免引用 WinForms 程序集 (System.Windows.Forms),这是控制台应用程序通常不需要。

    • 您会看到很大一部分代码专门用于 P/Invoke 签名(声明)以与 native Windows API 交互;这些是感激地改编自 pinvoke.net

    • 相比之下,Main() 方法中的实际代码比较短。

  • 如果您从 Visual Studio 中的控制台应用程序项目编译以下代码,并从 cmd.exe 控制台窗口(命令提示符)运行生成的可执行文件,该控制台窗口应该移动到(包含屏幕)的左下角。

    • 要在从 Visual Studio 运行时验证功能,请在关闭的 处放置一个断点,并在执行暂停时,按 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);

}

}

关于c# - 在屏幕左下角放置一个小的控制台窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27715004/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com