tangxu
2025-02-26 2fc64c1af548596c4719d4a3abdc10de7a1d7e8f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace DPumpHydr.WinFrmUI.WenSkin.Controls
{
    public class LogListBox : WenControl
    {
        public LogListBox()
        {
            Initcom();
        }
        [Category("Wen"), DefaultValue(true), Description("是否目录下输出日志")]
        public bool LogSave { get; set; } = true;
 
        [Category("Wen"), DefaultValue(100), Description("日志超过数量自动清理")]
        public int LogClearAutomaticNumber { get; set; } = 100;
 
        #region 私有属性
 
        private WenListView logListView;
 
        #endregion
 
        #region 初始化
        private void Initcom()
        {
            this.Size = new System.Drawing.Size(500, 200);
            logListView = new WenListView() { Dock = DockStyle.Fill, View = View.Details };
 
            logListView.Columns.Add("时间", 160);
            logListView.Columns.Add("信息", 300);
            logListView.Columns.Add("状体", 60);
            this.Controls.Add(logListView);
 
            //logListView.SizeChanged += LogListView_SizeChanged;
            this.SizeChanged += LogListBox_SizeChanged;
        }
 
        private void LogListBox_SizeChanged(object sender, EventArgs e)
        {
            if (logListView != null)
            {
                int sizeWidth = logListView.Size.Width;
                if (sizeWidth < 300)
                    sizeWidth = 300;
                logListView.Columns[1].Width = sizeWidth - 240;
            }
        }
        #endregion
 
 
        #region 表格信息显示插入日志
 
        public void AddLog(string text, string info = "消息")
        {
            AddFromsInfo(text, info);
        }
 
        public void AddFormat(string format, params object[] args)
        {
            string str = string.Format(format, args);
            AddFromsInfo(str);
        }
        //插入信息到表格中
        public void AddFromsInfo(string listinfo)
        {
            AddFromsInfo(listinfo, "消息");
        }
        public void AddFromsInfo(string listinfo, string liststate)
        {
            string datetime = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
            WenListViewItem listViewItem = new WenListViewItem
            {
                Text = datetime
            };
            listViewItem.SubItems.Add(listinfo);
            listViewItem.SubItems.Add(liststate);
            if (this.IsHandleCreated)
            {
                logListView.Invoke(new Action(() =>
                {
                    logListView.Items.Insert(0, listViewItem);
                    //插入日志
                    if (LogSave)
                        InsertLog(datetime, listinfo, liststate);
                    LogClearAutomatic();
                }));
            }
            else
            {
                logListView.Items.Insert(0, listViewItem);
                //插入日志
                if (LogSave)
                    InsertLog(datetime, listinfo, liststate);
                LogClearAutomatic();
            }
        }
        //插入日志
        private void InsertLog(string datetime, string listinfo, string liststate)
        {
            string txtPath = Application.StartupPath + "\\Log\\";
            if (!Directory.Exists(txtPath))
                Directory.CreateDirectory(txtPath);
            File.AppendAllText(string.Format("{0}.txt", txtPath + DateTime.Now.ToString("yyyyMMdd"))
                , string.Format("{0},{1},{2}\r\n", datetime, listinfo, liststate), Encoding.Default);
        }
        #endregion
 
        /// <summary>
        /// 日志清理
        /// </summary>
        public void LogClear()
        {
            logListView.Items.Clear();
        }
 
        public void LogClearAutomatic()
        {
            int count = logListView.Items.Count;
            if (LogClearAutomaticNumber < logListView.Items.Count)
            {
                for (int i = LogClearAutomaticNumber; i < count; i++)
                {
                    logListView.Items[i].Remove();
                }
            }
        }
    }
}