Move console window relative to screen

I am working on a C # console application, and I have increased the window height using Console.WindowHeight, but now the bottom of the window tends to leave the screen when I first open the application.

Is there a way, in a console application, to set the position of the console window relative to the screen? I looked in Console.SetWindowPosition, but this only affects the position of the console window relative to the "screen buffer", which does not seem to be what I need.

Thanks for any help!

+4
source share
1 answer

Here's a solution that uses a window handle and an imported function SetWindowPos()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();
        }
    }
}

, z-, / .

+5

Source: https://habr.com/ru/post/1606108/


All Articles