c# - 如何将 C# 控制台应用程序窗口移动到屏幕中央

标签 c# windows console-application window-position netcoreapp3.1

我正在使用 C# 控制台应用程序。如何将应用程序窗口移动到屏幕中央?

其他详细信息

框架版本:.NET Core 3.1
窗口大小: 37x20 (Console.SetWindowSize(37, 20);)

最佳答案

制作了一个小型实用程序类,可让您将控制台窗口居中。

使用示例:

WindowUtility.MoveWindowToCenter();

完整源代码:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

static class WindowUtility
{
    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll", SetLastError = true)]
    static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

    const uint SWP_NOSIZE = 0x0001;
    const uint SWP_NOZORDER = 0x0004;

    private static Size GetScreenSize() => new Size(GetSystemMetrics(0), GetSystemMetrics(1));

    private struct Size
    {
        public int Width { get; set; }
        public int Height { get; set; }

        public Size(int width, int height)
        {
            Width = width;
            Height = height;
        }
    }

    [DllImport("User32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
    private static extern int GetSystemMetrics(int nIndex);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool GetWindowRect(HandleRef hWnd, out Rect lpRect);

    [StructLayout(LayoutKind.Sequential)]
    private struct Rect
    {
        public int Left;        // x position of upper-left corner
        public int Top;         // y position of upper-left corner
        public int Right;       // x position of lower-right corner
        public int Bottom;      // y position of lower-right corner
    }

    private static Size GetWindowSize(IntPtr window)
    {
        if (!GetWindowRect(new HandleRef(null, window), out Rect rect))
            throw new Exception("Unable to get window rect!");

        int width = rect.Right - rect.Left;
        int height = rect.Bottom - rect.Top;

        return new Size(width, height);
    }

    public static void MoveWindowToCenter()
    {
        IntPtr window = Process.GetCurrentProcess().MainWindowHandle;

        if (window == IntPtr.Zero)
            throw new Exception("Couldn't find a window to center!");

        Size screenSize = GetScreenSize();
        Size windowSize = GetWindowSize(window);

        int x = (screenSize.Width - windowSize.Width) / 2;
        int y = (screenSize.Height - windowSize.Height) / 2;

        SetWindowPos(window, IntPtr.Zero, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
    }
}

引用资料:

一堆 StackOverflow 帖子帮助我整合了这个解决方案。 https://stackoverflow.com/a/42306412/5946094
https://stackoverflow.com/a/13547659/5946094
https://stackoverflow.com/a/43793468/5946094
https://stackoverflow.com/a/31273557/5946094

关于c# - 如何将 C# 控制台应用程序窗口移动到屏幕中央,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67008500/

相关文章:

c# - PDFsharp 文档

windows - 命令提示随机数是如何生成的?

c# - 控制台应用程序 (C#) 在交互时卡住

c# - EF 4.3 CodeFirst MVC3 WebApp 和控制台使用相同的模型,但不知何故它们 'see' 是导致模型支持错误的不同模型

c++ - 这种加法有什么问题?

c# - 帮助组合框背后的 Action /事件

c# - Entity Framework : Skip/Take functionality

c# - QueryOver 上的 GroupBy SqlFunction

windows - 如何使用脚本附加到 Path 环境变量

windows - 在 Windows 2008 Server 上将桌面快捷方式添加到 "My Computer"