这里没什么好奇怪的,这只是调试窗口的基础,有人可以使用它在Windows应用程序中输出调试文本。 它提供了相当基本的功能,因此可以随时进行修改以适合您的目的。
这个话题出现在一个问题线程中(某种程度上),大多数情况下它使我想起我一直想成为其中一个,所以我想与他人分享。 重要的是要注意,在Windows应用程序中还有其他收集调试信息的方法。 这只是以相当轻量级的形式打包的一种这样做的方法。
如有任何明显问题,随时通知我,请尽情享受。 编码愉快!
using System;
using System.Windows.Forms;
using System.Drawing;
namespace SomeNamespace
{
public class DebugWindow
{
#region Public Enum
/// <summary>
/// The ways in which a debug window can write its text
/// </summary>
public enum WriteModes
{
Prepend,
Append
}
#endregion
#region Private Members
private Form m_hostForm = new Form();
private RichTextBox m_console = new RichTextBox();
private WriteModes m_writeMode = WriteModes.Append;
#endregion
#region Constructor
public DebugWindow()
{
// Set up our form and put a textbox on it
m_hostForm.Size = new Size(600, 400);
m_hostForm.FormClosing += new FormClosingEventHandler(m_hostForm_FormClosing);
m_hostForm.MinimizeBox = false;
m_hostForm.MaximizeBox = false;
m_hostForm.Text = "Debug";
m_console.Location = new Point(0, 0);
m_console.Size = new Size(m_hostForm.Width - 8, m_hostForm.Height - 35);
m_console.Anchor = AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;
m_console.ReadOnly = true;
m_console.BorderStyle = BorderStyle.None;
m_hostForm.Controls.Add(m_console);
}
#endregion
#region Public Properties
/// <summary>
/// The write mode for this debug window
/// </summary>
public WriteModes WriteMode
{
get { return m_writeMode; }
set { m_writeMode = value; }
}
/// <summary>
/// The window location
/// </summary>
public Point Location
{
get { return m_hostForm.Location; }
set
{
m_hostForm.Location = new Point(value.X, value.Y);
m_hostForm.StartPosition = FormStartPosition.Manual;
}
}
/// <summary>
/// The window size
/// </summary>
public Size Size
{
get { return m_hostForm.Size; }
set { m_hostForm.Size = new Size(value.Width, value.Height); }
}
/// <summary>
/// The debug text for this window
/// </summary>
public string DebugText
{
get { return m_console.Text; }
}
/// <summary>
/// The title for this debug window
/// </summary>
public string WindowTitle
{
get { return m_hostForm.Text; }
set { m_hostForm.Text = value; }
}
#endregion
#region Public Methods
/// <summary>
/// Show the debug window
/// </summary>
public void Show()
{
m_hostForm.Show();
}
/// <summary>
/// Hide the debug window
/// </summary>
public void Hide()
{
m_hostForm.Hide();
}
/// <summary>
/// Writes a string to the debug window, ending in a line termination character
/// </summary>
/// <param name="str">The string to write</param>
public void WriteLine(string str)
{
Write(str);
Write(Environment.NewLine);
}
/// <summary>
/// Writes a line termination character to the debug window
/// </summary>
public void WriteLine()
{
Write(Environment.NewLine);
}
/// <summary>
/// Writes a string to the current debug window
/// </summary>
/// <param name="str"></param>
public void Write(string str)
{
// Write depending on the write mode
switch (m_writeMode)
{
case WriteModes.Append:
m_console.AppendText(str);
break;
case WriteModes.Prepend:
m_console.Text = str + m_console.Text;
break;
}
}
/// <summary>
/// Clears the debug window
/// </summary>
public void Clear()
{
m_console.Clear();
}
#endregion
#region Event Handlers
void m_hostForm_FormClosing(object sender, FormClosingEventArgs e)
{
// Don't allow the form to be disposed...
this.Hide();
e.Cancel = true;
}
#endregion
}
}
注意:将名称空间更改为所需的名称。
要使用此功能,只需为表单(或静态类)实例化调试窗口,然后将调试消息放在需要信息的重要位置即可。 在您的程序运行时,它将调试消息输出到此窗口,如果显示它,您将能够看到它,就好像您拥有控制台一样。
翻译自: https://bytes.com/topic/c-sharp/insights/889523-quick-n-dirty-debug-window