Proper use of Refresh() and Invalidate()
-
I'm drawing a few shapes on an img field on a windows form by overriding OnPaint, and using a double buffer as many people have detailed here. However, my shapes disappear after they are drawn. If I put a Refresh() at the end of my OnPaint override, it works (but obviously bogs down)If I set a timer it flickers at the rate of the timer. What am I doing wrong?
-
I'm drawing a few shapes on an img field on a windows form by overriding OnPaint, and using a double buffer as many people have detailed here. However, my shapes disappear after they are drawn. If I put a Refresh() at the end of my OnPaint override, it works (but obviously bogs down)If I set a timer it flickers at the rate of the timer. What am I doing wrong?
Uf, that happend a lot when I first used drawing. What I learned is that drawing directly in OnPaint event isn't good solution in most cases, better draw it in Form_Load for example. What you must know is that in this case you need refresh you drawing in Form's Activated event... because if you hide ur Form (which happends a lot when in debug mode) and than show it again your precious work will be gone... If you follow my suggestion and drop function calling in Page_Load then you'll have code like this:
Form_Load() { DrawStuff(); } Form_Activated() { DrawStuff(); }
In case you stick with OnPaint() I guess simple this.Invalidate(true); in Form_Activated should do the trick. I guess someone will eventualy give you right answer but if you get stuck, drop a code - that will help us to help you. cheers -
Uf, that happend a lot when I first used drawing. What I learned is that drawing directly in OnPaint event isn't good solution in most cases, better draw it in Form_Load for example. What you must know is that in this case you need refresh you drawing in Form's Activated event... because if you hide ur Form (which happends a lot when in debug mode) and than show it again your precious work will be gone... If you follow my suggestion and drop function calling in Page_Load then you'll have code like this:
Form_Load() { DrawStuff(); } Form_Activated() { DrawStuff(); }
In case you stick with OnPaint() I guess simple this.Invalidate(true); in Form_Activated should do the trick. I guess someone will eventualy give you right answer but if you get stuck, drop a code - that will help us to help you. cheersThank you for the response I am still a bit confused, however. One of the shapes I'm drawing I'd like to follow the mouse as it moves over the img box - So I would have to refresh() each time the mousemove event triggers, correct? If I put the refresh() in the mousemove event it seems to work fine as long as the mouse is moving, but as soon as the mouse stops, the shape disappears. This is really frustrating! Thank you for any help - mutty
-
Thank you for the response I am still a bit confused, however. One of the shapes I'm drawing I'd like to follow the mouse as it moves over the img box - So I would have to refresh() each time the mousemove event triggers, correct? If I put the refresh() in the mousemove event it seems to work fine as long as the mouse is moving, but as soon as the mouse stops, the shape disappears. This is really frustrating! Thank you for any help - mutty
This is working for me... even if Refresh in MouseHover is commented... but maybe you should try uncommenting it.
private void pictureBox1_MouseHover(object sender, System.EventArgs e) { //Refresh(); } private void pictureBox1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { Refresh(); } private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { using (Graphics g = this.CreateGraphics()) { PaintEventArgs pa = new PaintEventArgs(g, this.ClientRectangle); g.SmoothingMode = SmoothingMode.AntiAlias; //Draw anything pa.Graphics.DrawLine(new Pen(Color.FromArgb(255, 0, 0, 0)), new PointF(MousePosition.X-10, MousePosition.Y+10), new PointF(MousePosition.X-20, MousePosition.Y+20)); } }
In any case, if you can't get it to work, drop me .cs on pele@zig-zag.net and I'll look into it. Cheers