C# full screen console?

2019-02-09 06:46发布

问题:

I have seen that Windows can switch to the very basic console interface when updating the video drivers and I have also seen programs like Borland C++ doing this.
I'd really like to know how to make this with a console application in C# (or VB.NET if you prefer), and I don't mind using P/Invoke's (and I bet I must!).

回答1:

In older versions of Windows you could put any console into full screen with Alt-Enter (if I remember correctly).

With the introduction of the Desktop Window Manager and full screen composition via the GPU in Vista that full screen console window function was removed.

(When updating the graphics driver the graphics subsystem is being reset, what you see isn't a console window, but the graphics card default startup into text mode.)



回答2:

Windows 7 does not support Full Screen console applications. On XP you can use SetConsoleDisplayMode, you will need to P/Invoke to this, but it is relatively simple. I know on win 7 x64 this function will fail with error 120 This function is not spported on this system

To get the console handle you can use some of the code from this answer.



回答3:

You can right click on your console, click properties, and in option pan, set it to Full Screen. you can save this changes to be persist.



回答4:

Do you mean unloading the GUI altogether, or changing screen resolution, like when you install a new device driver and windows goes to 800x600/8bpp, instead of your normal resolution? I cant help if you want a full screen console, but if you are trying to change your screen resolution, etc, take a look at http://www.c-sharpcorner.com/UploadFile/GemingLeader/display-settings08262009094802AM/display-settings.aspx



回答5:

Perhaps my implementation here may help. Note that this will not work on windows systems lacking text-mode driver support.

using System;
using System.IO;
using System.Collections.Generic; //for dictionary
using System.Runtime.InteropServices; //for P/Invoke DLLImport

class App
{

        /// <summary>
        /// Contains native methods imported as unmanaged code.
        /// </summary>
        internal static class DllImports
        {
            [StructLayout(LayoutKind.Sequential)]
            public struct COORD
            {

                public short X;
                public short Y;
                public COORD(short x, short y) { 
                    this.X = x;
                    this.Y = y;
                }

            }
            [DllImport("kernel32.dll")]
            public static extern IntPtr GetStdHandle(int handle);
            [DllImport("kernel32.dll", SetLastError = true)]
            public static extern bool SetConsoleDisplayMode(
                IntPtr ConsoleOutput
                ,uint Flags
                ,out COORD NewScreenBufferDimensions
                );
        }
        /// Main App's Entry point
        public static void Main (string[] args)
        {
            IntPtr hConsole = DllImports.GetStdHandle(-11);   // get console handle
            DllImports.COORD xy = new DllImports.COORD(100,100);
            DllImports.SetConsoleDisplayMode(hConsole, 1, out xy); // set the console to fullscreen
            //SetConsoleDisplayMode(hConsole, 2);   // set the console to windowed

        }
}