110,579
社区成员
发帖
与我相关
我的任务
分享
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
drawPalette(g);
drawGrid(g);
drawBorder(g);
drawCursor(g);
}
void drawGrid(Graphics g)
{
for (int i = 0; i < rows; i++)
{
g.DrawLine(
Pens.Black,
0,
blockWidth * (i + 1),
blockWidth * cols,
blockWidth * (i + 1)
);
}
for (int i = 0; i < cols; i++)
{
g.DrawLine(
Pens.Black,
blockWidth * (i + 1),
0,
blockWidth * (i + 1),
blockWidth * rows
);
}
}
Color getColor(int row, int col)
{
byte r = 0, g = 0, b = 0;
int step = 0xff / (rows - 1);
r = (byte)(row * step);
g = (byte)(step * (col / rows));
b = (byte)(step * (col % rows));
return Color.FromArgb(r, g, b);
}
current = getColor(pt.Y / blockWidth, pt.X / blockWidth);
void updateCursor(Point pt)
{
lastCursor.X = cursor.X;
lastCursor.Y = cursor.Y;
cursor.X = pt.X - pt.X % blockWidth;
cursor.Y = pt.Y - pt.Y % blockWidth;
current = getColor(pt.Y / blockWidth, pt.X / blockWidth);
}
protected override void OnMouseMove(MouseEventArgs e)
{
updateCursor(e.Location);
// redraw larger spaces
Invalidate(
new Rectangle(
lastCursor.X - 1,
lastCursor.Y - 1,
lastCursor.Width + 2,
lastCursor.Height + 2
)
);
Invalidate(
new Rectangle(
cursor.X - 1,
cursor.Y - 1,
cursor.Width + 2,
cursor.Height + 2
)
);
// fire event
OnColorChanged();
}
// -- custom events
public delegate void ColorChangedEventHandler(object sender, ColorChangedEventArgs e);
[Description("Fires every time when color changed.")]
public event ColorChangedEventHandler ColorChanged;
protected virtual void OnColorChanged()
{
if (ColorChanged != null)
ColorChanged(this, new ColorChangedEventArgs(current));
}
// custom event args
public class ColorChangedEventArgs : EventArgs
{
Color color = Color.Black;
public Color Color
{
get { return color; }
set { color = value; }
}
public ColorChangedEventArgs(Color color)
: base()
{
this.color = color;
}
}