如何添加一个控制台类似元素C#的WinForms程序控制台、元素、类似、程序

2023-09-03 00:26:01 作者:發現自己是個笨蛋

我有一个程序,监视调试消息,我已经用一个TextBox尝试和附加的消息,但它没有规模非常好,减慢一路下跌,当消息的数量变大。然后,我尝试了列表框,但附加新邮件时,滚动被抢购顶端。它也不会允许剪切和粘贴像文本框一样。

什么是更好的方式来实现类似的元素嵌入在一个WinForms窗口中的控制台。

编辑: 我仍希望能够嵌入类似Visual Studio中的输出窗口,但因为我想不出这里有一个简单的方法是两种解决方案我用。 除了使用该作品RichTextBox的,但你必须时不时地清除它的每一个。我用我的PInvoke控制台。下面是我写来处理这一点包装类。

 
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用了System.Runtime.InteropServices;

命名空间刀豆
{
   类Ext_Console
   {
      静态布尔console_on = FALSE;

      公共静态无效展(布尔上,串题)
      {
         console_on =上;
         如果(console_on)
         {
            AllocConsole();
            Console.Title =称号;
            //用它来改变颜色
            Console.BackgroundColor = System.ConsoleColor.White;
            Console.ForegroundColor = System.ConsoleColor.Black;

         }
         其他
         {
            FreeConsole();
         }
      }

      公共静态无效写入(字符串输出)
      {
         如果(console_on)
         {
            Console.Write(输出);
         }
      }

      公共静态无效的WriteLine(字符串输出)
      {
         如果(console_on)
         {
            Console.WriteLine(输出);
         }
      }

      [的DllImport(KERNEL32.DLL)
      公共静态外部布尔AllocConsole();
      [的DllImport(KERNEL32.DLL)
      公共静态外部布尔FreeConsole();
   }
}


//例如电话
Ext_Console.Write(控制台输出);
Ext_Console.WriteLine(控制台输出);
Ext_Console.Show(真正的控制台标题);


 

解决方案

RichTextBox中有一个AppendText方法是快。 它可以处理大量的文字好。 我相信这是最适合你的需要。

I have a program that monitors debug messages and I have tried using a TextBox and appended the messages to it but it doesn't scale very well and slows way down when the number of messages gets large. I then tried a ListBox but the scrolling was snapping to the top when appending new messages. It also doesn't allow for cut and paste like the text box does.

vs2015怎么创建控制台应用程序

What is a better way to implement a console like element embedded in a winforms window.

Edit: I would still like to be able to embed a output window like visual studio but since I can't figure out an easy way here are the two solutions I use. In addition to using the RichTextBox which works but you have to clear it every now and then. I use a console that I pinvoke. Here is a little wrapper class that I wrote to handle this.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Con
{
   class Ext_Console 
   {
      static bool console_on = false;

      public static void Show(bool on,string title)
      {
         console_on = on;
         if (console_on)
         {
            AllocConsole();
            Console.Title = title;
            // use to change color
            Console.BackgroundColor = System.ConsoleColor.White;
            Console.ForegroundColor = System.ConsoleColor.Black;

         }
         else
         {
            FreeConsole();
         }
      }

      public static void Write(string output)
      {
         if (console_on)
         {
            Console.Write(output);
         }
      }

      public static void WriteLine(string output)
      {
         if (console_on)
         {
            Console.WriteLine(output);
         }
      }

      [DllImport("kernel32.dll")]
      public static extern Boolean AllocConsole();
      [DllImport("kernel32.dll")]
      public static extern Boolean FreeConsole();
   }
}


// example calls
Ext_Console.Write("console output  ");
Ext_Console.WriteLine("console output");
Ext_Console.Show(true,"Title of console");


解决方案

RichTextBox has an AppendText method that is fast. And it can handle large text well. I believe it is the best for what you need.