using HotelPms.Share.Windows.GraphicsApi; using HotelPms.Share.Windows.Util; using System; using System.ComponentModel; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Windows.Forms; namespace HotelPms.Share.Windows.Component { public class CountButton : System.Windows.Forms.Button { private bool m_DisableClickEvent = true; /// /// Click連打禁止 【※注意:ボタンイベントの中でFrom.Show()を使うとShownイベントなども消える場合があります。】 /// [Description("Click連打禁止 【※注意:ボタンイベントの中でFrom.Show()を使うとShownイベントなども消える場合があります。】"), Category("拡張動作"), DefaultValue(true)] public bool DisableClickEvent { get { return m_DisableClickEvent; } set { m_DisableClickEvent = value; } } private LinearGradientMode m_GradientMode = LinearGradientMode.Horizontal; [Description("GradientMode"), Category("拡張動作"), DefaultValue(LinearGradientMode.Horizontal)] public LinearGradientMode GradientMode { get { return m_GradientMode; } set { m_GradientMode = value; Invalidate(); } } private Color m_GradientColor = Color.Empty; [Description("GradientColor"), Category("拡張動作")] public Color GradientColor { get { return m_GradientColor; } set { m_GradientColor = value; Invalidate(); } } private int m_Count = 0; [Description("Count"), Category("拡張動作")] public int Count { get { return m_Count; } set { m_Count = value; Invalidate(); } } private Color m_CountForeColor = Color.White; [Description("CountForeColor"), Category("拡張動作")] public Color CountForeColor { get { return m_CountForeColor; } set { m_CountForeColor = value; Invalidate(); } } private Color m_CountBackColor = Color.Red; [Description("CountBackColor"), Category("拡張動作")] public Color CountBackColor { get { return m_CountBackColor; } set { m_CountBackColor = value; Invalidate(); } } private bool m_Selected = false; private bool m_MouseFocused = false; private StringFormat m_StringFormat = new StringFormat(); private Image imgEllipse = null; public CountButton() { FlatStyle = FlatStyle.Flat; } protected override void Dispose(bool disposing) { if (imgEllipse != null) { imgEllipse.Dispose(); } m_StringFormat.Dispose(); base.Dispose(disposing); } protected override void OnPaint(PaintEventArgs pevent) { base.OnPaint(pevent); if (this.FlatStyle != FlatStyle.Flat) { return; } if (this.Image == null) { return; } //ControlPaint.Light Color beginColor = this.m_MouseFocused ? (FlatAppearance.MouseOverBackColor == Color.Empty ? ControlPaint.Light(this.BackColor) : FlatAppearance.MouseOverBackColor) : m_Selected ? (FlatAppearance.MouseDownBackColor == Color.Empty ? ControlPaint.Light(this.BackColor) : FlatAppearance.MouseDownBackColor) : this.BackColor; Color endColor = m_GradientColor == Color.Empty ? beginColor : this.m_MouseFocused ? ControlPaint.LightLight(m_GradientColor) : m_Selected ? ControlPaint.Light(m_GradientColor) : m_GradientColor; //using (LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle, Color.FromArgb(0, 136, 0), Color.FromArgb(0, 166, 0), m_GradientMode)) using (LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle, beginColor, endColor, m_GradientMode)) { pevent.Graphics.FillRectangle(brush, this.ClientRectangle); } //線 if (this.FlatAppearance.BorderSize > 0 && !this.FlatAppearance.BorderColor.Equals(Color.Empty)) { using (Pen pen = new Pen(this.FlatAppearance.BorderColor, this.FlatAppearance.BorderSize)) { pevent.Graphics.DrawRectangle(pen, new Rectangle(this.ClientRectangle.X, this.ClientRectangle.Y, this.ClientRectangle.Width - this.FlatAppearance.BorderSize, this.ClientRectangle.Height - this.FlatAppearance.BorderSize)); } } float rate = 0.7F; int width = (int)(ClientRectangle.Width * rate); int height = (int)(ClientRectangle.Height * rate); Rectangle imgRect = new Rectangle((ClientRectangle.Width - width) / 2, (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); string val = Count.ToString(); Size textSize = TextRenderer.MeasureText(val, this.Font); textSize = new Size((int)(textSize.Width * 1.1F), (int)(textSize.Height * 1.1F)); //上余白、右余白固定 Rectangle textRect = new Rectangle(new Point(ClientRectangle.Width - textSize.Width - (int)(height * 0.2F), (int)(height * 0.2F)), textSize); using (SolidBrush textBackBrush = new SolidBrush(m_CountBackColor)) { GdiPlus.FillRoundRectangle(pevent.Graphics, textBackBrush, textRect, 5); } using (SolidBrush foreBrush = new SolidBrush(m_CountForeColor)) { TextRenderer.DrawText(pevent.Graphics, val, this.Font, textRect, m_CountForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter); } } 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; } } 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 OnClick(EventArgs e) { //System.Diagnostics.Debug.WriteLine("base.OnClick begin"); base.OnClick(e); //System.Diagnostics.Debug.WriteLine("base.OnClick end "); //保留されているメッセージを削除し、2度押しを許可しない if (m_DisableClickEvent) { RemovePeekMessage.Invoke(); } } } }