先找到你的项目-》右键,选择属性
然后把输出类型改成控制台应用程序,保存
最后的效果:
直接在解决方案对应的项目中右击,选择属性->应用程序->输出类型,在下拉列表中选择 控制台应用程序
由于控制台api被封装在kernel32.dll链接库中,而kernel32.dll并未托管dll,所以需要使用DllImport来导入。
static class Program
{
/// <summary>
/// 启动控制台
/// </summary>
/// <returns></returns>
[DllImport("kernel32.dll")]
public static extern Boolean AllocConsole();
/// <summary>
/// 释放控制台
/// </summary>
/// <returns></returns>
[DllImport("kernel32.dll")]
public static extern Boolean FreeConsole();
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
#if DEBUG
AllocConsole();
// Console.WriteLine("aa");
#endif
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
#if DEBUG
FreeConsole();
#endif
}
}
引用的函数:
[System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)]
[return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
static extern bool AllocConsole();//开启
[System.Runtime.InteropServices.DllImport("Kernel32")]
public static extern void FreeConsole();//关闭
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "FindWindow")]
extern static IntPtr FindWindow(string lpClassName, string lpWindowName);//找出运行的窗口
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetSystemMenu")]
extern static IntPtr GetSystemMenu(IntPtr hWnd, IntPtr bRevert); //取出窗口运行的菜单
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "RemoveMenu")]
extern static IntPtr RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags); //灰掉按钮
[System.Runtime.InteropServices.DllImport("User32.dll ", EntryPoint = "SetParent")]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);//设置父窗体
[System.Runtime.InteropServices.DllImport("user32.dll ", EntryPoint = "ShowWindow")]
public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);//显示窗口
内嵌窗体(我嵌入给一个tabpages的一个页面)
if (AllocConsole())
{
Console.Title = "数据显示";//先定义窗口标题,防止未找到
windowHandle = FindWindow(null, Process.GetCurrentProcess().MainModule.FileName);
if (windowHandle != IntPtr.Zero)//在根目录下或VS下进入
{
Thread.Sleep(100);
SetParent(windowHandle, 数据显示.TabPages[0].Handle);
closeMenu = GetSystemMenu(windowHandle, IntPtr.Zero);
uint SC_CLOSE = 0xF060;
RemoveMenu(closeMenu, SC_CLOSE, 0x0);//屏蔽关闭按钮
}
else //快捷方式启动进入
{
MessageBox.Show("未找到父窗体,即将重定向!!!","重定向查找");
windowHandle = FindWindow(null, "数据显示");
if (windowHandle != IntPtr.Zero)
{
Thread.Sleep(100);
SetParent(windowHandle, 数据显示.TabPages[0].Handle);
closeMenu = GetSystemMenu(windowHandle, IntPtr.Zero);
uint SC_CLOSE = 0xF060;
RemoveMenu(closeMenu, SC_CLOSE, 0x0);//屏蔽关闭按钮
}
}
Console.WindowWidth = 100;
Console.SetWindowPosition(0, 0);
Console.ForegroundColor = ConsoleColor.Green;
ShowWindow(windowHandle, 3);
}
else
{
MessageBox.Show("请注意控制台窗口未正常打开,请检查环境!","环境缺失");
return;
}
最后:在绑定之前,父窗体一定要刷出来哈
先找到你的项目-》右键,选择属性
然后把输出类型改成控制台应用程序,保存
最后的效果:
转载于:https://www.cnblogs.com/yzw-carrie/p/5534173.html
引用:
namespace 测试使用 { public partial class Form1 : Form { [System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)] [return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)] static extern bool AllocConsole(); [System.Runtime.InteropServices.DllImport("Kernel32")] public static extern void FreeConsole(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { AllocConsole(); //开启控制台 } } }
输出一
通常的代码段: Console.WriteLine(“测试”);
效果显示:
输出二:
创建方法类来进行输出
Shell.WriteLine("注意:启动程序...");
Shell.WriteLine("\tWritten by wuming");
Shell.WriteLine("{0}:{1}", "警告", "这是一条警告信息。");
Shell.WriteLine("{0}:{1}", "错误", "这是一条错误信息!");
Shell.WriteLine("{0}:{1}", "注意", "这是一条需要的注意信息。");
Shell.WriteLine("");
Shell.WriteLine("测试",ConsoleColor.DarkRed);
static class Shell { /// <summary> /// 输出信息 /// </summary> /// <param name="format"></param> /// <param name="args"></param> public static void WriteLine(string message, ConsoleColor GetConsoleColor) { Console.ForegroundColor = GetConsoleColor; Console.WriteLine(@"[{0}]{1}", DateTimeOffset.Now, message); } /// <summary> /// 输出信息 /// </summary> /// <param name="format"></param> /// <param name="args"></param> public static void WriteLine(string format, params object[] args) { WriteLine(string.Format(format, args)); } /// <summary> /// 输出信息 /// </summary> /// <param name="output"></param> public static void WriteLine(string output) { Console.ForegroundColor = GetConsoleColor(output); Console.WriteLine(@"[{0}]{1}", DateTimeOffset.Now, output); } /// <summary> /// 根据输出文本选择控制台文字颜色 /// </summary> /// <param name="output"></param> /// <returns></returns> private static ConsoleColor GetConsoleColor(string output) { if (output.StartsWith("警告")) return ConsoleColor.Yellow; if (output.StartsWith("错误")) return ConsoleColor.Red; if (output.StartsWith("注意")) return ConsoleColor.Green; return ConsoleColor.Gray; } }
效果显示:
转载于:https://www.cnblogs.com/FHL007/p/11115064.html