At some point in time you got frustrated that Windows console app opens on monitor that is not ideal for you or you just want to move it to another screen. Unfortunately Console does not offer out of the box solution to fix this problem so we need to find solution out of the box.

 

My solution is based upon consuming Windows API’s and Windows Forms to easily create maintainable solution.

 

SetWindowPos function seems to be a good start. By using it we can set Windows starting X and Y axis, as well as its width, height and window position. To use this function there are 2 challenges:

Getting monitors starting X point and getting the console Windows handle.

Monitors X axis point can be easily acquired by using WinForms  System.Windows.Forms.Screen.AllScreens property, which stores all connected monitors ant their information.

Getting Console Window handle is a bit trickier. First we need to use  GetConsoleWindow function assign it to the pointer and then pass it to the SetWindowPos.

 

SetWindowPos has a limitation thou. If we want to maximize screen, we cannot do it (we can extend it to monitors max width and height but its state would still not be maximized). Here we can use ShowWindow function where we pass our Console window and it’s desired state.

 

That would be all for this short tutorial, code sample is bellow.

 

Happy coding!