使用控制台调试WinForm窗体程序
标签:rri forms new test ptr ret tle for dll
using System.Runtime.InteropServices;
/* TODO:使用方法
Win32.AllocConsole();
Console.WriteLine(@"我是调试信息:目前已经运行到加载事件中");
*/
namespace WinFormUseConsoleToTestProgramDemo
{
///
/// 通过控制台Console调试WinForm程序
/// LDH @ 2018-2-26
///
public class Win32DebuggerHelper
{
[DllImport("kernel32.dll")]
public static extern bool AllocConsole();
[DllImport("kernel32.dll")]
public static extern bool FreeConsole();
}
}
using System;
using System.Windows.Forms;
namespace WinFormUseConsoleToTestProgramDemo
{
///
/// WinForm中使用Console控制台显示调试信息,方便调试WinForm窗体程序
/// LDH @ 2018-2-26
///
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
#region 无边框窗体拖动API
///
/// 重写WndProc方法,实现窗体移动和禁止双击最大化
///
/// Windows 消息
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case 0x4e:
case 0xd:
case 0xe:
case 0x14:
base.WndProc(ref m);
break;
case 0x84: //鼠标点任意位置后可以拖动窗体
DefWndProc(ref m);
if (m.Result.ToInt32() == 0x01)
{
m.Result = new IntPtr(0x02);
}
break;
case 0xA3: //禁止双击最大化
break;
default:
base.WndProc(ref m);
break;
}
}
#endregion
///
/// 窗体加载事件
///
///
///
private void FrmMain_Load(object sender, EventArgs e)
{
FormBorderStyle = FormBorderStyle.None;
Win32DebuggerHelper.AllocConsole();
Console.Title = @"WinForm中使用Console控制台显示调试信息,方便调试WinForm窗体程序 By LDH";
}
///
/// 计算
///
///
///
private void btnCalculate_Click(object sender, EventArgs e)
{
Console.Clear();
Console.WriteLine(@"我已经运行到方法 private void btnCalculate_Click(object sender, EventArgs e)");
var textBoxNum1 = textBoxNumber1.Text.Trim();
var textBoxNum2 = textBoxNumber2.Text.Trim();
//if (string.IsNullOrEmpty(textBoxNum1))
//{
// textBoxNumber1.Focus();
// return;
//}
//if (string.IsNullOrEmpty(textBoxNum2))
//{
// textBoxNumber2.Focus();
// return;
//}
try
{
var num1 = Convert.ToDouble(textBoxNum1);
var num2 = Convert.ToDouble(textBoxNum2);
txtResult.Text = (num1/num2).ToString("F");
}
catch (Exception ex)
{
Console.WriteLine(@"###########################################################################################");
Console.WriteLine(@"异常出现位置:{1}{0}", Environment.NewLine, ex.StackTrace);
Console.WriteLine(@"异常方法:{1}{0}", Environment.NewLine, ex.TargetSite);
Console.WriteLine(@"###########################################################################################");
}
}
}
}
使用控制台调试WinForm窗体程序
标签:rri forms new test ptr ret tle for dll
原文地址:https://www.cnblogs.com/LifeDecidesHappiness/p/8471769.html
评论