7

I'm working on a C# console application and I increased the height of the window using Console.WindowHeight, but now the bottom of the window tends to go off screen when the app is first opened.

Is there way, in a console app, to set the position of the console window relative to the screen? I looked into Console.SetWindowPosition, but this only affects the position of the console window relative to the 'screen buffer,' which doesn't seem to be what I'm after.

Thanks for any help!

fyodorfranz
  • 476
  • 8
  • 25
  • 1
    In native console applications the sequence is: ``HWND consoleWnd = GetConsoleWindow(); SetWindowPos(consoleWnd, .... );`` Now all you need to do is to see if you find functions with similar names in the .NET environment. Worst case, you still can call the native win32 functions. – BitTickler Sep 06 '15 at 00:46
  • 1
    http://stackoverflow.com/questions/1277563/how-do-i-get-the-handle-of-a-console-applications-window gives pointers regarding "how to get window handle". – BitTickler Sep 06 '15 at 00:55
  • Solution working in Windows 10 is here: http://stackoverflow.com/questions/2888824/console-setwindowposition-centered-each-and-every-time – scar80 Jul 04 '16 at 12:08
  • Does this answer your question? [Position a small console window to the bottom left of the screen?](https://stackoverflow.com/questions/27715004/position-a-small-console-window-to-the-bottom-left-of-the-screen) – Elaskanator Oct 31 '21 at 23:27

2 Answers2

8

Here a solution which uses the window handle and an imported SetWindowPos() native function to achieve what you are looking for:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;


namespace ConsoleWindowPos
{
    static class Imports
    {
        public static IntPtr HWND_BOTTOM = (IntPtr)1;
       // public static IntPtr HWND_NOTOPMOST = (IntPtr)-2;
        public static IntPtr HWND_TOP = (IntPtr)0;
        // public static IntPtr HWND_TOPMOST = (IntPtr)-1;

        public static uint SWP_NOSIZE = 1;
        public static uint SWP_NOZORDER = 4;

        [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
        public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, uint wFlags);
    }

    class Program
    {

        static void Main(string[] args)
        {
            var consoleWnd = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
            Imports.SetWindowPos(consoleWnd, 0, 0, 0, 0, 0, Imports.SWP_NOSIZE | Imports.SWP_NOZORDER);
            System.Console.ReadLine();
        }
    }
}

The code moves the console window to the top left of your screen, not changing z-order nor changing width/height of the window.

BitTickler
  • 10,905
  • 5
  • 32
  • 53
  • 1
    Thanks a lot for the reply! For some reason this isn't working for me yet; i.e. the window is still opening in the middle region of the screen. – fyodorfranz Sep 06 '15 at 01:21
  • Yes but then it should "jump" to the top left. That is what it does for me. Did you set a break point before SetWindowPos(), maybe? – BitTickler Sep 06 '15 at 01:21
  • I do that btw. on Windows 7 x64 with VS2015 community edition, Debug| Any CPU build configuration. – BitTickler Sep 06 '15 at 01:25
  • So I tried running it with the break point and definitely saw it jump to the top left. Is it possible to make it stay in that position? – fyodorfranz Sep 06 '15 at 01:30
  • It should stay there unless you move it somewhere else later on :) – BitTickler Sep 06 '15 at 01:34
  • 1
    Could not get this to work on a system running Windows 10 x64 and Visual Studio 2015 WITHOUT using debug. Thanks for the effort, though. – gimlichael Dec 09 '15 at 14:03
  • @MichaelMortensen What does "without using debug" mean in this context? Seems to be working for me (VS2017, Win10x64), fwiw. – ruffin May 16 '17 at 22:07
-1

You can use Console.SetWindowPosition(int left, int top), which works for both .NET Framework and .NET 5.0.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Brian Boyd
  • 162
  • 1
  • 4