A question about delegates of Mouse Events...

B

Beringer

Hello,

I have a panel that I have used as a control to be drawn on.
I want to handle say OnMouseHover or OnMouseDown events. To do this I used
Visual C# and it created a delegate called: panel1_OnMouseDown. I have
provided a snipet from the delegate:
private void panel1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs mea)

{

if (mea.Button == MouseButtons.Left)

{

Graphics grfx = CreateGraphics();

Pen pen = new Pen(Color.AliceBlue, 20);

grfx.DrawLine(pen, 100, 100, 3000, 3000);

grfx.Dispose();

}


}

The problem I have (and as you can see from the very large pen and line
size) is that, while the code excutes, there is no visible line drawn. This
makes me think about the CreateGraphics method. Does the returned graphics
object(grfx) alow me to draw on the panel's client area or somewhere else?
If not on the panel, then how do I draw on the panel?

Thank you in advance,

Eric
 
B

Ben Dewey

In order to draw on a Panel you have to place the code in the Panel.Paint()
event. then get the Graphics object from the
System.Windows.Forms.PaintEventArgs e Object.

So in you mouseDown code set the points you need and say panel1.Refresh();
then in the panel1_Paint do something like
{
e.Graphics.DrawLine(pen, 100, 100, 3000, 3000);
}
 

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