Код: Выделить всё
private SolidBrush b;
private Graphics g; //Encapsulates a GDI+ drawing surface
Pen p = new Pen(Brushes.DeepSkyBlue); //Pens are used to draw objects
private Color c = Color.Black;
private Font f;
// TO DRAW THE RECTANGLE
private void button1_Click(object sender, EventArgs e)
{
g = CreateGraphics();
g.DrawRectangle(p, 200, 200, 50, 50);
SolidBrush b = new SolidBrush(c);
}
//TO CHOOSE WHICH COLOR
private void button2_Click(object sender, EventArgs e)
{
colorDialog1.Color = c;
colorDialog1.ShowDialog(); //Display with the previous colour already chosen
c = colorDialog1.Color; //Display the actual COLOUR dialog box
}
private void form_Paint(object sender, PaintEventArgs e)
{
g = e.Graphics; //Get the Graphics object from the PaintEventArgs
p = new Pen(c); //Create a new Pen using the current colour
f = new Font("Arial", 20); //Create a new Font
b = new SolidBrush(c); //Create a new brush using the current colour
}
Подробнее здесь: https://stackoverflow.com/questions/798 ... th-a-color