drawing on a panel

  • Thread starter Thread starter dk60
  • Start date Start date
D

dk60

when I draw an ellipse on a panel on a form the ellipse actually gets
drawn behind the panel, so that the panel covers it. Is there a way to
draw graphics inside a panel, or some other control, so that the panel
serves as a canvas, and the mouse coordinates are relative to the
panel? even if I use the panel MouseMove event the coordinated of e
are still relative to the form and not relative to the panel.
 
dk60 said:
when I draw an ellipse on a panel on a form the ellipse actually gets
drawn behind the panel, so that the panel covers it. Is there a way to
draw graphics inside a panel, or some other control, so that the panel
serves as a canvas, and the mouse coordinates are relative to the
panel? even if I use the panel MouseMove event the coordinated of e
are still relative to the form and not relative to the panel.

You can use this to get the local coords

Point localMousePosition = panel1.PointToClient(Cursor.Position);

mick
 
dk60 said:
when I draw an ellipse on a panel on a form the ellipse actually gets
drawn behind the panel, so that the panel covers it. Is there a way to
draw graphics inside a panel, or some other control, so that the panel
serves as a canvas, and the mouse coordinates are relative to the
panel? even if I use the panel MouseMove event the coordinated of e
are still relative to the form and not relative to the panel.

You can use this to get the local coords

Point localMousePosition = panel1.PointToClient(Cursor.Position);

Also, when you create your Graphics object are you using something
like this

Graphics g = panel1.CreateGraphics();

mick
 
when I draw an ellipse on a panel on a form the ellipse actually gets
drawn behind the panel, so that the panel covers it. Is there a way to
draw graphics inside a panel, or some other control, so that the panel
serves as a canvas, and the mouse coordinates are relative to the
panel? even if I use the panel MouseMove event the coordinated of e
are still relative to the form and not relative to the panel.

It sounds like you are handling the forms paint event handler rather
than the panels paint handler. You want to handle the panel's paint
event as follows:

public Form1()
{
InitializeComponent();
panel1.Paint += new PaintEventHandler(panel1_Paint);
}

void panel1_Paint(object sender, PaintEventArgs e)
{
Pen p = Pens.Black;
Point loc = new Point (0, 0);
Size s = new Size(12, 15);
Rectangle r = new Rectangle(loc, s);
e.Graphics.DrawEllipse(p, r);
}
 
Back
Top