ホテル管理システム
ogi
yesterday 1a1c8e71fcd14858f595029f089b2d4a00202b32
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;
using HotelPms.Share.Windows.Animations;
using System;
using System.Drawing.Imaging;
 
namespace HotelPms.Share.Windows.Component
{
    public class MaterialButton : Button
    {
        private float radius = 10F;
 
        /// <summary>
        /// 半径
        /// </summary>
        public float Radius
        { 
            get { return radius; }
            set { radius = value; Invalidate(); }                 
        }
 
        private readonly AnimationManager _animationManager;
 
        private bool m_Selected = false;
        private bool m_MouseFocused = false;
        private StringFormat m_StringFormat = new StringFormat();
 
        //public event EventHandler ClickFinished;
        public new event EventHandler Click;
 
        public MaterialButton()
        {
            BackColor = Color.FromArgb(255, 0, 105, 217);
            ForeColor = Color.White;
            Cursor = Cursors.Hand;
            _animationManager = new AnimationManager(false)
            {
                Increment = 0.03,
                AnimationType = AnimationType.EaseOut
            };
            _animationManager.OnAnimationProgress += sender => Invalidate();
            _animationManager.OnAnimationFinished += _animationManager_OnAnimationFinished;
 
            FlatStyle = FlatStyle.Flat;
            //AutoSizeMode = AutoSizeMode.GrowAndShrink;
            //AutoSize = true;
        }
 
        private void _animationManager_OnAnimationFinished(object sender)
        {
            Click?.Invoke(this, new EventArgs());
            //ClickFinished?.Invoke(this, new EventArgs());
        }
 
        protected override void Dispose(bool disposing)
        {
            m_StringFormat.Dispose();
            base.Dispose(disposing);
        }
 
        public override string Text
        {
            get { return base.Text; }
            set
            {
                base.Text = value;
                Invalidate();
            }
        }
 
        protected override void OnEnter(EventArgs e)
        {
            base.OnEnter(e);
            m_Selected = true;
        }
 
        protected override void OnLeave(EventArgs e)
        {
            base.OnLeave(e);
            m_Selected = false;
        }
 
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            m_MouseFocused = true;
        }
 
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            m_MouseFocused = false;
        }
 
        protected override void OnMouseUp(MouseEventArgs mevent)
        {
            base.OnMouseUp(mevent);
 
            _animationManager.StartNewAnimation(AnimationDirection.In, mevent.Location);
        }
 
        protected override void OnPreviewKeyDown(PreviewKeyDownEventArgs e)
        {
            base.OnPreviewKeyDown(e);
 
            if (e.KeyCode == Keys.Enter)
            {
                _animationManager.StartNewAnimation(AnimationDirection.In, new Point(Width / 2, Height / 2));
            }
        }
 
        protected override void OnPaint(PaintEventArgs pevent)
        {
            var g = pevent.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;
 
            g.Clear(Parent.BackColor);
 
            if (radius.CompareTo(0F) <= 0)
            {
                Color beginColor = m_MouseFocused ? (FlatAppearance.MouseOverBackColor == Color.Empty ? ControlPaint.Light(BackColor) : FlatAppearance.MouseOverBackColor) : m_Selected ? (FlatAppearance.MouseDownBackColor == Color.Empty ? ControlPaint.Light(BackColor) : FlatAppearance.MouseDownBackColor) : BackColor;
                using (SolidBrush backBrush = new SolidBrush(beginColor))
                {
                    g.FillRectangle(backBrush, ClientRectangle);
                }
 
                //線
                if (this.FlatAppearance.BorderSize > 0 && !this.FlatAppearance.BorderColor.Equals(Color.Empty))
                {
                    using (Pen pen = new Pen(this.FlatAppearance.BorderColor, this.FlatAppearance.BorderSize))
                    {
                        g.DrawRectangle(pen, ClientRectangle);
                    }
                }
            }
            else
            {
                using (var backgroundPath = CreateRoundRect(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width - 1, ClientRectangle.Height - 1, radius))
                {
                    Color beginColor = m_MouseFocused ? (FlatAppearance.MouseOverBackColor == Color.Empty ? ControlPaint.Light(BackColor) : FlatAppearance.MouseOverBackColor) : m_Selected ? (FlatAppearance.MouseDownBackColor == Color.Empty ? ControlPaint.LightLight(BackColor) : FlatAppearance.MouseDownBackColor) : BackColor;
                    using (SolidBrush backBrush = new SolidBrush(beginColor))
                    {
                        g.FillPath(backBrush, backgroundPath);
                    }
 
                    //線
                    if (this.FlatAppearance.BorderSize > 0 && !this.FlatAppearance.BorderColor.Equals(Color.Empty))
                    {
                        using (Pen pen = new Pen(this.FlatAppearance.BorderColor, this.FlatAppearance.BorderSize))
                        {
                            g.DrawPath(pen, backgroundPath);
                        }
                    }
                }
            }
 
            if (_animationManager.IsAnimating())
            {
                for (int i = 0; i < _animationManager.GetAnimationCount(); i++)
                {
                    var animationValue = _animationManager.GetProgress(i);
                    var animationSource = _animationManager.GetSource(i);
                    using (var rippleBrush = new SolidBrush(Color.FromArgb((int)(51 - (animationValue * 50)), Color.White)))
                    {
                        var rippleSize = (int)(animationValue * Width * 2);
                        g.FillEllipse(rippleBrush, new Rectangle(animationSource.X - rippleSize / 2, animationSource.Y - rippleSize / 2, rippleSize, rippleSize));
                    }
                }
            }
 
            Rectangle textR = ClientRectangle;
            if (Image != null)
            {
                int x = 5;
                float rate = 0.5F;
                int width = (int)(ClientRectangle.Width * 0.3) - x;
                int height = (int)(ClientRectangle.Height * rate);
 
                //Zoom
                if ((double)Image.Width / (double)Image.Height > (double)width / (double)height) // image is wider 
                {
                    height = (int)((double)Image.Height / (double)Image.Width * (double)width);
                }
                else
                {
                    width = (int)((double)Image.Width / (double)Image.Height * (double)height);
                }
 
                //居中
                Rectangle imgRect = new Rectangle(x, (ClientRectangle.Height - height) / 2, width, height);
                ImageAttributes vAttr = new ImageAttributes();
                vAttr.SetColorKey((Image as Bitmap).GetPixel(0, 0), (Image as Bitmap).GetPixel(0, 0));
                pevent.Graphics.DrawImage(Image, imgRect, 0, 0, Image.Width, Image.Height, GraphicsUnit.Pixel, vAttr);
 
                textR = new Rectangle(new Point(x + ClientRectangle.X + width, ClientRectangle.Y), new Size(ClientRectangle.Width - width, ClientRectangle.Height));
            }
 
            SetStringFormat();
            using (SolidBrush foreBrush = new SolidBrush(this.ForeColor))
            {
                g.DrawString(Text, Font, foreBrush, textR, m_StringFormat);
            }
        }
 
        public static GraphicsPath CreateRoundRect(float x, float y, float width, float height, float radius)
        {
            var gp = new GraphicsPath();
            gp.AddLine(x + radius, y, x + width - (radius * 2), y);
            gp.AddArc(x + width - (radius * 2), y, radius * 2, radius * 2, 270, 90);
            gp.AddLine(x + width, y + radius, x + width, y + height - (radius * 2));
            gp.AddArc(x + width - (radius * 2), y + height - (radius * 2), radius * 2, radius * 2, 0, 90);
            gp.AddLine(x + width - (radius * 2), y + height, x + radius, y + height);
            gp.AddArc(x, y + height - (radius * 2), radius * 2, radius * 2, 90, 90);
            gp.AddLine(x, y + height - (radius * 2), x, y + radius);
            gp.AddArc(x, y, radius * 2, radius * 2, 180, 90);
            gp.CloseFigure();
            return gp;
        }
 
        public static GraphicsPath CreateRoundRect(Rectangle rect, float radius)
        {
            return CreateRoundRect(rect.X, rect.Y, rect.Width, rect.Height, radius);
        }
 
        private void SetStringFormat()
        {
            switch (this.TextAlign)
            {
                case ContentAlignment.BottomCenter:
                case ContentAlignment.BottomLeft:
                case ContentAlignment.BottomRight:
                    m_StringFormat.LineAlignment = StringAlignment.Far;
                    break;
                case ContentAlignment.MiddleCenter:
                case ContentAlignment.MiddleLeft:
                case ContentAlignment.MiddleRight:
                    m_StringFormat.LineAlignment = StringAlignment.Center;
                    break;
                case ContentAlignment.TopCenter:
                case ContentAlignment.TopLeft:
                case ContentAlignment.TopRight:
                    m_StringFormat.LineAlignment = StringAlignment.Near;
                    break;
            }
 
            switch (this.TextAlign)
            {
                case ContentAlignment.TopLeft:
                case ContentAlignment.BottomLeft:
                case ContentAlignment.MiddleLeft:
                    m_StringFormat.Alignment = StringAlignment.Near;
                    break;
                case ContentAlignment.MiddleCenter:
                case ContentAlignment.BottomCenter:
                case ContentAlignment.TopCenter:
                    m_StringFormat.Alignment = StringAlignment.Center;
                    break;
                case ContentAlignment.BottomRight:
                case ContentAlignment.MiddleRight:
                case ContentAlignment.TopRight:
                    m_StringFormat.Alignment = StringAlignment.Far;
                    break;
            }
        }
    }
}