Home Ask Login Register

Developers Planet

Your answer is one click away!

TeamBreezy February 2016

Programmatically set console window size and position

I would like to open multiple console programs on my desktop. This is what I have to do everytime: 1.Right click Desktop->Screen resolution->Detect (4 monitors). 2.Open 16 different console programs (4 per screen). 3.Clicking on all windows to get the Z-order correctly. 3.Right click Taskbar->Show Windows Stacked (to organize all 16 windows to perfect squares, 4 on each screen in order of z-index).

Is there a way to do even just a part of this programmatically to help this go quicker?


Fratyx February 2016

You can use the windows API to move your console window. Use DllImport to declare the WinApi functions you want to use:

[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GetConsoleWindow();

[DllImport("user32.dll", SetLastError = true)]
internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);

Then call them: e.g.

IntPtr ptr = GetConsoleWindow();
MoveWindow(ptr, 0, 0, 1000, 400, true);

You can use further WinApi function as SetWindowPos. You can find the DllImport syntax by searching the web for PInvoke and the name of the function. Follow the explanations there and in MSDN.

Post Status

Asked in February 2016
Viewed 1,860 times
Voted 12
Answered 1 times


Leave an answer

Quote of the day: live life