在Qt中,经常使用
qDebug()<<"Hello World";
的方式往控制台打印一些输出,以便观察程序的运行情况。
在Java中(eclipse、myeclipse中),经常使用的是
System.out.println("Hello World");
的方式。
在Android中,经常使用的是
Log.d("Hello World");
. . .
在C#的时候,使用的是
Console.WriteLine("Hello World");
开发winform的时候,需要先往
主函数
所在
的源文件中加入以下内容。
引入库:
using System.Runtime.InteropServices;
在Main()前,添加:
[DllImport("kernel32.dll")]
public static extern bool AllocConsole();
[DllImport("kernel32.dll")]
static extern bool FreeConsole();
在Main()中,第一行添加:
AllocConsole();
在Main()中,最后一行添加:
FreeConsole();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
// 控制台输出,需加入此库
using System.Runtime.InteropServices;
namespace HelloWorld_WindowsForm
{
static class Program
{
[DllImport("kernel32.dll")]
public static extern bool AllocConsole();
[DllImport("kernel32.dll")]
static extern bool FreeConsole();
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
// 允许调用控制台输出
AllocConsole();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new LoginPage());
// 释放
FreeConsole();
}
}
}
打赏二维码:
在
Winform
程序中有时候
调试
会通过Console.Write()方式
输出
一些信息,这些信息是在Visual Studio的
输出
窗口显示。
所以就会想,能不能调用系统的Cmd窗口
输出
呢,经过一番查阅,发现是可以的,现在就把方法写下了:
主要用到的是win32 API函数实现的:
1 [DllImport("kernel32.dll")]
2 static extern bool Fr
控制台
作为信息显示窗口对于
调试
程序查找程序BUG有诸多好处,所以给GUI程序绑定一个
控制台
窗口用于显示程序运行时的状态是非常有必要的,否则就需要用
WinForm
的控件自己去实现了。首先想让
WinForm
程序绑定
控制台
窗口需要调用Windows API,需要先导入命名空间:
using System.Runtime.InteropServices;
然后导入dll:
[DllImport("kernel32.dll")]
public static extern bool AllocCons...
这个问题困扰我好久了,为什么用Console.WriteLine()总是看不到
输出
?
难道
winform
程序就不能用Console的
输出
,经过一番百度,貌似真的是这样
那难道就只能在程序上搞个textbox来
输出
吗?又或者只能是
输出
到文本?
网上某文提到了以下解决方法(红色为我加的):
1.debug只在[debug模式下才执行](运行按钮后面的下拉框可选)