Form not showing after Invalidate()
For some reason, this code does not actually draw my bitmap file... or show the form.
namespace GraphicsEngine
{
public partial class Form1 : Form
{
Bitmap[] dude = new Bitmap[3];
Bitmap dude0 = new Bitmap(@"C:\Directory.bmp");
Point renderpoint = new Point(1, 1);
public Form1()
{
dude[0] = new Bitmap(@"C:\Directory.bmp");
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MainL开发者_开发问答oop();
}
private void MainLoop()
{
double FPS = 30.0;
long ticks1 = 0;
long ticks2 = 0;
double interval = (double)Stopwatch.Frequency / FPS;
while (!this.IsDisposed)
{
ticks2 = Stopwatch.GetTimestamp();
if (ticks2 >= ticks1 + interval)
{
ticks1 = Stopwatch.GetTimestamp();
this.Invalidate();
}
Thread.Sleep(1);
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawImage(dude0, renderpoint);
}
}
}
Any ideas?
Your problem ought to be a bit more obvious than not seeing the bitmap, you should not see the form either. That's because you never complete the Load event. You could use the Shown event instead.
Check this thread for the code for a true game loop.
Try replacing your call to this.Invalidate();
with this.Refresh();
.
Looks like your MainLoop() could be an infinite loop. You can put Console.Out.WriteLine(ticks1);
in your while loop to validate this.
It gets stuck in the Load event handler and none of the main form validation occurs. It definitely draws the picture if you comment out the call to MainLoop().
Form1_Paint is not called unless if you have UserPaint set to true. Try this as your constructor
public Form1() {
dude[0] = new Bitmap(@"C:\Directory.bmp");
InitializeComponent();
this.SetStyle( System.Windows.Forms.ControlStyles.UserPaint, true );
this.SetStyle( System.Windows.Forms.ControlStyles.AllPaintingInWmPaint, true );
this.SetStyle( System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer, true );
}
Just beware, when you do this, you may be responsible for all the paints on the form.
精彩评论