Suzie

Suzie

  • NA
  • 4
  • 0

How to Drawing on a panel in C#

Apr 1 2007 11:16 PM
Hi,

I'm new to C# and trying to develop this program that allows the user to draw freeform with the mouse on a panel. There are 2 groupboxes. One is called Color and it has 4 radio buttons: red, blue, green and black. The second is Size and has 3 radio buttons: small, medium, and large. The user should be able to select a color and a brush size and draw on the panel. Here is what I have so far. Any help will be greatly appreciated.

[CODE]
public partial class drawingPanelForm : Form
{
bool shouldPaint = false; // determines whether to paint
private Color m_Color;
private Size m_Size;
public void FillEllipse(Brush brush,int x,int y,int width,int height);
Brush brush;
int x;
int y;
int width;
int height;
//default constructor
public drawingPanelForm()
{
InitializeComponent();
}
//should paint when mouse button is pressed down
private void drawingPanelForm_MouseDown(object sender, MouseEventArgs e)
{
//indicate that user is dragging the mouse
shouldPaint = true;
}
//stop painting when mouse button is released
private void drawingPanelForm_MouseUp(object sender, MouseEventArgs e)
{
//indicate that user released the mouse button
shouldPaint = false;
}
private void redRadioButton_CheckedChanged(object sender, EventArgs e)
{
m_Color = Color.Red;
}
private void blueRadioButton_CheckedChanged(object sender, EventArgs e)
{
m_Color = Color.Blue;
}
private void greenRadioButton_CheckedChanged(object sender, EventArgs e)
{
m_Color = Color.Green;
}
private void blackRadioButton_CheckedChanged(object sender, EventArgs e)
{
m_Color = Color.Black;
}
private void smallRadioButton_CheckedChanged(object sender, EventArgs e)
{
m_Size = new Size(4,4);
}
private void mediumRadioButton_CheckedChanged(object sender, EventArgs e)
{
m_Size = new Size(6, 6);
}
private void largeRadioButton_CheckedChanged(object sender, EventArgs e)
{
m_Size = new Size(12, 12);
}
private void drawPanel_MouseMove(object sender, MouseEventArgs e)
{
if (shouldPaint)//check if mouse button is being pressed
{
//draw where the mouse pointer is present
Graphics graphics = CreateGraphics();
graphics.FillEllipse(new SolidBrush(m_Color), e.X, e.Y, m_Width, m_Heigth);
graphics.Dispose();
}
}
}
}

[/CODE]

Answers (2)