C# CreateGraphics()
-
Well, to draw on a panel, i use: Graphics pg = panel.CreateGraphics(); pg.FillEllipse( new SolidBrush( Color.Black ), e.X, e.Y,2,2); Its draw, but, when i minimize the form, the panel is clear! What can i do to not clear the draw? Thanks.
Anytime a window is moved off screen, minimized, or is obstructed from view (by another window) it needs to be repainted then next time it is shown. In order for your graphics to survive this process, the drawing code needs to be called in response to the
Paint
event of the control in question (or in an override ofOnPaint
for a derived control). Also, when you move the code you posted to the Paint event handler, use theGraphics
object supplied in thePaintEventHandler
parameter instead of callingCreateGraphics
. Charlie if(!curlies){ return; } -
Well, to draw on a panel, i use: Graphics pg = panel.CreateGraphics(); pg.FillEllipse( new SolidBrush( Color.Black ), e.X, e.Y,2,2); Its draw, but, when i minimize the form, the panel is clear! What can i do to not clear the draw? Thanks.
You need to modify your code as below:
private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
//create graphics object from PaintEvent Argument,
//(this also reduces the flickkering)
Graphics pg = e.Graphics;
pg.DrawEllipse(new Pen(Color.Black ), 0, 0,
this.panel1.Width,this.panel1.Height);
pg.FillEllipse( new SolidBrush( Color.Red ), 0, 0,
this.panel1.Width, this.panel1.Height);}
-
You need to modify your code as below:
private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
//create graphics object from PaintEvent Argument,
//(this also reduces the flickkering)
Graphics pg = e.Graphics;
pg.DrawEllipse(new Pen(Color.Black ), 0, 0,
this.panel1.Width,this.panel1.Height);
pg.FillEllipse( new SolidBrush( Color.Red ), 0, 0,
this.panel1.Width, this.panel1.Height);}
-
Moon Boy wrote: In this way, how i ll get the mouse position to draw? Thats windows job! Not yours.... top secret xacc-ide 0.0.1
-
Hi MoonBoy, You want me to write the entire code and logic, then below is it:
int mouseX =0, mouseY =0;
private void panel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
//create graphics object from PaintEvent Argument,
//other wise there can be flickkering
Graphics pg = e.Graphics;
pg.DrawEllipse(new Pen(Color.Black ), mouseX, mouseY, 50,50);
pg.FillEllipse( new SolidBrush( Color.Red ),mouseX, mouseY, 50, 50);}
private void panel1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
this.mouseX = e.X;
this.mouseY = e.Y;
this.panel1.Invalidate();
}Do revert back whether it could solve your purpose or not? Regards, Jay.