How to: Winform Dynamic boot, console command line?

Source: Internet
Author: User

Demand
WinForm program output type is a Windows program (not a command-line program)
At run time want to enter some information compile development debugging, how to realize this function

Answer:
AllocConsole,freeconsole These two APIs can call and close the command line at any time.

Code Demo:
API section

UsingSystem;
UsingSystem.Collections.Generic;
UsingSystem.Linq;
UsingSystem.Text;
UsingSystem.Runtime.InteropServices;

NamespaceWindowsFormsApplication1
{
PublicPartialClassNativeMethods {

///<summary>
///Start the console
///</summary>
///<returns></returns>
[DllImport ("Kernel32.dll")]
PublicStaticexternboolAllocConsole ();
///<summary>
///Release the console
///</summary>
<returns></returns>
[DllImport ("Kernel32.dll")]
public static extern bool freeconsole ();



}
}
Implementation of startup parameters

UsingSystem;
UsingSystem.Collections.Generic;
UsingSystem.Linq;
UsingSystem.Windows.Forms;
UsingSystem.Runtime.InteropServices;
UsingSystem.Diagnostics;

NamespaceWindowsFormsApplication1
{
StaticClassProgram
{
///<summary>
///The main entry point for the application.
///</summary>
[STAThread]
StaticvoidMain (String[] args)
{
Try
{

If(args. Length>0&&args[0]. ToLower ()=="-C")
{//Start with the command line xxxx.exe-c parameter, Console

//Note: Do not use Main (string[] args), System.Environment.GetCommandLineArgs (); You can also get command line arguments anywhere

//Start
Nativemethods.allocconsole ();
Console.WriteLine ("Console to start");
}

Application.enablevisualstyles ();
Application.setcompatibletextrenderingdefault (False);
Application.Run (New Form1 ());
}
finally
{
// off (if it is not written in this position, it is OK)
Nativemethods.freeconsole ();
}
}
}
}
Program implementation
UsingSystem;
UsingSystem.Collections.Generic;
UsingSystem.ComponentModel;
UsingSystem.Data;
UsingSystem.Drawing;
UsingSystem.Linq;
UsingSystem.Text;
UsingSystem.Windows.Forms;

NamespaceWindowsFormsApplication1
{
PublicPartialClassForm1:form
{
PublicForm1 ()
{
InitializeComponent ();
}

PrivatevoidBtnopenconsole_click (Objectsender, EventArgs e)
{
//Turn on the console
Nativemethods.allocconsole ();
}

PrivatevoidBtncloseconsole_click (Objectsender, EventArgs e)
{
//Close the console
Nativemethods.freeconsole ();
}

private void< Span style= "color: #000000;" > btnout_click (object sender,  eventargs e)
        {
             // analog output
             Console.WriteLine (TextBox1.Text);
        }
    }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.