LogControl.cs 1.7 KB
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TheMachine
{
    public partial class LogControl : UserControl
    {
        public LogControl()
        {
            InitializeComponent();
            showLogProDelegate = new ShowLogProDelegate(ShowLogPro);
            LogUtil.ShowLog += LogUtil_ShowLog;

        }
        delegate void ShowLogProDelegate(string msg, Color color);
        ShowLogProDelegate showLogProDelegate;
        private void LogUtil_ShowLog(string msg, Color color)
        {
            if (ParentForm==null)
                return;

            if (this.InvokeRequired)
                this.Invoke(showLogProDelegate, msg, color);
            else
                ShowLogPro(msg, color);

        }

        private List<string> logList = new List<string>();
        public int showCount = 30;
        private void ShowLogPro(string msg, Color color)
        {
            try
            {
                if (logList.Count >= showCount)
                {
                    logList.RemoveAt(0);
                }

                DateTime now = DateTime.Now;
                logList.Add(now.ToLongTimeString() + "  " + msg + Environment.NewLine);
                logBox.Text= string.Join("", logList);
                logBox.Select(logBox.Text.Length, 0);
                logBox.ScrollToCaret();

            }
            catch (Exception ex)
            {
                LogUtil.error("出错:" + ex.ToString());
            }
        }

        private void LogControl_Load(object sender, EventArgs e)
        {
            
        }
    }
}