Open Command prompt window within a Panel control in C#

If you want to open the command prompt window within your windows application, you can use the setparent method of the user32 API.
 
Open Command prompt window within a Panel control in C#
 

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;
 
namespace WindowsFormsApplication1
{ 
    public partial class Form1 : Form
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);
 
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        public Form1()
        {
            InitializeComponent();
        }
 
 
        private void button1_Click(object sender, EventArgs e)
        {
            const int WM_SYSCOMMAND = 0x112;
            const int SC_MINIMIZE = 0xF020;
            const int SC_MAXIMIZE = 0xF030;
 
            Process p = Process.Start(
                       new ProcessStartInfo()
                       {
                           FileName = "cmd.exe",
                           WindowStyle = ProcessWindowStyle.Minimized
                       });
            Thread.Sleep(500);
            IntPtr value = SetParent(p.MainWindowHandle, panel1.Handle);
            SendMessage(p.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
        }
    }
}

One thought on “Open Command prompt window within a Panel control in C#”

Comments are closed.