using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
namespace Tools{
class CmdDocking {
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr
FindWindow([MarshalAs(UnmanagedType.LPTStr)] string lpClassName,
[MarshalAs(UnmanagedType.LPTStr)] string lpWindowName);
[DllImport("user32.dll")]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X,
int Y, int cx, int cy, uint uFlags);
public CmdDocking(){
StartConsole();
}
private const String ClassName = "ConsoleWindowClass";
/// <summary>
/// Die System Console CMD.EXE starten
/// </summary>
private void StartConsole() {
Process process = new Process();
process.StartInfo = new ProcessStartInfo(string.Format(@"{0}\cmd.exe",Environment.GetFolderPath(Environment.SpecialFolder.System)));
process.Start();
}
/// <summary>
/// Console in eigene Form einbinden
/// </summary>
/// <param name="formToDock">Form in die, die Console eingebunden werden soll</param>
/// <param name="posX">Position X des Consolen Fensters</param>
/// <param name="posY">Position Y des Consolen Fensters</param>
/// <param name="width">Breite des consolen Fensters</param>
/// <param name="hight">Höhe des consolen Fensters</param>
public void DockConsole(Form formToDock, int posX, int posY, int width, int hight) {
SetWindowPos(GetHwnd(), IntPtr.Zero, posX, posY, width, hight, 0);
SetParent(GetHwnd(), formToDock.Handle);
}
private IntPtr GetHwnd() {
return FindWindow(ClassName, null);
}
}
}