using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.ComponentModel;
namespace Uchukamen.Forms
{
/// <summary>
/// LEDButton の概要の説明です。
/// </summary>
public class LEDButton : System.Windows.Forms.Button
{
private bool isLedOn = false;
private bool ledBlink = false;
private Color ledOffColor = Color.Black;
private Color ledBorderColor = Color.Green;
private Color ledColor = Color.GreenYellow;
private Timer timer;
private float ratio = 0.0F;
private float delta = 0.1F;
public LEDButton() : base()
{
timer = new Timer();
timer.Tick += new System.EventHandler(OnTimer);
}
#region Properties
[Category( "LED" )]
[Description( "LEDの On/Off" )]
[DefaultValue( false )]
public bool IsLedOn
{
get
{
return this.isLedOn;
}
set
{
this.isLedOn = value;
// cause a repaint if necessary
if (this.IsHandleCreated && this.Visible)
{
Invalidate();
}
}
}
[Category( "LED" )]
[Description( "LEDを点滅するかどうか" )]
[DefaultValue( false )]
public bool LedBlink
{
get
{
return this.ledBlink;
}
set
{
this.ledBlink = value;
this.timer.Enabled = this.ledBlink;
// cause a repaint if necessary
if (this.IsHandleCreated && this.Visible)
{
Invalidate();
}
}
}
[Category( "LED" )]
[Description( "LEDの色" )]
[DefaultValue( typeof(Color), "GreenYellow" )]
public Color LedColor
{
get
{
return this.ledColor;
}
set
{
this.ledColor = value;
// cause a repaint if necessary
if (this.IsHandleCreated && this.Visible)
{
Invalidate();
}
}
}
[Category( "LED" )]
[Description( "LED Offの色" )]
[DefaultValue( typeof(Color), "Black" )]
public Color LedOffColor
{
get
{
return this.ledOffColor;
}
set
{
this.ledOffColor = value;
// cause a repaint if necessary
if (this.IsHandleCreated && this.Visible)
{
Invalidate();
}
}
}
[Category( "LED" )]
[Description( "LED の境界色" )]
[DefaultValue( typeof(Color), "Green" )]
public Color LedBorderColor
{
get
{
return this.ledBorderColor;
}
set
{
this.ledBorderColor = value;
// cause a repaint if necessary
if (this.IsHandleCreated && this.Visible)
{
Invalidate();
}
}
}
[Category( "LED" )]
[Description( "時間間隔(mSec)" )]
[DefaultValue( 100 )]
public int Interval
{
get
{
return timer.Interval;
}
set
{
timer.Interval = value;
}
}
#endregion
private void DrawLED(Graphics gr)
{
// LED の表示色を決定する。
Color currentColor;
if (this.LedBlink)
{
// LED が点滅する場合
ratio += delta;
if ( ratio > 1.0F )
{
delta = -delta;
ratio = 1.0F;
}
else if ( ratio < 0.0F )
{
delta = -delta;
ratio = 0.0F;
}
byte r = (byte)(LedColor.R * ratio + LedOffColor.R *( 1.0 - ratio ) );
byte g = (byte)(LedColor.G * ratio + LedOffColor.G *( 1.0 - ratio ) );
byte b = (byte)(LedColor.B * ratio + LedOffColor.B *( 1.0 - ratio ) );
currentColor = Color.FromArgb(r,g,b);
}
else
{
// 点滅しない場合
currentColor = isLedOn ? this.ledColor : this.LedOffColor;
}
// LED を描画する
Brush bgBrush = new SolidBrush( currentColor );
gr.FillRectangle(bgBrush,
ClientRectangle.X + (int)(ClientRectangle.Width*0.05),
ClientRectangle.Y + (int)(ClientRectangle.Height*0.1),
(int)(ClientRectangle.Width*0.1), (int)(ClientRectangle.Height*0.8));
// LED の境界線を描く
Pen pen = new Pen(LedBorderColor, 1.0F);
gr.DrawRectangle(pen,
ClientRectangle.X + (int)(ClientRectangle.Width*0.05),
ClientRectangle.Y + (int)(ClientRectangle.Height*0.1),
(int)(ClientRectangle.Width*0.1), (int)(ClientRectangle.Height*0.8));
}
private void OnTimer(object sender, System.EventArgs e)
{
Invalidate();
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
this.DrawLED(pe.Graphics);
}
}
}
|