using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace Audentia.Common.Controls
{
[DefaultEvent("Click")]
public sealed class Button : PictureBox
{
public bool Hovered { get; set; }
private Timer _t;
public Button()
{
SizeMode = PictureBoxSizeMode.AutoSize;
Click += mouseclicked;
_t = new Timer {Interval = 1};
_t.Tick += _t_Tick;
}
//ToDo: Vorheriges Control speichern und abfragen
private void _t_Tick(object sender, EventArgs e)
{
foreach (Control c in Parent.Controls)
{
if(c is Button)
{
if(c.Name!=Name)
(c as Button).clicked = false;
}
}
_t.Stop();
}
private void mouseclicked(object sender, EventArgs e)
{
_t.Start();
clicked = true;
}
protected override void OnMouseHover(System.EventArgs e)
{
Cursor = Cursors.Hand;
Hovered = true;
Invalidate();
base.OnMouseHover(e);
}
protected override void OnMouseLeave(System.EventArgs e)
{
if(!clicked)
Hovered = false;
Invalidate();
base.OnMouseLeave(e);
}
private bool clicked;
protected override void OnPaint(PaintEventArgs pe)
{
if (Image != null)
{
pe.Graphics.DrawImage(Hovered ? Lighten(new Bitmap(Image)) : Image, 0, 0, Image.Width, Image.Height);
}
else
{
pe.Graphics.DrawImage(ErrorImage, 0, 0, Image.Width, Image.Height);
}
}
private Bitmap Lighten(Bitmap bitmap)
{
for (int i = 0; i < bitmap.Width; i++)
{
for (int j = 0; j < bitmap.Height; j++)
{
var p = bitmap.GetPixel(i, j);
var pp = Color.FromArgb(150, p);
if (p.A > 0)
bitmap.SetPixel(i, j, pp);
}
}
return bitmap;
}
}
}