Click to See Complete Forum and Search --> : [Question] how to animate this.height ?
alexandergre
February 22nd, 2009, 04:50 PM
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int h;
private void Form1_Load(object sender, EventArgs e)
{
this.Height = 100;
h = this.Height;
}
private void button1_Click(object sender, EventArgs e)
{
while (h >= 800)
{
this.Height = h;
h++;
}
}
}
}
Why isnt it working? What is wrong?
maybe "this" is related to button1.
I want that the height of the form changes from 100 to 800 smoothly, during 1 second or so.
heeelp... thanx..
*****Ooops! I posted in the wrong category. admin, please move this thread to the right section. sorry...
BigEd781
February 22nd, 2009, 05:00 PM
Because either:
a) it is happening too fast, or
b) the compiler is imply optimizing it away. Not sure about this.
c) the messages are being flattened into a single message in the queue.
See what happens if you put a Thread.Sleep(20) or something at the end of the loop. I don't think this is a good idea, but that's not the point.
Mutant_Fruit
February 22nd, 2009, 05:18 PM
while (h >= 800)
{
this.Height = h;
h++;
}
You'll never enter this loop because h is always *less* than 800 at the start. That should be "<=" not ">=".
Secondly this won't actually work. What you'd need to do is create a timer (System.Windows.Forms.Timer) with an interval of ~1 millisecond and every tick you increase the height by 1 until the height reaches 800. Then stop the timer. This will probably look horrible, but you can improve things from there.
BigEd781
February 22nd, 2009, 06:08 PM
You'll never enter this loop because h is always *less* than 800 at the start. That should be "<=" not ">=".
Didn't even notice that ;). Your method will not work because of how Windows forms work. While your loop is processing it will block the Windows message pump. You are changing the value, but the paint messages will not be processed. So, when the loop exits, your paint message will process and it will simply be the same as doing .Height = 800;.
Mutant_Fruit
February 22nd, 2009, 06:26 PM
Your method will not work because of how Windows forms work.
Luckily, I already covered this and described a method which would work ;)
Secondly this won't actually work. What you'd need to do is create a timer (System.Windows.Forms.Timer) with an interval of ~1 millisecond and every tick you increase the height by 1 until the height reaches 800. Then stop the timer. This will probably look horrible, but you can improve things from there.
BigEd781
February 22nd, 2009, 06:39 PM
Luckily, I already covered this and described a method which would work ;)
Yeah, "luckily" you did, but you didn't explain why it would not work.
alexandergre
February 23rd, 2009, 06:33 AM
Problem solved, thank you very much guys. ;)
I also have a very very smooth resizing process. here is how I did it:
private void button1_Click(object sender, EventArgs e)
{
//resizes the height to 100
timer2.Stop();
timer1.Start();
}
private void button2_Click(object sender, EventArgs e)
{
//resizes the height to 800
timer1.Stop();
timer2.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (this.Height > 200)
{
this.Height = this.Height - 30;
}
else if (this.Height > 120)
{
this.Height = this.Height - 15;
}
else if (this.Height > 100)
{
this.Height = this.Height - 5;
}
}
private void timer2_Tick(object sender, EventArgs e)
{
if (this.Height < 600)
{
this.Height = this.Height + 30;
}
else if (this.Height < 750)
{
this.Height = this.Height + 15;
}
else if (this.Height < 800)
{
this.Height = this.Height + 7;
}
}
Mutant_Fruit
February 23rd, 2009, 01:25 PM
Yeah, "luckily" you did, but you didn't explain why it would not work.
Your previous post looks like you read the first line in my post and hit reply. You quoted the part where I said you need to use <= instead of >= and then said my approach wouldn't work, as if i didn't say the very same in the very next line. There was no reference even to the workable solution immediately following that.
Sorry if i misinterpreted your post, but that's the way I read it ;)
I also have a very very smooth resizing process. here is how I did it:
Looks like a good solution. If you had changed in steps of 1 pixel from 100 -> 800, it probably would've looked like *** and ate CPU time. Glad it's working now.
Shuja Ali
February 23rd, 2009, 01:33 PM
How about trying AnimateWindow API with AW_SLIDE parameter. Just look at Pinvoke.net, they have a sample.
BigEd781
February 23rd, 2009, 03:45 PM
Your previous post looks like you read the first line in my post and hit reply. You quoted the part where I said you need to use <= instead of >= and then said my approach wouldn't work, as if i didn't say the very same in the very next line. There was no reference even to the workable solution immediately following that.
Sorry if i misinterpreted your post, but that's the way I read it ;)
No no no, I just meant to say that you were correct, and the explain a little about why that loop would not work. That's all.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.