カラーピッカーとかで使うようなグラデーションのかかったスライダーを作ってみました。
相変わらず糞コードです。
- using System;
- using System;
- using System.ComponentModel;
- using System.Drawing;
- using System.Windows.Forms;
- using System.Drawing.Drawing2D;
-
- public partial class CSlider : Control
- {
-
-
- [Description("コントロールの値が変更するとき発生します。")]
- [Category("アクション")]
- public event EventHandler ValueChanged;
-
-
- private int _maximum = 100;
- public int Maximum
- {
- set
- {
- this._maximum = value;
- }
- get
- {
- return this._maximum;
- }
- }
-
- public int Minimum { get; set; }
- private int _value;
- public int Value
- {
- set
- {
- this._value = value;
- drawGradation();
- this.Refresh();
- if (ValueChanged != null && Value <= Minimum && Value >= Maximum) ValueChanged(this, new EventArgs());
- }
- get
- {
- return this._value;
- }
- }
- private Color _color1 = Color.Black;
- public Color Color1
- {
- set
- {
- this._color1 = value;
- drawGradation();
- this.Refresh();
- }
- get
- {
-
- return this._color1;
- }
- }
-
- private Color _color2 = Color.White;
- public Color Color2
- {
- set
- {
- this._color2 = value;
- drawGradation();
- this.Refresh();
- }
- get
- {
-
- return this._color2;
- }
- }
-
- public CSlider()
- {
- InitializeComponent();
-
- this.SetStyle(ControlStyles.ResizeRedraw, true);
- this.SetStyle(ControlStyles.DoubleBuffer, true);
- this.SetStyle(ControlStyles.UserPaint, true);
- this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
-
- this.Resize += new System.EventHandler(this.CSlider_Resize);
- this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.CSlider_MouseMove);
- this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.CSlider_MouseDown);
- this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.CSlider_MouseUp);
- this.MouseLeave += new System.EventHandler(this.CSlider_MouseLeave);
- this.MouseEnter += new System.EventHandler(this.CSlider_MouseEnter);
-
- drawTick();
- }
-
-
-
- private Bitmap tick;
- private void drawTick()
- {
- tick = new Bitmap(10, 10);
-
- Graphics g = Graphics.FromImage(tick);
- g.SmoothingMode = SmoothingMode.HighQuality;
- Point[] point = { new Point(5,1), new Point(9,5),
- new Point(9,7), new Point(8,8),
- new Point(2,8), new Point(1,7),
- new Point(1,5), new Point(5,1) };
- Brush b = new SolidBrush(Color.White);
- g.FillPolygon(b, point);
- Pen p = new Pen(Color.Black, 1);
- if (activeFlag == true)
- {
- p = new Pen(Color.Brown, 1);
- }
- g.DrawLines(p, point);
- g.Dispose();
- }
-
- protected override void OnPaint(PaintEventArgs pe)
- {
- base.OnPaint(pe);
-
- pe.Graphics.DrawImage(gradation, 5, 0, gradation.Width, gradation.Height);
- pe.Graphics.DrawImage(tick, (Value * gradation.Width) / (Maximum - Minimum), gradation.Height);
-
- }
-
-
- private Bitmap gradation;
- private void drawGradation()
- {
- if (this.Width > 10 && this.Height > 10)
- {
- Size gradationSize = new Size(this.Width - 10, this.Height - 10);
-
- gradation = new Bitmap(gradationSize.Width, gradationSize.Height);
- Graphics g = Graphics.FromImage(gradation);
- LinearGradientBrush gb =
- new LinearGradientBrush(new Point(0, 0), new Point(gradationSize.Width, gradationSize.Height),
- Color1, Color2);
- g.FillRectangle(gb, new Rectangle(0, 0, gradationSize.Width, gradationSize.Height));
-
-
- Pen p = new Pen(Color.Black, 1);
- if (activeFlag == true)
- {
- p = new Pen(Color.Brown, 1);
- }
-
- g.DrawRectangle(p, 0, 0, gradationSize.Width - 1, gradationSize.Height - 1);
- g.Dispose();
- }
-
- }
-
-
-
- private void CSlider_Resize(object sender, EventArgs e)
- {
- drawGradation();
- this.Refresh();
- }
-
-
-
- private bool mouseDown = false;
- private void CSlider_MouseDown(object sender, MouseEventArgs e)
- {
- Value = (int)(((float)(e.X - 5) / (this.Width - 10)) * (Maximum - Minimum));
- if (Value <= Minimum) Value = Minimum;
- if (Value >= Maximum) Value = Maximum;
-
- this.Refresh();
-
- if (ValueChanged != null) ValueChanged(this, new EventArgs());
-
- mouseDown = true;
- }
-
- private void CSlider_MouseMove(object sender, MouseEventArgs e)
- {
- if (mouseDown == true)
- {
- Value = (int)(((float)(e.X - 5) / (this.Width - 10)) * (Maximum - Minimum));
- if (Value <= Minimum) Value = Minimum;
- if (Value >= Maximum) Value = Maximum;
-
- this.Refresh();
-
- if (ValueChanged != null) ValueChanged(this, new EventArgs());
- }
- }
-
- private void CSlider_MouseUp(object sender, MouseEventArgs e)
- {
- mouseDown = false;
- }
-
-
- private bool activeFlag = false;
- private void CSlider_MouseEnter(object sender, EventArgs e)
- {
- activeFlag = true;
-
- drawGradation();
- drawTick();
- this.Refresh();
- }
-
- private void CSlider_MouseLeave(object sender, EventArgs e)
- {
- activeFlag = false;
-
- drawGradation();
- drawTick();
- this.Refresh();
- }
-
- }
using System;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
public partial class CSlider : Control
{
// イベントの宣言
[Description("コントロールの値が変更するとき発生します。")]
[Category("アクション")]
public event EventHandler ValueChanged;
// プロパティ
private int _maximum = 100;
public int Maximum
{
set
{
this._maximum = value;
}
get
{
return this._maximum;
}
}
public int Minimum { get; set; }
private int _value;
public int Value
{
set
{
this._value = value;
drawGradation();
this.Refresh();
if (ValueChanged != null && Value <= Minimum && Value >= Maximum) ValueChanged(this, new EventArgs());
}
get
{
return this._value;
}
}
private Color _color1 = Color.Black;
public Color Color1
{
set
{
this._color1 = value;
drawGradation();
this.Refresh();
}
get
{
return this._color1;
}
}
private Color _color2 = Color.White;
public Color Color2
{
set
{
this._color2 = value;
drawGradation();
this.Refresh();
}
get
{
return this._color2;
}
}
public CSlider()
{
InitializeComponent();
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.Resize += new System.EventHandler(this.CSlider_Resize);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.CSlider_MouseMove);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.CSlider_MouseDown);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.CSlider_MouseUp);
this.MouseLeave += new System.EventHandler(this.CSlider_MouseLeave);
this.MouseEnter += new System.EventHandler(this.CSlider_MouseEnter);
drawTick();
}
// 描画
//針の描画
private Bitmap tick;
private void drawTick()
{
tick = new Bitmap(10, 10);
Graphics g = Graphics.FromImage(tick);
g.SmoothingMode = SmoothingMode.HighQuality;
Point[] point = { new Point(5,1), new Point(9,5),
new Point(9,7), new Point(8,8),
new Point(2,8), new Point(1,7),
new Point(1,5), new Point(5,1) };
Brush b = new SolidBrush(Color.White);
g.FillPolygon(b, point);
Pen p = new Pen(Color.Black, 1);
if (activeFlag == true)
{
p = new Pen(Color.Brown, 1);
}
g.DrawLines(p, point);
g.Dispose();
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
pe.Graphics.DrawImage(gradation, 5, 0, gradation.Width, gradation.Height);
pe.Graphics.DrawImage(tick, (Value * gradation.Width) / (Maximum - Minimum), gradation.Height);
}
//グラデーションの描画
private Bitmap gradation;
private void drawGradation()
{
if (this.Width > 10 && this.Height > 10)
{
Size gradationSize = new Size(this.Width - 10, this.Height - 10);
gradation = new Bitmap(gradationSize.Width, gradationSize.Height);
Graphics g = Graphics.FromImage(gradation);
LinearGradientBrush gb =
new LinearGradientBrush(new Point(0, 0), new Point(gradationSize.Width, gradationSize.Height),
Color1, Color2);
g.FillRectangle(gb, new Rectangle(0, 0, gradationSize.Width, gradationSize.Height));
//枠線
Pen p = new Pen(Color.Black, 1);
if (activeFlag == true)
{
p = new Pen(Color.Brown, 1);
}
g.DrawRectangle(p, 0, 0, gradationSize.Width - 1, gradationSize.Height - 1);
g.Dispose();
}
}
//リサイズ時、グラデーションの再描画
private void CSlider_Resize(object sender, EventArgs e)
{
drawGradation();
this.Refresh();
}
//プロパティ
private bool mouseDown = false;
private void CSlider_MouseDown(object sender, MouseEventArgs e)
{
Value = (int)(((float)(e.X - 5) / (this.Width - 10)) * (Maximum - Minimum));
if (Value <= Minimum) Value = Minimum;
if (Value >= Maximum) Value = Maximum;
this.Refresh();
if (ValueChanged != null) ValueChanged(this, new EventArgs());
mouseDown = true;
}
private void CSlider_MouseMove(object sender, MouseEventArgs e)
{
if (mouseDown == true)
{
Value = (int)(((float)(e.X - 5) / (this.Width - 10)) * (Maximum - Minimum));
if (Value <= Minimum) Value = Minimum;
if (Value >= Maximum) Value = Maximum;
this.Refresh();
if (ValueChanged != null) ValueChanged(this, new EventArgs());
}
}
private void CSlider_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
private bool activeFlag = false;
private void CSlider_MouseEnter(object sender, EventArgs e)
{
activeFlag = true;
drawGradation();
drawTick();
this.Refresh();
}
private void CSlider_MouseLeave(object sender, EventArgs e)
{
activeFlag = false;
drawGradation();
drawTick();
this.Refresh();
}
}
//日記 すき焼きともつ鍋食べた。うまかった。