thefeduke wrote:Very impressive. I had to explore. I took your DOS powershell command out of context to try to understand this magic. your FOR construct seems to run %psCmd% in a way that hides the output. That consists of error messages about not being able to update the system variable $host. Yet the routine works. Fool that I am, I changed $host to $myhost and the errors disappeared and it still worked. More exploring. Screen print:
Arghh. My fault! My apologies to place a post containing typos.
(not tested) Here, you'll find a powershell script to resize and move a window
https://gallery.technet.microsoft.com/scriptcenter/Set-the-position-and-size-54853527Surely is not what you pretend, but it may be all done with an hybrid script.
Once the c# exe is build, you can simply call it, and remove the creation stuff , or delete and recreate each time.
NOTE .Net framework must be installed on your system.
Hope it helps.
EDIT 17/08/2017, restore window size when changing buffer size (and little polish).
Code: Select all
//>nul 2>nul||@goto :batch_code
/*
:batch_code
@echo off
SetLocal
rem place desired exe name
set "theExeFile=mySetWinPos.exe"
if not exist "%theExeFile%" call :build_the_exe || exit/B
%theExeFile% w:80,25 b:80,250 p:100,50
:: your script here.
:: blah blah
:: blah blah
rem del "%theExeFile%" >NUL 2>NUL
EndLocal
EndLocal
exit /b 0
:build_the_exe
for /f "tokens=* delims=" %%v in ('dir /b /s /a:-d /o:-n "%SystemRoot%\Microsoft.NET\Framework\csc.exe"') do (
set "csc=%%v"
)
if "%csc%" == "" echo/&echo/Warning: Net Framework Not Found&exit/B 1
call "%csc%" /nologo /out:"%theExeFile%" "%~dpsfnx0"
exit/B 0
*/
//begin c# code
using System;
using System.Runtime.InteropServices;
/*
op-code may be
b - to set buffer size
p - to set window position
w - to set window size
args may follow the format, op-code:x-axis,y-axis
i,e to change window size and buffer size
%theExeFile% w:80,40 b:80,250
*/
namespace ElZooilogico
{
class windowPos
{
public static uint SWP_NOSIZE = 1;
public static uint SWP_NOZORDER = 4;
[DllImport("kernel32.dll", ExactSpelling = true)]
private 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, uint wFlags);
private static IntPtr myconsole = GetConsoleWindow();
public static int Main(string[] args)
{
string[] tmp = null;
string[] buf = null;
try {
if ( args.Length < 1 )
return 1;
for ( int i = 0; i < args.Length; i++ )
{
tmp = args[i].Split(':');
if ( tmp.Length == 2 )
{
buf = tmp[1].Split(',');
if ( tmp[0].Equals("w", StringComparison.OrdinalIgnoreCase) )
Console.SetWindowSize(Int32.Parse(buf[0]), Int32.Parse(buf[1]));
if ( tmp[0].Equals("p", StringComparison.OrdinalIgnoreCase) )
SetWindowPos(myconsole, 0, Int32.Parse(buf[0]), Int32.Parse(buf[1]), 0, 0, SWP_NOSIZE | SWP_NOZORDER);
if ( tmp[0].Equals("b", StringComparison.OrdinalIgnoreCase) )
{
int width = Console.WindowWidth, height = Console.WindowHeight;
// the following two lines are mandatory to decrease buffer size
Console.Clear();
Console.SetWindowSize(1, 1);
Console.SetBufferSize(Int32.Parse(buf[0]), Int32.Parse(buf[1]));
Console.SetWindowSize(width, height);
}
}
}
} catch (Exception e) { System.Windows.Forms.MessageBox.Show(e.Message); return 9; }
//} catch { return 1; }
return 0;
}
} // class windowPos
} // namespace ElZooilogico