ucErrorDisplay.cs 6.19 KB
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace HaierExe
{
    public partial class ucErrorDisplay : UserControl
    {
        
        public ucErrorDisplay()
        {
            InitializeComponent();
            //原点
            EF = label2.Location;
            this.FontChanged += new EventHandler(ucErrorDisplay_FontChanged);
            this.ForeColorChanged += new EventHandler(ucErrorDisplay_ForeColorChanged);
            this.BackColorChanged += new EventHandler(ucErrorDisplay_BackColorChanged);
        }

        void ucErrorDisplay_BackColorChanged(object sender, EventArgs e)
        {
            label1.BackColor = this.BackColor;
            //throw new NotImplementedException();
        }

        void ucErrorDisplay_ForeColorChanged(object sender, EventArgs e)
        {
            label1.ForeColor = this.ForeColor;
            //throw new NotImplementedException();
        }

        void ucErrorDisplay_FontChanged(object sender, EventArgs e)
        {
            label1.Font = this.Font;
            //throw new NotImplementedException();
        }
        /// <summary>
        /// 移动的速度
        /// </summary>
        private int _SpeedMove=2;
        public int SpeedMove
        {
            get
            {
                return _SpeedMove;
            }
            set
            {
                if (value >= 0&&value<=50)
                {
                    if (_SpeedMove != value)
                    {
                        _SpeedMove = value;
                    }
                }
            }
        }
        /// <summary>
        /// 移动的时间间隔
        /// </summary>
        private int _Interval = 500;
        public int Interval
        {
            get 
            {
                return _Interval;
            }
            set
            {
                if (value > 100 && value < 5000)
                {
                    if (_Interval != value)
                    {
                        _Interval = value;
                        timer1.Interval = _Interval;
                    }
                }
            }
        }
        /// <summary>
        /// 定时器移动
        /// </summary>
        private bool _Enable = false;
        public bool MoveEnable
        {
            get
            {
                return _Enable;
            }
            set
            {
                if (_Enable != value)
                {
                    _Enable = value;
                    timer1.Enabled = _Enable;
                    if (_Enable == false)
                    {
                        label1.Location = PF;
                    }
                    else
                    {
                        label1.Location = EF;
                    }
                }
            }
        }

        /// <summary>
        /// 原点
        /// </summary>
        private Point PF = new Point(1,0);
        /// <summary>
        /// 移动起始点
        /// </summary>
        private Point EF = new Point();
        /// <summary>
        /// 文件名称
        /// </summary>
        private string _strPathName;

        private string PreTime = "";
        /// <summary>
        /// 错误信息
        /// </summary>
        private string _ErrText;
        public string ErrText
        {
            get 
            {
                return _ErrText;
            }
            set
            {
                
                if (value!=null && value != "" && value.Length <= 65535)
                {
                    if (_ErrText != value)
                    {
                        _ErrText = value;
                        PreTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                        if (_ErrText != "")
                        {
                            label1.Text = PreTime + "发生错误:" + _ErrText;
                            writeErrorLog(label1.Text);
                        }
                    }
                }
                else if (value=="")
                {
                    label1.Text = "";
                }

            }
        }
        private int x;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (label1.Location.X > PF.X - label1.Width && label1.Location.X< this.Width - label2.Width)
            {
                x -= SpeedMove;
                if (x >= 0 && x <= EF.X)
                {
                    label1.Location = new Point(x, PF.Y);
                }
                else
                {
                    x = EF.X;
                }
            }
            
        }

        /// <summary>
        /// 错误日志记录
        /// </summary>
        /// <param name="errorTxt"></param>
        private void writeErrorLog(string errorTxt)
        {
            try
            {
                string strPath =  "D:\\SunEast\\Log\\ErrorLogRecord";
                string strPathName = strPath + string.Format("\\{0}.txt", DateTime.Now.ToString("yyyy-MM-dd"));
                _strPathName = strPathName;
                if (!Directory.Exists(strPath))
                {
                   Directory.CreateDirectory(strPath);
                }
                if (errorTxt != "")
                {
                    StreamWriter sw = new StreamWriter(_strPathName);
                    sw.WriteLine(errorTxt);
                    sw.Close();
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// 错误日志查看
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void label2_Click(object sender, EventArgs e)
        {
            try
            {
                if (_strPathName.Length > 0)
                {
                    System.Diagnostics.Process.Start(_strPathName);
                    ErrText = "";
                } 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}