Best time to force a paint

J

Jeff

I have some controls (panels) that I have performed some custom
painting on. I find however that when I drag some other window (like
say an copy of notepad) over and across my application that the paint
disappears. I can repaint manually, but I need to know when to do
it. I tried the various events associated with my form (GotFocus,
LostFocus, Validating, etc), but I could not find the right one to
trigger when I can brought my form back to focus (I would have assumed
that GotFocus would fire at this time).

Can anyone suggest a good time to force a repaint?

Let me go through the steps in case I'm unclear:

1. Start MyApp - it loads on the screen and it has some pretty
painted panels.
2. Open Notepad and move it over top of MyApp.
3. Move Notepad outta the way, MyApp is now no longer so pretty (the
paint is all gone).
4. Drag MyApp around the screen a little. Now the paint is back.
(Just have to give it a shake).

Thoughts?

Thanks.
 
O

Oliver Sturm [MVP C#]

Hi,

All this depends on how you do your painting. You should be doing it in a
Paint event handler or override (if you have derived your own Panel type),
then your custom painting will not be destroyed by other applications. Try
this simple sample - it paints a red cross on a panel, and the drawing
should stay on whatever you do.

public class PanelPaintingForm : Form {
public PanelPaintingForm( ) {
panel = new Panel( );
panel.Paint += new PaintEventHandler(panel_Paint);
panel.Location = new Point(40, 40);
panel.Size = new Size(100, 50);
panel.Parent = this;
}

Panel panel;

void panel_Paint(object sender, PaintEventArgs e) {
e.Graphics.DrawLine(Pens.Red, 0, 0, panel.Width - 1, panel.Height - 1);
e.Graphics.DrawLine(Pens.Red, 0, panel.Height - 1, panel.Width - 1, 0);
}
}


Oliver Sturm
 
J

Jeff

Hi,

All this depends on how you do your painting. You should be doing it in a
Paint event handler or override (if you have derived your own Panel type),
then your custom painting will not be destroyed by other applications. Try
this simple sample - it paints a red cross on a panel, and the drawing
should stay on whatever you do.

public class PanelPaintingForm : Form {
public PanelPaintingForm( ) {
panel = new Panel( );
panel.Paint += new PaintEventHandler(panel_Paint);
panel.Location = new Point(40, 40);
panel.Size = new Size(100, 50);
panel.Parent = this;
}

Panel panel;

void panel_Paint(object sender, PaintEventArgs e) {
e.Graphics.DrawLine(Pens.Red, 0, 0, panel.Width - 1, panel.Height - 1);
e.Graphics.DrawLine(Pens.Red, 0, panel.Height - 1, panel.Width - 1, 0);
}
}

Oliver Sturm
--http://www.sturmnet.org/blog- MVP C#


Thanks very much, I got it working correctly now. I just was
misunderstanding the whole concept.
 

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