tangxu
2024-10-22 6a07c4c846ffbb1e93afdf0260e123e4c145f419
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace DPumpHydr.WinFrmUI.WenSkin
{
    public partial class MsgBox : DPumpHydr.WinFrmUI.WenSkin.Forms.WenForm
    {
        public MsgBox()
        {
            InitializeComponent();
            this.SizeChanged += (s, e) =>
            {
                if (wenImageButton1 != null)
                    wenImageButton1.Width = (this.Width - this.FrameWidth * 2) / 2;
            };
            Text = "消息";
            mesIcon = MsgBoxIcon.Info;
            this.StartPosition = FormStartPosition.CenterScreen;
        }
 
        public MsgBox(string text) : this()
        {
            Message = text;
        }
        public MsgBox(string text, MsgBoxIcon mesBoxIcon) : this(text)
        {
            MesIcon = mesBoxIcon;
            switch (MesIcon)
            {
                case MsgBoxIcon.Asterisk:
                    Text = "提醒";
                    break;
                case MsgBoxIcon.Error:
                    Text = "错误";
                    break;
                case MsgBoxIcon.Info:
                    Text = "消息";
                    break;
                case MsgBoxIcon.Warning:
                    Text = "警告";
                    break;
                default:
                    break;
            }
        }
        public MsgBox(string text, string caption, MsgBoxIcon msg) : this(text,msg)
        {
            Text = caption;
        }
 
        #region 私有属性
 
        private string message;
        private MsgBoxIcon mesIcon;
 
        #endregion
 
 
        #region 公有属性
 
        public MsgBoxIcon MesIcon
        {
            get => mesIcon;
            set
            {
                mesIcon = value;
                this.Invalidate();
                if (value == MsgBoxIcon.Info)
                {
                    wenImageButton1.Visible = false;
                }
            }
        }
 
        public string Message
        {
            get => message;
            set
            {
                message = value;
 
                Graphics g = this.CreateGraphics();
                SizeF sizef = g.MeasureString(value, this.Font);
 
                int width = sizef.Width > 400 ? 400 : (int)sizef.Width + this.FrameWidth * 2 + 80;
                int height = this.TitleHeight + this.FrameWidth + panel1.Height;
 
                this.Size = new Size(this.Width < width ? width : this.Width, this.Height < height ? height : this.Height);
 
                this.Invalidate();
            }
        }
 
        #endregion
 
        public enum MsgBoxIcon
        {
            Asterisk,
            Error,
            Info,
            Warning,
        }
 
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
 
            Rectangle rec = new Rectangle(this.FrameWidth, this.TitleHeight, this.Width - this.FrameWidth * 2, this.Height - this.TitleHeight - this.FrameWidth - panel1.Height);
 
            Rectangle recStr = new Rectangle(rec.X + 70, rec.Y, rec.Width - 70, rec.Height);
 
            Rectangle recIco = new Rectangle(rec.X + 5, rec.Y + (rec.Height - 60) / 2, 60, 60);
 
            Graphics g = e.Graphics;
 
            using StringFormat sf = new StringFormat(StringFormatFlags.NoClip)
            {
                LineAlignment = StringAlignment.Center,
                Trimming = StringTrimming.EllipsisCharacter
            };
 
            g.DrawString(Message, Font, new SolidBrush(this.ForeColor), recStr, sf);
 
            switch (MesIcon)
            {
                case MsgBoxIcon.Asterisk:
                    g.DrawImage(Properties.Resources.Asterisk, recIco);
                    break;
                case MsgBoxIcon.Error:
                    g.DrawImage(Properties.Resources.error, recIco);
                    break;
                case MsgBoxIcon.Info:
                    g.DrawImage(Properties.Resources.Info, recIco);
                    break;
                case MsgBoxIcon.Warning:
                    g.DrawImage(Properties.Resources.Warning, recIco);
                    break;
                default:
                    break;
            }
        }
 
        private void wenImageButton2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
 
        private void wenImageButton1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}