public RoundButton() { Width = 65; Height = 30; stroke = true; strokeColor = Color.Gray; // grey //inactive1 = Color.FromArgb(224, 224, 224); //inactive2 = Color.FromArgb(224, 224, 224); //active1 = Color.FromArgb(192, 192, 192); //active2 = Color.FromArgb(192, 192, 192); // blue inactive1 = Color.FromArgb(215, 228, 242); inactive2 = Color.FromArgb(185, 209, 234); active1 = Color.FromArgb(135, 206, 235); active2 = Color.FromArgb(215, 228, 242); strokeColor = Color.FromArgb(215, 228, 242);; radius = 4; roundedRect = new RoundedRectangleF(Width, Height, radius); SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint, true); BackColor = Color.Transparent; ForeColor = Color.Black; Font = new Font("Segoe UI", 8, FontStyle.Bold); state = MouseState.Leave; Transparency = false; }
protected override void OnPaint(PaintEventArgs e) { #region Transparency if (Transparency) { ColourHelper.MakeTransparent(this, e.Graphics); } #endregion #region Drawing e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear; e.Graphics.CompositingQuality = CompositingQuality.HighQuality; e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; roundedRect = new RoundedRectangleF(Width, Height, radius); e.Graphics.FillRectangle(Brushes.Transparent, ClientRectangle); var R1 = (active1.R + inactive1.R) / 2; var G1 = (active1.G + inactive1.G) / 2; var B1 = (active1.B + inactive1.B) / 2; var R2 = (active2.R + inactive2.R) / 2; var G2 = (active2.G + inactive2.G) / 2; var B2 = (active2.B + inactive2.B) / 2; var rect = new Rectangle(0, 0, Width, Height); if (Enabled) { if (state == MouseState.Leave) { using (var inactiveGB = new LinearGradientBrush(rect, inactive1, inactive2, 90f)) { e.Graphics.FillPath(inactiveGB, roundedRect.Path); } } else if (state == MouseState.Enter) { using (var activeGB = new LinearGradientBrush(rect, active1, active2, 90f)) { e.Graphics.FillPath(activeGB, roundedRect.Path); } } else if (state == MouseState.Down) { using (var downGB = new LinearGradientBrush(rect, Color.FromArgb(R1, G1, B1), Color.FromArgb(R2, G2, B2), 90f)) { e.Graphics.FillPath(downGB, roundedRect.Path); } } if (stroke) { using (var pen = new Pen(strokeColor, 1)) using (var path = new RoundedRectangleF(Width - (radius > 0 ? 0 : 1), Height - (radius > 0 ? 0 : 1), radius).Path) { e.Graphics.DrawPath(pen, path); } } } else { var linear1 = Color.FromArgb(224, 224, 224); var linear2 = Color.FromArgb(224, 224, 224); using (var inactiveGB = new LinearGradientBrush(rect, linear1, linear2, 90f)) { e.Graphics.FillPath(inactiveGB, roundedRect.Path); e.Graphics.DrawPath(new Pen(inactiveGB), roundedRect.Path); } } #endregion #region Text Drawing using (var sf = new StringFormat { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center }) using (Brush brush = new SolidBrush(ForeColor)) { if (this.Image == null) { e.Graphics.DrawString(Text, Font, brush, ClientRectangle, sf); } else { e.Graphics.DrawString(Text, Font, brush, ClientRectangle.Width / 2 + (Image.Width / 2), ClientRectangle.Height / 2 + 1, sf); e.Graphics.DrawImage(Image, 8, 6, Image.Width, Image.Height); } } #endregion #region Background Image Drawing if (BackgroundImage != null) { e.Graphics.SmoothingMode = SmoothingMode.HighQuality; e.Graphics.InterpolationMode = InterpolationMode.Low; e.Graphics.CompositingQuality = CompositingQuality.HighQuality; e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; using (var sf = new StringFormat { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center }) using (Brush brush = new SolidBrush(ForeColor)) { e.Graphics.DrawImage(BackgroundImage, (roundedRect.Rect.Size.Width - BackgroundImage.Size.Width) / 2 + 1, (roundedRect.Rect.Size.Height - BackgroundImage.Size.Height) / 2 + 1 ); } } #endregion base.OnPaint(e); }