The Winform dynamic startup console command line method

  • 2020-05-09 19:11:15
  • OfStack

demand
winForm program output type is windows program (not a command line program)
How do you implement the 1 function if you want to enter some information at runtime to compile development and debug

Answer:

The two API, AllocConsole and FreeConsole, can call and close the command line at any time.

Code demo:
API part


using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
    public partial class NativeMethods
    {
        /// <summary>
        ///  Startup console 
        /// </summary>
        /// <returns></returns>
        [DllImport("kernel32.dll")]
        public static extern bool AllocConsole();
        /// <summary>
        ///  Release console 
        /// </summary>
        /// <returns></returns>
        [DllImport("kernel32.dll")]
        public static extern bool FreeConsole();
    }
}

Start the implementation of the parameters

using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        ///  The application's main entry point. 
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                if (args.Length > 0 && args[0].ToLower() == "-c")
                {// From the command line  xxxx.exe -c  Parameter startup, Console
                    // Note: no  Main(string[] args) , System.Environment.GetCommandLineArgs();   You can also get command line arguments anywhere 
                    // Start the 
                    NativeMethods.AllocConsole();
                    Console.WriteLine(" Console to start ");
                }
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
            finally
            {
                // Shut down   (it doesn't matter if I write it here or not.) 
                NativeMethods.FreeConsole();
            }
        }
    }
}

Program implementation

using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void btnOpenConsole_Click(object sender, EventArgs e)
        {
            // Open console 
            NativeMethods.AllocConsole();
        }
        private void btnCloseConsole_Click(object sender, EventArgs e)
        {
            // Close the console 
            NativeMethods.FreeConsole();
        }
        private void btnOut_Click(object sender, EventArgs e)
        {
            // Analog output 
            Console.WriteLine(textBox1.Text);
        }
    }
}

Code download :(VS2008 if other versions of VS please modify it yourself)
http://xiazai.ofstack.com/201302/other/WinformShellConsole_VS08.rar

Finally:

The code is simple, but it's good for printing out some temporary debugging information at run time
Action 1, drawing with GUI, can easily affect the situation of the Print event
Sometimes in the client where the program problems open the console output 1 some debugging information look more convenient;
The controls are so threadless that they are easier to use than a separate log window and easier to copy content, and support the paus key.


Related articles: