Winform中嵌入Console控制台

本文介绍了一种在C#中通过调用Win32API实现禁用控制台关闭按钮的方法,并提供了具体的源代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://blog.youkuaiyun.com/wangzi041/article/details/4269954

最近做了小软件,里面要用到实时显示日志;

 

本来呢,是自己做个累似的窗口,原理就是用定时读取文本文件,但是发现如果文本文件到了100K的时候,程序会显得很卡;

 

所以不得已还是得用Console控制台。

 

目前来说,网上有两种方法,一种是将项目的输出改成控制台方式;另一种就是调用win32 api来运行控制台...

 

本人更倾向于第二种方式;

 

但是在做的过程中碰到了一个问题,就是关闭控制台的话,整个程序就关闭掉了...这个问题我还无法解决。为了避免直接点击关闭按钮,在程序中把叉按钮灰掉了,这样就可以避免掉点击了~!

 

话不多说。贴源码

 

一个调用Win32 API的类

 

[c-sharp] view plain copy print ?

[DllImport("Kernel32.dll")] private static extern bool AllocConsole(); //启动窗口 [DllImport("kernel32.dll", EntryPoint = "FreeConsole")] private static extern bool FreeConsole(); //释放窗口,即关闭 [DllImport("user32.dll", EntryPoint = "FindWindow")] extern static IntPtr FindWindow(string lpClassName, string lpWindowName);//找出运行的窗口 [DllImport("user32.dll", EntryPoint = "GetSystemMenu")] extern static IntPtr GetSystemMenu(IntPtr hWnd, IntPtr bRevert); //取出窗口运行的菜单 [DllImport("user32.dll", EntryPoint = "RemoveMenu")] extern static IntPtr RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags); //灰掉按钮

 

 

[DllImport("Kernel32.dll")]  
       private static extern bool AllocConsole(); //启动窗口   
       [DllImport("kernel32.dll", EntryPoint = "FreeConsole")]  
       private static extern bool FreeConsole();      //释放窗口,即关闭   
       [DllImport("user32.dll", EntryPoint = "FindWindow")]  
       extern static IntPtr FindWindow(string lpClassName, string lpWindowName);//找出运行的窗口   
  
       [DllImport("user32.dll", EntryPoint = "GetSystemMenu")]  
       extern static IntPtr GetSystemMenu(IntPtr hWnd, IntPtr bRevert); //取出窗口运行的菜单   
  
       [DllImport("user32.dll", EntryPoint = "RemoveMenu")]  
       extern static IntPtr RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags); //灰掉按钮  

 

调用方法如下

 

[c-sharp] view plain copy print ?
AllocConsole();  
IntPtr windowHandle = FindWindow(null, Process.GetCurrentProcess().MainModule.FileName);  
IntPtr closeMenu = GetSystemMenu(windowHandle, IntPtr.Zero);  
uint SC_CLOSE = 0xF060;  
RemoveMenu(closeMenu, SC_CLOSE, 0x0);   
for(int i=0;i<100;i++)  
{  
    Console.WriteLine("i=:" + i);                  
}  

 

AllocConsole(); IntPtr windowHandle = FindWindow(null, Process.GetCurrentProcess().MainModule.FileName); IntPtr closeMenu = GetSystemMenu(windowHandle, IntPtr.Zero); uint SC_CLOSE = 0xF060; RemoveMenu(closeMenu, SC_CLOSE, 0x0); for(int i=0;i<100;i++) { Console.WriteLine("i=:" + i); }

 

 

另外还可以调用 如下方法来改变标题的名称!

 

[c-sharp] view plain copy print ?
[DllImport("Kernel32.dll")]  
       public static extern bool SetConsoleTitle(string strMessage);  

 

转载于:https://www.cnblogs.com/hellolong/articles/2777308.html

### 如何在 WinForms 应用程序中嵌入控制台窗口 为了实现将控制台窗口集成到 Windows Forms (WinForms) 应用程序中的功能,可以创建自定义控件或利用现有库。一种常见方法是通过 `System.Diagnostics.Process` 类启动一个新的进程并捕获其标准输入、输出流。 下面展示了一个简单的例子,该示例展示了如何在一个 Panel 控件内显示来自子进程中 Console 的文本: ```csharp using System; using System.Diagnostics; using System.IO; using System.Text; using System.Windows.Forms; public class EmbeddedConsole : Form { private TextBox outputTextBox; private Process process; public EmbeddedConsole() { this.outputTextBox = new TextBox(); this.outputTextBox.Dock = DockStyle.Fill; this.outputTextBox.Multiline = true; this.outputTextBox.ScrollBars = ScrollBars.Vertical; this.Controls.Add(this.outputTextBox); StartEmbeddedProcess(); } private void StartEmbeddedProcess() { var startInfo = new ProcessStartInfo("cmd.exe") { RedirectStandardOutput = true, UseShellExecute = false }; process = new Process { StartInfo = startInfo }; process.OutputDataReceived += OnOutputDataReceived; process.Start(); // Begin asynchronous read of the stdout stream. process.BeginOutputReadLine(); } protected override void OnFormClosing(FormClosingEventArgs e){ base.OnFormClosing(e); if(process != null && !process.HasExited){ try{ process.Kill(); }catch(Exception ex){ MessageBox.Show($"Failed to kill process: {ex.Message}"); } } } private void OnOutputDataReceived(object sender, DataReceivedEventArgs args) { Invoke((Action)(() => { if (!string.IsNullOrEmpty(args.Data)) { outputTextBox.AppendText($"{args.Data}\r\n"); } })); } } ``` 此代码片段实现了基本的功能[^1]。请注意,这段代码仅用于演示目的;实际应用时可能还需要处理更多细节,比如错误消息重定向、命令行参数传递以及更复杂的交互逻辑等。 #### 注意事项 - 此解决方案适用于简单场景下的命令行工具调用。 - 对于复杂的应用需求,建议考虑其他方案,如使用专门设计的支持多平台特性的第三方组件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值