I tried this method: (but at startup it does not immediately appear in the center, but with a delay)

[DllImport("kernel32.dll", ExactSpelling = true)] public static extern IntPtr GetConsoleWindow(); [DllImport("user32.dll", EntryPoint = "SetWindowPos")] public static extern IntPtr SetWindowPos(IntPtr hWnd,int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags); public const int SWP_NOSIZE = 0x0001; public static IntPtr MyConsole = GetConsoleWindow(); public static void Inizialize() { var xpos = 450; var ypos = 350; SetWindowPos(MyConsole, 0, xpos, ypos, 0, 0, SWP_NOSIZE); } 

Is it possible to call the console in the central ( CenterScreen ) area immediately at startup, without delay?

    1 answer 1

    The problem is solved in this way, when you start the window instantly appears in the center of the desktop screen.

     [DllImport("kernel32.dll", SetLastError = true)] public static extern IntPtr GetConsoleWindow(); [DllImport("user32.dll", EntryPoint = "SetWindowPos")] public static extern IntPtr SetWindowPos ( IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags ); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); [StructLayout(LayoutKind.Sequential)] public struct RECT { public int Left; public int Top; public int Right; public int Bottom; } public static void Inizialize() { var wndRect = new RECT(); var cWidth = wndRect.Right - wndRect.Left; var cHeight = wndRect.Bottom - wndRect.Top; var SWP_NOSIZE = 0x1; var HWND_TOPMOST = -1; var Width = 1366; var Height = 768; IntPtr handle = Process.GetCurrentProcess().MainWindowHandle; SetWindowPos ( handle, HWND_TOPMOST, Width / 3 - cWidth / 2, Height / 2 - cHeight / 2, 0, 0, SWP_NOSIZE ); } 

    PS: For the place GetConsoleWindow decided to take Process.GetCurrentProcess().MainWindowHandle , I thought that it would be easier)