Graphics ... almost working ... missing one line?
- From: "Peter Webb" <webbfamily@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 9 Dec 2007 00:50:05 +1100
I previously asked about two problems I had with some graphics - the first was that when I drew animation to a picturebox it wouldn't display when the Form loaded.
It was suggested to me by everyone that I stop using my own CreateGraphics calls, and override an OnPaint call to do this. As suggested, I created a custom control "drawBox" with an override on its OnPaint. Because I am using manual double buffering, as people suggested it was really just a matter of putting myBuffer.Render() into the OnPaint, and using drawBox.Invalidate as the call. In the end I only had to change/add about 8 lines of code, and its only taken about 16 hours to work out exactly which ones.
However, there is one annoying problem that remains. I can get it to show the initial screen just fine when it loads. But what I really want it to do is play the opening animation.
If I put the call to animate the screen
play_animation();
As the very last line of Form1_Load or anywhere else that I have tried, then the Form1 doesn't actually load and show anything until the animation is finished, at which point the Form pops up showing only the final (end) frame of the animation.
I had the same problem with the old (bad) way I did graphics, so I assume its not related ... but FWIW, here are they key parts of my new graphics:
public partial class Form1 : Form
{
static public drawBox pictureBoxn;
....
public class drawBox : Control
{
public drawBox()
{
this.Height = 600;
this.Width = 600;
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
myBuffer.Render();
}
}
I call the following in Form1_Load:
pictureBoxn = new drawBox();
currentContext = BufferedGraphicsManager.Current;
myBuffer = currentContext.Allocate(pictureBoxn.CreateGraphics(),
pictureBoxn.DisplayRectangle);
this.Controls.Add(pictureBoxn);
canvas = myBuffer.Graphics;
canvas.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
// canvas.Clear(Color.White);
screenwidth = pictureBoxn.Width;
screenheight = pictureBoxn.Height;
tracks = new Bitmap((int)screenwidth, (int)screenheight);
backgroundcanvas = Graphics.FromImage(tracks);
backgroundcanvas.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
backgroundcanvas.Clear(Color.Red);
pictureBoxn.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBoxn_Paint);
pictureBoxn.Refresh();
pictureBoxn.Show();
pictureBoxn.Update();
pictureBoxn.Invalidate();
.
- Follow-Ups:
- Re: Graphics ... almost working ... missing one line?
- From: Nicholas Paldino [.NET/C# MVP]
- Re: Graphics ... almost working ... missing one line?
- Prev by Date: Re: Input Language
- Next by Date: Re: OOP Question
- Previous by thread: unhandled exception: OutOfMemoryException
- Next by thread: Re: Graphics ... almost working ... missing one line?
- Index(es):
Relevant Pages
|