使用控制台调试WinForm窗体程序

2019/7/21 16:15:38 人评论 次浏览 分类:学习教程

原文链接:http://www.cnblogs.com/LifeDecidesHappiness/p/8471769.html
1.程序代码结构

2.Win32DebuggerHelper.cs
using System.Runtime.InteropServices;

/*  TODO:使用方法
         Win32.AllocConsole();
         Console.WriteLine(@"我是调试信息:目前已经运行到加载事件中"); 
*/

namespace WinFormUseConsoleToTestProgramDemo
{
    /// <summary>
    ///     通过控制台Console调试WinForm程序
    ///     LDH @ 2018-2-26
    /// </summary>
    public class Win32DebuggerHelper
    {
        [DllImport("kernel32.dll")]
        public static extern bool AllocConsole();

        [DllImport("kernel32.dll")]
        public static extern bool FreeConsole();
    }
}
3.FrmMain.cs
using System;
using System.Windows.Forms;

namespace WinFormUseConsoleToTestProgramDemo
{
    /// <summary>
    ///     WinForm中使用Console控制台显示调试信息,方便调试WinForm窗体程序
    ///     LDH @ 2018-2-26
    /// </summary>
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        #region 无边框窗体拖动API

        /// <summary>
        ///     重写WndProc方法,实现窗体移动和禁止双击最大化
        /// </summary>
        /// <param name="m">Windows 消息</param>
        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

        /// <summary>
        ///     窗体加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmMain_Load(object sender, EventArgs e)
        {
            FormBorderStyle = FormBorderStyle.None;
            Win32DebuggerHelper.AllocConsole();
            Console.Title = @"WinForm中使用Console控制台显示调试信息,方便调试WinForm窗体程序 By LDH";
        }

        /// <summary>
        ///     计算
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        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(@"###########################################################################################");
            }
        }
    }
}
4.程序运行录像

转载于:https://www.cnblogs.com/LifeDecidesHappiness/p/8471769.html

相关资讯

    暂无相关的资讯...

共有访客发表了评论 网友评论

验证码: 看不清楚?
    -->