You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现控制台全屏(Alt+Enter效果)?求对应代码方案

实现控制台真正全屏(Alt+Enter触发)

我之前也碰到过这个需求——普通的窗口最大化API确实达不到真正的全屏效果,它只是把窗口拉到屏幕边缘,但任务栏还会显示。要实现覆盖整个屏幕(包括任务栏)的全屏控制台,得借助Windows原生Win32 API直接操作控制台窗口的属性,下面是具体实现方案:

C# 实现代码

using System;
using System.Runtime.InteropServices;

class FullScreenConsole
{
    // 导入Win32核心API函数
    [DllImport("user32.dll")]
    private static extern IntPtr GetConsoleWindow();

    [DllImport("user32.dll")]
    private static extern int GetWindowLongPtr(IntPtr hWnd, int nIndex);

    [DllImport("user32.dll")]
    private static extern int SetWindowLongPtr(IntPtr hWnd, int nIndex, int dwNewLong);

    [DllImport("user32.dll")]
    private static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFO lpmi);

    [DllImport("user32.dll")]
    private static extern IntPtr MonitorFromWindow(IntPtr hWnd, uint dwFlags);

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

    [DllImport("user32.dll")]
    private static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);

    // 定义API所需常量
    private const int GWL_STYLE = -16;
    private const int WS_OVERLAPPEDWINDOW = 0x00CF0000;
    private const int WS_VISIBLE = 0x10000000;
    private const uint MONITOR_DEFAULTTOPRIMARY = 0x00000001;
    private const uint SWP_FRAMECHANGED = 0x0040;

    // 定义API所需结构体
    [StructLayout(LayoutKind.Sequential)]
    private struct MONITORINFO
    {
        public int cbSize;
        public RECT rcMonitor;
        public RECT rcWork;
        public uint dwFlags;
    }

    [StructLayout(LayoutKind.Sequential)]
    private struct RECT
    {
        public int left;
        public int top;
        public int right;
        public int bottom;
    }

    // 保存窗口原始状态的变量
    private static bool _isFullScreen = false;
    private static int _originalWindowStyle;
    private static RECT _originalWindowRect;

    static void Main(string[] args)
    {
        Console.WriteLine("按Alt+Enter切换全屏/窗口模式");
        Console.WriteLine("按任意键退出");

        while (true)
        {
            if (Console.KeyAvailable)
            {
                var key = Console.ReadKey(true);
                if (key.Modifiers == ConsoleModifiers.Alt && key.Key == ConsoleKey.Enter)
                {
                    ToggleFullScreen();
                }
                else
                {
                    break;
                }
            }
        }
    }

    private static void ToggleFullScreen()
    {
        IntPtr consoleWindow = GetConsoleWindow();

        if (!_isFullScreen)
        {
            // 保存原始窗口样式和位置,方便后续恢复
            _originalWindowStyle = GetWindowLongPtr(consoleWindow, GWL_STYLE);
            GetWindowRect(consoleWindow, ref _originalWindowRect);

            // 获取主显示器的完整屏幕区域(包含任务栏)
            MONITORINFO monitorInfo = new MONITORINFO();
            monitorInfo.cbSize = Marshal.SizeOf(monitorInfo);
            IntPtr primaryMonitor = MonitorFromWindow(consoleWindow, MONITOR_DEFAULTTOPRIMARY);
            GetMonitorInfo(primaryMonitor, ref monitorInfo);

            // 移除窗口标题栏、边框,设置为全屏尺寸
            SetWindowLongPtr(consoleWindow, GWL_STYLE, WS_VISIBLE);
            SetWindowPos(consoleWindow, IntPtr.Zero,
                monitorInfo.rcMonitor.left, monitorInfo.rcMonitor.top,
                monitorInfo.rcMonitor.right - monitorInfo.rcMonitor.left,
                monitorInfo.rcMonitor.bottom - monitorInfo.rcMonitor.top,
                SWP_FRAMECHANGED);
        }
        else
        {
            // 恢复原始窗口样式和位置
            SetWindowLongPtr(consoleWindow, GWL_STYLE, _originalWindowStyle);
            SetWindowPos(consoleWindow, IntPtr.Zero,
                _originalWindowRect.left, _originalWindowRect.top,
                _originalWindowRect.right - _originalWindowRect.left,
                _originalWindowRect.bottom - _originalWindowRect.top,
                SWP_FRAMECHANGED);
        }

        _isFullScreen = !_isFullScreen;
    }
}

代码核心逻辑说明

  1. 获取控制台窗口句柄:通过GetConsoleWindow()拿到控制台窗口的核心操作入口;
  2. 保存原始状态:切换全屏前记录窗口的原始样式和位置,确保恢复时能回到之前的状态;
  3. 获取全屏区域:用MonitorFromWindow和GetMonitorInfo拿到主显示器的完整屏幕范围(注意用rcMonitor而非rcWork,后者是排除任务栏的工作区);
  4. 切换全屏/窗口:修改窗口样式移除边框和标题栏,同时将窗口尺寸设置为完整屏幕大小;恢复时还原之前保存的状态即可。

C++ 原生实现代码(如果偏好C++)

#include <windows.h>
#include <iostream>
#include <conio.h>

bool isFullScreen = false;
LONG originalWindowStyle;
RECT originalWindowRect;

void ToggleFullScreen()
{
    HWND consoleWindow = GetConsoleWindow();

    if (!isFullScreen)
    {
        // 保存原始窗口状态
        originalWindowStyle = GetWindowLongPtr(consoleWindow, GWL_STYLE);
        GetWindowRect(consoleWindow, &originalWindowRect);

        // 获取主显示器完整屏幕区域
        MONITORINFO monitorInfo;
        monitorInfo.cbSize = sizeof(MONITORINFO);
        HMONITOR primaryMonitor = MonitorFromWindow(consoleWindow, MONITOR_DEFAULTTOPRIMARY);
        GetMonitorInfo(primaryMonitor, &monitorInfo);

        // 设置全屏样式与尺寸
        SetWindowLongPtr(consoleWindow, GWL_STYLE, WS_VISIBLE);
        SetWindowPos(consoleWindow, NULL,
            monitorInfo.rcMonitor.left, monitorInfo.rcMonitor.top,
            monitorInfo.rcMonitor.right - monitorInfo.rcMonitor.left,
            monitorInfo.rcMonitor.bottom - monitorInfo.rcMonitor.top,
            SWP_FRAMECHANGED);
    }
    else
    {
        // 恢复原始窗口状态
        SetWindowLongPtr(consoleWindow, GWL_STYLE, originalWindowStyle);
        SetWindowPos(consoleWindow, NULL,
            originalWindowRect.left, originalWindowRect.top,
            originalWindowRect.right - originalWindowRect.left,
            originalWindowRect.bottom - originalWindowRect.top,
            SWP_FRAMECHANGED);
    }

    isFullScreen = !isFullScreen;
}

int main()
{
    std::cout << "按Alt+Enter切换全屏/窗口模式" << std::endl;
    std::cout << "按任意键退出" << std::endl;

    while (true)
    {
        if (_kbhit())
        {
            int key = _getch();
            if (key == 0 || key == 0xE0) // 处理组合键的扩展码
            {
                int secondKey = _getch();
                if (secondKey == 0x1C) // Enter键的扩展码
                {
                    // 检查Alt键是否处于按下状态
                    if (GetAsyncKeyState(VK_MENU) & 0x8000)
                    {
                        ToggleFullScreen();
                    }
                }
            }
            else
            {
                break;
            }
        }
    }

    return 0;
}

注意事项

  • 这段代码仅适用于Windows系统,因为依赖Win32 API;
  • 普通控制台程序默认拥有修改窗口属性的权限,无需额外提权;
  • 如果用Python等其他语言,也可以通过ctypes库调用相同的Win32 API,核心思路完全一致。

内容的提问来源于stack exchange,提问作者user5659664

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:24:51