none
How to launch .NET core Console Window at non-default location RRS feed

  • Question

  • Hello:

    I have multiple .NET Core Console projects, all those console program will launch console window at the default location, which is near the center of the current windows 10 screen. Therefore, I have to click each of them, and drag them to other different position.

    For example, when I have 4 of such .net core Console programs, then I want to drag the first one to the upper left corner of the windows 10 screen, then drag the second one to the lower left corner of the current screen, then drag the third one to the upper right corner of the screen, and the last one lower right corner of the screen.

    I saw some articles showing how to do similar thing using Windows API, but since I am using .NET core, I don’t want to use Windows API, as it depends on Windows only.

    My monitor has resolution of 3840 by 2160 pixels on Windows 10 (version 1903).

    I want to divided the screen into 4 parts: upper left, lower left, upper right and lower right, while each part occurs about 20% to 25% of the total screen areas.

    Please show me how to use C# to place 4 console program window into each of the 4 parts when they launched.

    By the way, my IDE is Visual Studio 2019 version 16.2.4.

    Thanks,

    Sunday, September 8, 2019 5:07 PM

Answers

  • Hello:

    Thanks for your reply, but since I am using .NET Core, and would like to use my program cross-platform, so I want to have some code not using Windows API, which is only working on Windows platform.

    Is it possible?

    Thanks,

    • Marked as answer by zydjohn Tuesday, September 10, 2019 8:07 AM
    Monday, September 9, 2019 6:40 AM

All replies

  • Hi zydjohn,

    Thank you for posting here.

    You could try the following code to change console window default location.

     class Program
        {
            static void Main(string[] args)
            {
                IntPtr handle = Process.GetCurrentProcess().MainWindowHandle;
                MoveWindow(handle, 0, 0, 1000, 500, true);//upper left
                Console.WriteLine("I am console 1");
                Console.ReadKey();
            }
    
            [DllImport("user32.dll", SetLastError = true)]
            internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
        }

    You could use the following code to change the location of console window.

    MoveWindow(handle, 930, 0, 1000, 500, true); //upper right
    MoveWindow(handle, 0, 500, 1000, 500, true); //lower left
    MoveWindow(handle, 930, 500, 1000, 500, true); // lower right

    Test Result:


    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, September 9, 2019 5:30 AM
    Moderator
  • Hello:

    Thanks for your reply, but since I am using .NET Core, and would like to use my program cross-platform, so I want to have some code not using Windows API, which is only working on Windows platform.

    Is it possible?

    Thanks,

    • Marked as answer by zydjohn Tuesday, September 10, 2019 8:07 AM
    Monday, September 9, 2019 6:40 AM
  • Hi zydjohn,

    Thanks for the feedback.

    Based on my search, I could not find the direct way to do it without using windows API. 

    Maybe it has not been developed.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 9, 2019 9:02 AM
    Moderator