winform 运行中使用控制台动态输出调试及运行信息
来源:互联网 发布:mp3播放器推荐 知乎 编辑:程序博客网 时间:2024/06/02 12:28
如何:在 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.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);
}
}
}
- winform 运行中使用控制台动态输出调试及运行信息
- MFC中使用控制台输出调试信息
- MFC中使用控制台输出调试信息
- Eclipse中运行MapReduce程序控制台无调试信息-log4j
- Eclipse中运行MapReduce程序控制台无调试信息-log4j
- winform运行后台运行cmd并实时输出运行信息
- 程序运行时将打印信息输出到控制台窗口
- 调试MFC时使用控制台(consel)输出调试信息
- MFC中使用控制台输出Debug信息
- 使用命名管道在winform程序中输出windows service运行状态
- C# 窗体程序,使用控制台输出调试信息
- WinForm程序中使用控制台作为输出窗口
- WinForm程序中使用控制台(console)作为输出窗口
- MinGW eclipse中运行c++控制台没输出
- Android studio中运行java项目,控制台输出中文乱码
- Eclipse中运行MapReduce程序时控制台打印进度信息
- MFC中使用printf输出调试信息
- PowerShell中的同步命令运行及信息输出
- XP去掉桌面快捷方式小箭头
- Eclipse调试Android工具集锦之三-ADB
- Flex4提示框效果
- minit2440 开发板网络配置的解决
- C++ 对象的内存布局(下)
- winform 运行中使用控制台动态输出调试及运行信息
- 动态规划解游船费用问题
- 关于SQL Server还原错误:The backup set holds a backup of a database other than the existing database.
- Win32汇编——文件操作
- HDU/HDOJ 3264 计算几何+二分 2009 宁波区域赛
- CentOS 安装Oracle netca 产生错误
- Jave语言和C#语言
- 用malloc动态分配二维数组
- 《当程序员的那些狗日日子》(三十八)仓促中的选择