3

I'm looking for a way to get the size of the actual window of the console in pixels. All what I've found this far are uses winforms or WPF, both of which I don't use. I'm not looking for how to get the size inside the console window but how big the window itself is.

  • The console is effectively under Windows/DOS Control, not under your control. It is also really wierd why you think you need that. It sounds like you got a XY problem there: https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem | Could you please specify what you need this for, so we can look for a doable way? – Christopher Nov 10 '19 at 03:55
  • Using the windows API it should be easy to get the size and screen position of a window. I think, the difficult task will be to find the window that runs the console within. Microsoft is developing a new terminal that will or could replace the _ConHost.exe_. But even today you cannot assume that your process always will run within _ConHost.exe_. So you cannot just seek for _ConHost.exe_ process to find the window you're looking for. Therefore I disagree with the comment from @GerardoGrignoli in this point. I still wonder why one would want to know this information – Daniel Schmid Nov 10 '19 at 09:42
  • @Christopher So my real issue is about moving the actual console window by a specific amount of pixels. This is why I thought I needed the pixel values. One of the main things I wanted to do was center the window in the middle of the screen, and would thus need both the screen size and the window size as I've yet to find any way to center the console using anything besides absolute positioning. The actual moving is being done with SetWindowPos (https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowpos). –  Nov 10 '19 at 14:40

2 Answers2

2

I search the web but I found nothing usefull.

All answers return either number of characters either the full screen resolution.

The Windows console is historically an emulation of the DOS shell.

From How to get the screen size of Console Application?:

using System.Management;  // Need assembly reference added to project

Console.Write("Console resolution in char:");
Console.Write(Console.WindowWidth + "x" + Console.WindowHeight);

var scope = new ManagementScope();
scope.Connect();
var query = new ObjectQuery("SELECT * FROM Win32_VideoController");
using ( var searcher = new ManagementObjectSearcher(scope, query) )
  foreach ( var result in searcher.Get() )
    Console.WriteLine("Screen resolution in pixels: {0}x{1}",
                      result.GetPropertyValue("CurrentHorizontalResolution"),
                      result.GetPropertyValue("CurrentVerticalResolution"));

It returns the screen resolution from the current video driver mode.

The Windows command line is no more a DOS command line but a reduced shell.

Many things and many DOS commands were lost.

You cannot create programs in the console itself anymore.

The command line outputs API is now very simple and basic.

Today a console app can only know its resolution in chars.

There is no more advanced API nor interrupts available.

The Windows DOS shell COMMAND.COM has been abandonned for Command Prompt CMD.EXE after Windows Me.

There is no simple way to know what is the resolution in pixels of the Windows that "hosts" the inner console.

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

[DllImport("user32.dll")]
private static extern int GetWindowRect(IntPtr hwnd, out Rectangle rect);

Rectangle rect;
GetWindowRect(Process.GetCurrentProcess().MainWindowHandle, out rect);
Console.WriteLine($"Console window location: {rect.X}x{rect.Y}");
Console.WriteLine($"Console window resolution: {rect.Width}x{rect.Height}");

But the result is not very accurate.

For example it shows:

Console window location: 187x174
Console window resolution: 1336x812

When the real resolution is 1149x638...

And this console window resolution vary at each run...

Console window location: 99x58
Console window resolution: 1248x696

Console window location: 143x116
Console window resolution: 1292x754

Console window location: 77x29
Console window resolution: 1226x667

Console window location: 187x174
Console window resolution: 1336x812

The real console form size is:

RealWidth = rect.Width - rect.X;
RealHeight = rect.Height - rect.Y;
  • I'm marking this as my answer as by using the second solution provided, I can subtract the location from the resolution and get the size. This works when I tested it for the purpose I needed it for. –  Nov 10 '19 at 15:57
  • 1
    Indeed! I had not seen that. It is strange. Thanks. –  Nov 10 '19 at 16:03
-1

The issue and its history

I doubt the Console is in any way under the level of control that you would need for this. Basically, when the console was originally designed, the idea of a Window had not yet been thought up. They are assumed to be full-screen in very low resolution, which was common back in those days. And they never really changed since then.

On higher resolutions, the best equation possible is to make a Window that covers part of the screen. But it is a Window only under OS control, not yours.

You could have a Console Window in parallel to a normal Desktop GUI interface. It is actually pretty simple. But it is still the same old console with all limits you got right now.

My best advice is:

Stop trying to use console. Instead, fake a Console Window using WindowsForms, WPF or UWP.

Using Process and Input/Output stream redirection, you can easily control a command line from your WinForms/WPF program. Just be warned that there is 3 Stream. Input, Output and Error (which is usually also sent to the console window). All without the console showing. It used to be a common approach to control programs that were command-line only, but nowadays it is mostly forgotten outside of the Unix/Linux World, as nearly everything has a Frontend.

You can then map the input/output to a label, textbox, richtTextBox or the like that mostly behaves and looks like a Console Window, without being one. And since it is Window of your display technology of choice, you got all the options to manipulate it.

InteXX
  • 6,135
  • 6
  • 43
  • 80
Christopher
  • 9,634
  • 2
  • 17
  • 31
  • I know the console is a piece of junk, but sadly that is what my limitations are for this project of mine. Though for anyone doing anything else than what I'm doing, its probably smart to just fake it instead, as the hassle for workarounds grow bigger otherwise. –  Nov 10 '19 at 15:59