drawing on a panel

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.
 
M

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);

mick
 
M

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
 
F

Family Tree Mike

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);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top