math-suite/Source/Launcher/Controls/MyButton.cs

107 lines
3.8 KiB
C#
Raw Permalink Normal View History

2018-02-05 23:24:46 +00:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DynamicLink.Controls
{
public class MyButton : Button
{
private readonly Color ColorNormal1 = Color.FromArgb(0xff, 0xee, 0xee, 0xee);
private readonly Color ColorNormal2 = Color.FromArgb(0xff, 0xbb, 0xbb, 0xbb);
private readonly Color ColorFocus1 = Color.FromArgb(0xff, 0xff, 0xff, 0xff);
private readonly Color ColorFocus2 = Color.FromArgb(0xff, 0xc8, 0xc8, 0xc8);
private readonly Color ColorPressed1 = Color.FromArgb(0xff, 0xbb, 0xbb, 0xbb);
private readonly Color ColorPressed2 = Color.FromArgb(0xff, 0xdd, 0xdd, 0xdd);
public bool IsMouseOver { get; private set; }
public bool IsPressed { get; private set; }
public MyButton() : base()
{
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.SetStyle(ControlStyles.Opaque, false);
//this.Paint += new PaintEventHandler(MyButton_Paint);
this.MouseEnter += new EventHandler(MyButton_MouseEnter);
this.MouseLeave += new EventHandler(MyButton_MouseLeave);
this.MouseDown += new MouseEventHandler(MyButton_MouseDown);
this.MouseUp += new MouseEventHandler(MyButton_MouseUp);
}
#region Mouse input
void MyButton_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
IsPressed = false;
this.Invalidate();
}
}
void MyButton_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
IsPressed = true;
this.Invalidate();
}
}
void MyButton_MouseLeave(object sender, EventArgs e)
{
IsMouseOver = false;
this.Invalidate();
}
void MyButton_MouseEnter(object sender, EventArgs e)
{
IsMouseOver = true;
this.Invalidate();
}
#endregion
protected override void OnPaint(PaintEventArgs e)
{
// base.OnPaint(pevent);
//InvalidateEx();
// Create gradient
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, this.Width, this.Height), ColorNormal1, ColorNormal2, 90);
if (this.IsMouseOver) brush = new LinearGradientBrush(new Rectangle(0, 0, this.Width, this.Height), ColorFocus1, ColorFocus2, 90);
if (this.IsPressed) brush = new LinearGradientBrush(new Rectangle(0, 0, this.Width, this.Height), ColorPressed1, ColorPressed2, 90);
// Set up graphics
// e.Graphics.Clear(this.BackColor);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
// Draw rectangle
RoundedRectangle.FillRoundedRectangle(e.Graphics, new Rectangle(0, 0, this.Width - 1, this.Height - 1), brush, 4);
e.Graphics.DrawPath(Pens.Gray, RoundedRectangle.GetPath(new Rectangle(0, 0, this.Width - 1, this.Height - 1), 4));
// Draw text
SizeF size = e.Graphics.MeasureString(this.Text, this.Font);
e.Graphics.DrawString(this.Text, this.Font, Brushes.Black, (this.Width - size.Width) / 2, (this.Height - size.Height) / 2);
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// MyButton
//
this.BackColor = System.Drawing.Color.Transparent;
this.UseVisualStyleBackColor = false;
this.ResumeLayout(false);
}
}
}