Programmatically set console window size and position

偶尔善良 提交于 2020-01-11 06:09:07

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/35263590/programmatically-set-console-window-size-and-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!