na zacatek tridy
Pen p;
Brush b;
na konec konstruktoru
p = new Pen (panel1.BackColor);
b = new SolidBrush (panel1.BackColor);
na konec newColorMenuItem_Click
p = new Pen(t.BackColor);
upravit pictureBox_MouseMove
/*
Brush b = new LinearGradientBrush(new Point(0, 0),
new Point(100, 100),
Color.Red,
Color.Blue);
*/
g.FillEllipse (b, X0, Y0, X1 - X0, Y1 - Y0);
g.DrawEllipse (p, X0, Y0, X1 - X0, Y1 - Y0);
upravit panel1_MouseDown
private void panel1_MouseDown (object sender, MouseEventArgs e)
{
// Panel t = (Panel) sender;
// Panel t = sender as Panel;
Control t = (Control) sender;
if (e.Button == MouseButtons.Middle)
{
colorDialog1.Color = t.BackColor;
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
t.BackColor = colorDialog1.Color;
p = new Pen (t.BackColor);
}
}
else if (e.Button == MouseButtons.Left)
{
p = new Pen (t.BackColor);
}
else if (e.Button == MouseButtons.Right)
{
b = new SolidBrush (t.BackColor);
}
}
Cely soubor
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace malovani
{
public partial class Form1 : Form
{
private Pen p;
private Brush b;
private int cnt = 1;
public Form1()
{
InitializeComponent();
pictureBox1_SizeChanged (null, null);
p = new Pen(panel1.BackColor);
b = new SolidBrush (panel1.BackColor);
}
private void panel1_Click(object sender, EventArgs e)
{
// p = new Pen (panel1.BackColor);
p = new Pen((sender as Panel).BackColor);
}
private void panel1_MouseDown (object sender, MouseEventArgs e)
{
// Panel t = (Panel) sender;
// Panel t = sender as Panel;
Control t = (Control) sender;
if (e.Button == MouseButtons.Middle)
{
colorDialog1.Color = t.BackColor;
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
t.BackColor = colorDialog1.Color;
p = new Pen (t.BackColor);
}
}
else if (e.Button == MouseButtons.Left)
{
p = new Pen (t.BackColor);
}
else if (e.Button == MouseButtons.Right)
{
b = new SolidBrush (t.BackColor);
}
}
private void newColorToolStripMenuItem_Click(object sender, EventArgs e)
{
Panel t = new Panel();
t.Parent = toolPanel;
t.BackColor = Color.CornflowerBlue;
t.Width = panel1.Width;
t.Height = panel1.Height;
t.Top = panel1.Top;
cnt++;
t.Left = cnt * panel1.Left + (cnt - 1) * panel1.Width;
t.MouseDown += panel1_MouseDown;
p = new Pen (t.BackColor);
}
private void pictureBox1_SizeChanged(object sender, EventArgs e)
{
Image old = pictureBox1.Image;
int w = pictureBox1.Width;
int h = pictureBox1.Height;
if (old != null)
{
w = Math.Max (w, old.Width);
h = Math.Max (h, old.Height);
}
pictureBox1.Image = new Bitmap (w, h);
Graphics g = Graphics.FromImage(pictureBox1.Image);
Brush b = new SolidBrush(Color.White);
g.FillRectangle (b, 0, 0, w, h);
if (old != null)
g.DrawImage (old, 0, 0);
}
private void testToolStripMenuItem_Click(object sender, EventArgs e)
{
Graphics g = Graphics.FromImage (pictureBox1.Image);
Pen p = new Pen (Color.Red);
g.DrawLine (p, 10, 10, 100, 100);
pictureBox1.Invalidate();
}
private int X0, Y0;
private bool s = false;
private Bitmap save;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
X0 = e.X;
Y0 = e.Y;
s = true;
save = new Bitmap (pictureBox1.Image);
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (s)
{
Graphics g = Graphics.FromImage(pictureBox1.Image);
g.DrawImage (save, 0, 0);
// Pen p = new Pen(Color.Red);
// g.DrawLine (p, X0, Y0, e.X, e.Y);
int X1 = X0;
int Y1 = Y0;
int X2 = e.X;
int Y2 = e.Y;
if (X2 < X1) { int t = X1; X1 = X2; X2 = t; }
if (Y2 < Y1) { int t = Y1; Y1 = Y2; Y2 = t; }
/*
Brush b = new SolidBrush (Color.Yellow);
// using System.Drawing.Drawing2D;
b = new LinearGradientBrush (new Point (0, 0),
new Point (100, 100),
Color.Red,
Color.Blue);
g.FillRectangle(b, X1, Y1, X2 - X1, Y2 - Y1);
*/
g.FillEllipse (b, X1, Y1, X2 - X1, Y2 - Y1);
// g.DrawRectangle(p, X1, Y1, X2 - X1, Y2 - Y1);
g.DrawEllipse(p, X1, Y1, X2 - X1, Y2 - Y1);
pictureBox1.Invalidate();
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
s = false;
}
}
}