如何:在 Winform 动态启动、控制台命令行?

需求
   winForm 程序输出类型为 windows 程序(不是命令行程序)
   在运行时想输入一些信息编译开发调试,如何实现这一功能

解答:
  AllocConsole、FreeConsole 这两个 API 可以在任何时候调用和关闭 命令行。

代码演示:
API 部分

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
    public partial class NativeMethods {
    
        /// <summary>
        /// 启动控制台
        /// </summary>
        /// <returns></returns>
        [DllImport("kernel32.dll")]
        public static extern bool AllocConsole();
        /// <summary>
        /// 释放控制台
        /// </summary>
        /// <returns></returns>
        [DllImport("kernel32.dll")]
        public static extern bool FreeConsole();

        
    
    }
}

启动参数的实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            try
            {
                
                if(args.Length > 0 && args[0].ToLower() == "-c")
                {//通过命令行 xxxx.exe -c 参数启动,Console
                    
                    //注意:不用 Main(string[] args)、System.Environment.GetCommandLineArgs();  也可以取得命令行参数在任何地方
                    
                    //启动
                    NativeMethods.AllocConsole();
                    Console.WriteLine("控制台以启动");
                }
                
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
            finally
            {
                //关闭 (如果在这个位置其实写不写都行了)
                NativeMethods.FreeConsole();
            }
        }
    }
}

程序实现
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;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnOpenConsole_Click(object sender, EventArgs e)
        {
            //开启控制台
            NativeMethods.AllocConsole();
        }

        private void btnCloseConsole_Click(object sender, EventArgs e)
        {
            //关闭控制台
            NativeMethods.FreeConsole();
        }

        private void btnOut_Click(object sender, EventArgs e)
        {
            //模拟输出
            Console.WriteLine(textBox1.Text);
        }
    }
}

转载于:https://www.cnblogs.com/kalilo/p/3919481.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值