开发者

How do I include a console in Winforms?

开发者 https://www.devze.com 2023-01-19 13:32 出处:网络
I would like to have a console window embedded in a Winform.开发者_开发百科 Is there any way to do this?All you need to do is call the windows API function AllocConsloe then use the normal console cla

I would like to have a console window embedded in a Winform.开发者_开发百科 Is there any way to do this?


All you need to do is call the windows API function AllocConsloe then use the normal console class here is the form code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace waTest
{
    public partial class Form1 : Form
    {
        [DllImport("Kernel32.dll")]
        static extern Boolean AllocConsole( );

        public Form1( )
        {
            InitializeComponent();
        }

        private void Form1_Load( object sender, EventArgs e )
        {
            if ( !AllocConsole() )
                 MessageBox.Show("Failed");
            Console.WriteLine("test");
            string input = Console.ReadLine();
            MessageBox.Show(input);
        }
    }
}


Oh! You want the console in the window. You can write your own and pipe input to and from stdout and stdin. Or you can imbed powershell but there is no baked in control. – rerun Oct 12 '10 at 19:49


You can do this basically by:

  1. Creating the cmd process
  2. Setting the parent of that process to be the form (or some panel for example)
  3. Plug in the events to resize when needed
  4. Kill the process when the main process doesn't need the cmd process anymore.

You need to call the API directly for this (you need SetParent and SetWindowPos). Here is an article on how to do it with examples:

http://www.geekpedia.com/tutorial230_Capturing-Applications-in-a-Form-with-API-Calls.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号