using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.ComponentModel;
namespace Uchukamen.Windows.Forms
{
/// <summary>
/// StarButton の概要の説明です。
/// </summary>
public class StarButton : System.Windows.Forms.Button
{
private bool isMouseOver = false;
private bool isMouseDown = false;
private Color borderColor = SystemColors.HotTrack;
private Color mouseOvertColor = SystemColors.ActiveCaption;
private Color mouseDownColor = Color.GreenYellow;
public StarButton() : base()
{
}
#region Properties
[Category( "Star Button" )]
[Description( "境界の色" )]
[DefaultValue( typeof(Color), "HotTrack" )]
public Color BorderColor
{
get
{
return this.borderColor;
}
set
{
this.borderColor = value;
// cause a repaint if necessary
if (this.IsHandleCreated && this.Visible)
{
Invalidate();
}
}
}
[Category( "Star Button" )]
[Description( "Mouse が上に来たときの色" )]
[DefaultValue( typeof(Color), "ActiveCaption" )]
public Color MouseOvertColor
{
get
{
return this.mouseOvertColor;
}
set
{
this.mouseOvertColor = value;
// cause a repaint if necessary
if (this.IsHandleCreated && this.Visible)
{
Invalidate();
}
}
}
[Category( "Star Button" )]
[Description( "マウスが押されたときの色" )]
[DefaultValue( typeof(Color), "GreenYellow" )]
public Color MouseDownColor
{
get
{
return this.mouseDownColor;
}
set
{
this.mouseDownColor = value;
// cause a repaint if necessary
if (this.IsHandleCreated && this.Visible)
{
Invalidate();
}
}
}
#endregion
protected void DrawButton(Graphics gr)
{
// ボタンの表示色を決定する。
Color currentColor;
currentColor = isMouseOver ? this.mouseOvertColor : this.BackColor;
currentColor = isMouseDown ? this.mouseDownColor : currentColor;
// Star の Path を計算する。
GraphicsPath starPath = this.GetPath();
// Star を描画する
Brush bgBrush = new SolidBrush( currentColor );
gr.FillPath(bgBrush, starPath);
// Star の境界線を描く
Pen pen = new Pen(BorderColor, 2.0F);
gr.DrawPath(pen, starPath);
// ボタンのリージョンを Star にする。
this.Region = new System.Drawing.Region(starPath);
}
/// <summary>
/// 星型の Path を作る。
/// </summary>
/// <returns></returns>
protected GraphicsPath GetPath()
{
GraphicsPath gPath = new GraphicsPath();
int r = System.Math.Min(this.Width, this.Height)/2;
Point[] points =
{
new Point((int)(r*Math.Sin(Math.PI*2*0.0)), (int)(r*-Math.Cos(Math.PI*2*0))),
new Point((int)(r*Math.Sin(Math.PI*2*1/5)), (int)(r*-Math.Cos(Math.PI*2*1/5))),
new Point((int)(r*Math.Sin(Math.PI*2*2/5)), (int)(r*-Math.Cos(Math.PI*2*2/5))),
new Point((int)(r*Math.Sin(Math.PI*2*3/5)), (int)(r*-Math.Cos(Math.PI*2*3/5))),
new Point((int)(r*Math.Sin(Math.PI*2*4/5)), (int)(r*-Math.Cos(Math.PI*2*4/5))),
};
gPath.FillMode = System.Drawing.Drawing2D.FillMode.Winding;
gPath.AddLine(points[0], points[2]);
gPath.AddLine(points[2], points[4]);
gPath.AddLine(points[4], points[1]);
gPath.AddLine(points[1], points[3]);
gPath.AddLine(points[3], points[0]);
// 中心点を星の中心へ移す。
gPath.Transform(new System.Drawing.Drawing2D.Matrix(1,0,0,1,r,r));
return gPath;
}
protected override void OnPaint(PaintEventArgs pe)
{
// base.OnPaint(pe);
this.DrawButton(pe.Graphics);
}
protected override void OnMouseEnter(System.EventArgs e)
{
base.OnMouseEnter(e);
this.isMouseOver = true;
Invalidate();
}
protected override void OnMouseLeave(System.EventArgs e)
{
base.OnMouseLeave(e);
this.isMouseOver = false;
Invalidate();
}
protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
{
base.OnMouseUp(e);
this.isMouseDown = false;
Invalidate();
}
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs mevent)
{
base.OnMouseDown(mevent);
this.isMouseDown = true;
Invalidate();
}
}
}
|