Hi,
the below code drawing a custom Oval button, The problem is: I want after the click event to change its color many times but it stops at the red color and repaint again, so it always achieve just the first condition:
what should i do !!
the below code drawing a custom Oval button, The problem is: I want after the click event to change its color many times but it stops at the red color and repaint again, so it always achieve just the first condition:
Code:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace ShappedButton
{
public partial class ctrlOvalBtn : Button
{
public ctrlOvalBtn()
{
InitializeComponent();
}
private bool _clicked = false;
public bool Clicked
{
get { return _clicked; }
set
{
_clicked = value;
Invalidate();
}
}
protected override void OnPaint(PaintEventArgs pevent)
{
Graphics g = pevent.Graphics;
Rectangle innerRect = new Rectangle(new Point(0, 0), new Size(this.Width, this.Height));
GraphicsPath innerPath = new GraphicsPath();
SolidBrush brshFill = new SolidBrush(Color.WhiteSmoke);
SolidBrush brshLine = new SolidBrush(Color.Black);
innerPath.AddEllipse(innerRect);
Region innerRegion = new Region(innerPath);
this.Region = innerRegion;
pevent.Graphics.FillEllipse(brshLine, 0, 0, this.Width, this.Height);
pevent.Graphics.FillEllipse(brshFill, 1, 1, this.Width - 2, this.Height - 2);
if (_clicked == false)
{
pevent.Graphics.FillEllipse(brshLine, 0, 0, this.Width, this.Height);
pevent.Graphics.FillEllipse(brshFill, 1, 1, this.Width - 2, this.Height - 2);
}
else
{
if (brshFill.Color.Equals(Color.WhiteSmoke))
{
brshFill.Color = Color.Red;
pevent.Graphics.FillEllipse(brshFill, 1, 1, this.Width - 2, this.Height - 2);
}
else if (brshFill.Color.Equals(Color.Red))
{
brshFill.Color = Color.Blue;
pevent.Graphics.FillEllipse(brshFill, 1, 1, this.Width - 2, this.Height - 2);
}
else if (brshFill.Color.Equals(Color.Blue))
{
brshFill.Color = Color.Brown;
pevent.Graphics.FillEllipse(brshFill, 1, 1, this.Width - 2, this.Height - 2);
}
else
{
brshFill.Color = Color.WhiteSmoke;
pevent.Graphics.FillEllipse(brshFill, 1, 1, this.Width - 2, this.Height - 2);
}
}
// Dispose of painting objects
brshFill.Dispose();
brshLine.Dispose();
}
protected override void OnMouseEnter(EventArgs e)
{
this.Cursor = Cursors.Hand;
base.OnMouseEnter(e);
}
protected override void OnMouseLeave(EventArgs e)
{
this.Cursor = Cursors.Arrow;
base.OnMouseLeave(e);
}
protected override void OnMouseDown(MouseEventArgs mevent)
{
_clicked = true;
base.OnMouseDown(mevent);
}
protected override void OnMouseUp(MouseEventArgs mevent)
{
_clicked = true;
base.OnMouseUp(mevent);
}
}
}
Comment