15

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!).

Vercas
  • 8,931
  • 15
  • 66
  • 106
  • If the application should be full screen, I'd consider creating a windows application instead of a console application. Unless you have requirements that say it must be a console application, there really is no reason not to go with a window. – Jason Down Dec 12 '10 at 18:15
  • Yes, well I am going to use a full screen form with a console-like control. – Vercas Dec 12 '10 at 18:28
  • My approach works fine how do you say it's not working? – Saeed Amiri Dec 13 '10 at 10:02
  • Try it on Windows Vista or Windows Seven. PLUS I was looking for a method to do this with my console alone. – Vercas Dec 18 '10 at 09:40
  • You can examine the way CYGWIN is doing the things. I use minty and its console is windowed, and has a maximize which takes the whole screen. – Bakudan Mar 13 '12 at 19:37
  • The C# System.Console codebase doesn't even provide a wrapper to fullscreen functionality, so Vercas is correct: P/Invoke's are a must. – Lorenz Lo Sauer Sep 18 '12 at 08:25
  • By the way: There still are no solution to the question. Providing custom console widget for an apllication is a solution but how to do that? Are there any opensource libraries implementing that? – jaboja May 06 '14 at 16:14
  • @JakubJagiełło I was looking for a custom console host just a few days ago... More precisely, I am looking into providing window size and other such parameters reliably. I did not find anything helpful. – Vercas May 07 '14 at 17:13

6 Answers6

7

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.)

Richard
  • 106,783
  • 21
  • 203
  • 265
  • Thanks, this is the most complete (can I say that?) answer of all! – Vercas Dec 12 '10 at 18:27
  • 2
    Alt+Enter still works on console windows in Windows 10. – Matheus Rocha Jul 10 '17 at 21:39
  • @MatheusRocha, in my Win10, ALT-Enter performs a maximize but not a full screen. – Xonatron Oct 07 '20 at 14:20
  • @Xonatron Some config might be preventing full screen windows. By default (tested on a brand new updated installation of Win10) Alt+Enter makes the console window full screen. Have you searched around about any configs or registry keys that might have control over this behaviour? – Matheus Rocha Oct 07 '20 at 15:51
  • 1
    @MatheusRocha try doing an alt-tab: the console window stays the same: it is maximised without a title bar. In older Windows there was a screen mode change to text and alt-tab away out restore the console window. Based on the question (graphics card switching to text mode) what happens today is not the old "full screen text mode". – Richard Oct 07 '20 at 16:02
  • Additionally: Windows Terminal is so much more functional for command line purposes I really don't understand why anyone would want the old console windows... – Richard Oct 07 '20 at 16:03
  • @Richard It certainly isn't the same, but the asker just wants to replicate the "full screen console" appearance AFAIK. – Matheus Rocha Oct 07 '20 at 16:18
  • @Richard The asker doesn't wanna use the Cmd nor the terminal, he wants to replicate that behaviour in C#. C# console applications use the native console window, same as the command prompt. – Matheus Rocha Oct 07 '20 at 16:18
  • @Richard Just to clarify: It uses the native console window ENCAPSULATED by managed code. But it's still the native console window under the hood. – Matheus Rocha Oct 07 '20 at 16:20
3

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.

Community
  • 1
  • 1
Chris Taylor
  • 52,623
  • 10
  • 78
  • 89
  • On CMD (x86, Windows 7) I get an error that the device driver does not support this. – Vercas Dec 12 '10 at 18:26
  • @Vercas: Actually, it is device driver dependent. The support or lack thereof is in the OS code, not the driver, but different OS code paths get used for legacy display drivers (support is present) vs WDDM (no support for full-screen). – Ben Voigt Jun 30 '12 at 18:32
1

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

        }
}
Lorenz Lo Sauer
  • 23,698
  • 16
  • 85
  • 87
1

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.

Saeed Amiri
  • 22,252
  • 5
  • 45
  • 83
  • Is this still available on Windows 7 (and Vista)? – Jason Down Dec 12 '10 at 18:21
  • @Hans Unfortunately, that is true in this case. – Vercas Dec 12 '10 at 18:32
  • @Jason Down, I have XP sp3, I didn't know about Seven, @Vercas, @Hans Passant, It works carefully in XP, and because you can set it to be persist, It's useful for stand alone applications, because there is no need to do for each running of a program, you will do it after installation. – Saeed Amiri Dec 12 '10 at 19:25
  • @Vercas, just be care full, you should set the Console of .net frame work, not usual command prompt, goto install folder of framework and find it. – Saeed Amiri Dec 12 '10 at 19:39
  • @Saeed It doesn't work on Windows Vista and Windows Seven, so this is a dead cause. – Vercas Dec 13 '10 at 18:54
1

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

weloytty
  • 5,808
  • 5
  • 28
  • 35
0

good but wrong question duo to the lack of deep understanding the concept of a "console" in a generic fashion

1- why the question is wrong?


assume a tty:
enter image description here
if you want to change the size of a console app window from the code which is responsible for the program of that console app, it is similar that you want to write outside of the paper which is in the tty device!!
in other words, a console in your computer is like a virtual paper for the program you write in c# (or...), if for example the maximum width of your console window is 1000px you can change your console maximum available width, maximum to n where n<=1000 (e.g.: in windows console you can use Console.BufferWidth property for this) but you can not change the width of the window which is responsible for showing the console to you from your console app program (instead of using .Net libraries which are for changing windows properties in your console app) cause actually that is non sence!
so the right question is:

how to change the size of the window which is responsible to monitor a console application?

2- now what is workaround?


2-1- logical and robust way: use .Net UI Automation for this, you can get the process of your console app (e.g. with Process.GetCurrentProcess()), get the AutomationElement from your process, put it into a WindowPattern and finally you can perform a method like yourTarget.SetWindowVisualState(WindowVisualState.Maximized) or anything you want on it
2-2- hacky and not robust way: use below code:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.IO;  
using System.Diagnostics;  
using System.Runtime.InteropServices;  
namespace Consolefullscreen  
{  
   class Program  
   {  
      [DllImport("kernel32.dll", ExactSpelling = true)]  
      private static extern IntPtr GetConsoleWindow();  
      private static IntPtr ThisConsole = GetConsoleWindow();  
      [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]  
      private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);  
      private const int HIDE = 0;  
      private const int MAXIMIZE = 3;  
      private const int MINIMIZE = 6;  
      private const int RESTORE = 9;  
      static void Main(string[] args)  
      {  
         Console.SetWindowSize(Console.LargestWindowWidth, Console.LargestWindowHeight);  
         ShowWindow(ThisConsole, MAXIMIZE);  
         Console.ReadLine();  
      }  
   }  
}