CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2008
    Posts
    13

    [SOLVED] how to animate this.height ?

    Code:
    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...
    Last edited by alexandergre; February 23rd, 2009 at 11:35 AM.

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: [Question] how to animate this.height ?

    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.

  3. #3
    Join Date
    May 2007
    Posts
    1,546

    Re: [Question] how to animate this.height ?

    Code:
    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.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: [Question] how to animate this.height ?

    Quote Originally Posted by Mutant_Fruit View Post
    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;.

  5. #5
    Join Date
    May 2007
    Posts
    1,546

    Re: [Question] how to animate this.height ?

    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.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: [Question] how to animate this.height ?

    Quote Originally Posted by Mutant_Fruit View Post
    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.

  7. #7
    Join Date
    Sep 2008
    Posts
    13

    Re: [Question] how to animate this.height ?

    Problem solved, thank you very much guys.
    I also have a very very smooth resizing process. here is how I did it:

    Code:
           
    
    
            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;
                }
    
            }

  8. #8
    Join Date
    May 2007
    Posts
    1,546

    Re: [Question] how to animate this.height ?

    Quote Originally Posted by BigEd781 View Post
    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.
    Last edited by Mutant_Fruit; February 23rd, 2009 at 02:27 PM.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  9. #9
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: [Question] how to animate this.height ?

    How about trying AnimateWindow API with AW_SLIDE parameter. Just look at Pinvoke.net, they have a sample.

  10. #10
    Join Date
    Jun 2008
    Posts
    2,477

    Re: [Question] how to animate this.height ?

    Quote Originally Posted by Mutant_Fruit View Post
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured