在Winform动态启动、控制台命令行的方法
来源:互联网 发布:lol s8 知乎 编辑:程序博客网 时间:2024/06/05 20:13
需求
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.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.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);
}
}
}
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);
}
}
}
代码下载:(VS2008 如果其他版本VS请自行修改)
/Files/flashelf/WinformShellConsole_VS08.rar
最后:
其实代码很简单,不过很适合在运行时输出一些临时调试信息
用GUI画图的操作一般下断点很容易影响Print 事件的情况
,有时候在客户那里程序问题在上打开控制台输出一些调试信息看着比较方便;
而且控制太没有线程限制的,所以使用起来要比单独的日志窗口方便、而且容易复制内容,还支持 paus 键;
- 在Winform动态启动、控制台命令行的方法
- 在 Winform 动态启动、控制台命令行
- 在winform中动态启动命令行
- 在Android命令行启动程序的方法
- 在Android命令行启动程序的方法
- 一种在linux下启动shell进程并取得控制台输出的方法
- c# winform 程序在自启动后的获取路径的方法
- WinForm程序启动控制台窗口Console
- Winform动态调用Webservice的方法
- winform程序在当前会话只允许启动一个的方法
- C#之WinForm基础 winform中用到控制台的consol.writeline,在哪里可以看到效果
- Android AM命令行启动程序的方法
- AM 命令行启动程序的方法
- Android命令行启动程序的方法
- Android命令行启动程序的方法
- ubuntu从命令行启动的方法
- Android AM命令行启动程序的方法
- Android AM命令行启动程序的方法
- BFS--hdoj2102
- seo经验分享 也许对你们有帮助
- Ant使用教程Eclipse下
- Rest API 概述
- 【综述】(MIT博士)林达华老师-"概率模型与计算机视觉”
- 在Winform动态启动、控制台命令行的方法
- 使用 CXF 做 webservice 简单例子
- 稀疏矩阵的十字链表存储的思路
- C/C++字符变量无法显示的问题
- 独一无二的职位:开源社区经理
- 字典序问题
- oracle的一些高级学习网址收集
- ݿհʊtel1353005-6393
- 函数PostEvent、triggerevent详解