Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Stretch to parent control

Stretch to parent control

Scheduled Pinned Locked Moved C#
performancehelptutorial
14 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L LimitedAtonement

    Dear Mr. Viagra, There might be many ways to do this. Is this in the on-paint method of said control, or is this a child control? Perhaps you're just making a bitmap show up on your Panel, and it should change size dynamically. Well, here's a go: Create a Control : Panel, and override its OnPaint method to do something like this:

    e.Graphics.Clear(BackColor);
    e.Graphics.DrawImage(image, new System.Drawing.Rectangle(0, 0, ClientSize.Width-1, ClientSize.Height-1));

    which will automatically scale. (Be sure to override OnResize with Invaliate(true); to make sure that a full redraw is done.) Then, in the parent form (with the custom Control:Panel as a child control), run something like:

    for (int a = 0; a < 500; a++)
    Control.Size = new System.Drawing.Size(a,a);

    There's an attempt to answer the question, but you have given VERY little information. Warning:I have not tested any of the code above, so it might not work. Good luck!

    In Christ, Aaron Laws http://ProCure.com

    A Offline
    A Offline
    Abhishek Sur
    wrote on last edited by
    #4

    LimitedAtonement wrote:

    Dear Mr. Viagra,

    :wtf: :wtf: :wtf:

    Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


    My Latest Articles-->** Simplify Code Using NDepend
    Basics of Bing Search API using .NET
    Microsoft Bing MAP using Javascript

    1 Reply Last reply
    0
    • Z Zaegra

      Hey, I'd like a control to stretch to it's parents' size in several steps. I have accomplished this, but the result isn't statisfying. This is because im using (Control.) DrawToBitmap in a for loop, which builds up memory pretty fast. I can solve this by using GC.Collect(), but that doesn't look very professional. Thus i'm looking for an alternative, that makes a control stretch in several steps (as in an animation) without flickering. (Perfectly smooth) I'd appreciate some ideas on how to solve this problem ;) Thanks in advance, Zaegra

      Motivation is the key to software development.

      A Offline
      A Offline
      Abhishek Sur
      wrote on last edited by
      #5

      What sort of controls you are talking about ? Are you talking about visual elements? How about Anchor / Dock properties. Have you tried them ?

      Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


      My Latest Articles-->** Simplify Code Using NDepend
      Basics of Bing Search API using .NET
      Microsoft Bing MAP using Javascript

      Z 1 Reply Last reply
      0
      • L LimitedAtonement

        Dear Mr. Viagra, There might be many ways to do this. Is this in the on-paint method of said control, or is this a child control? Perhaps you're just making a bitmap show up on your Panel, and it should change size dynamically. Well, here's a go: Create a Control : Panel, and override its OnPaint method to do something like this:

        e.Graphics.Clear(BackColor);
        e.Graphics.DrawImage(image, new System.Drawing.Rectangle(0, 0, ClientSize.Width-1, ClientSize.Height-1));

        which will automatically scale. (Be sure to override OnResize with Invaliate(true); to make sure that a full redraw is done.) Then, in the parent form (with the custom Control:Panel as a child control), run something like:

        for (int a = 0; a < 500; a++)
        Control.Size = new System.Drawing.Size(a,a);

        There's an attempt to answer the question, but you have given VERY little information. Warning:I have not tested any of the code above, so it might not work. Good luck!

        In Christ, Aaron Laws http://ProCure.com

        0 Offline
        0 Offline
        0x3c0
        wrote on last edited by
        #6

        LimitedAtonement wrote:

        Dear Mr. Viagra,

        That's quite the typo. :laugh: :omg:

        OSDev :)

        1 Reply Last reply
        0
        • A Abhishek Sur

          What sort of controls you are talking about ? Are you talking about visual elements? How about Anchor / Dock properties. Have you tried them ?

          Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.


          My Latest Articles-->** Simplify Code Using NDepend
          Basics of Bing Search API using .NET
          Microsoft Bing MAP using Javascript

          Z Offline
          Z Offline
          Zaegra
          wrote on last edited by
          #7

          I'm talking about a listview, but it should basically work for every control. I need to: 1) Create a bitmap of the control using Control.ToBitmap(), 2) Calculate the aspects ratio between the size of the control and it's parent and 3) Stretch the bitmap and draw it. 4) Repeat so that it looks like an animation. But I've tried to set the backgroundimage, but it won't redraw automatically. Therefore I implemented the Control.Update() method, but this results in a) Good Animation but b) Flicker. And I don't want the flicker :) You understand? :) Thanks for your interest! Zaegra

          Motivation is the key to software development.

          1 Reply Last reply
          0
          • Z Zaegra

            Hey, I'd like a control to stretch to it's parents' size in several steps. I have accomplished this, but the result isn't statisfying. This is because im using (Control.) DrawToBitmap in a for loop, which builds up memory pretty fast. I can solve this by using GC.Collect(), but that doesn't look very professional. Thus i'm looking for an alternative, that makes a control stretch in several steps (as in an animation) without flickering. (Perfectly smooth) I'd appreciate some ideas on how to solve this problem ;) Thanks in advance, Zaegra

            Motivation is the key to software development.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #8

            Hi, 1. You don't need GC.Collect(), using it is a bad idea most of the time. You should however Dispose() of all disposable objects when you no longer need them; prime candidates might be Graphics, Image, Bitmap, Font and then some. 2. I'm not sure I understand your question. If what you need is a non-functional small Control, that slowly grows, and only becomes functional in its final size, then I suggest you: - create the final Control on a Panel "panelCanvas" that is NOT shown on your Form; - turn it into a Bitmap once; - show an empty Panel "panelEmpty" on your Form, onto which you paint the Bitmap in varying sizes until you reach the final size (use Paint handler, a Windows.Forms.Timer and Panel.Invalidate) - finally remove panelEmpty and add panelCanvas to Controls. Of course you should do all of this with a Form that is double-buffered. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


            Z 1 Reply Last reply
            0
            • L Luc Pattyn

              Hi, 1. You don't need GC.Collect(), using it is a bad idea most of the time. You should however Dispose() of all disposable objects when you no longer need them; prime candidates might be Graphics, Image, Bitmap, Font and then some. 2. I'm not sure I understand your question. If what you need is a non-functional small Control, that slowly grows, and only becomes functional in its final size, then I suggest you: - create the final Control on a Panel "panelCanvas" that is NOT shown on your Form; - turn it into a Bitmap once; - show an empty Panel "panelEmpty" on your Form, onto which you paint the Bitmap in varying sizes until you reach the final size (use Paint handler, a Windows.Forms.Timer and Panel.Invalidate) - finally remove panelEmpty and add panelCanvas to Controls. Of course you should do all of this with a Form that is double-buffered. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


              Z Offline
              Z Offline
              Zaegra
              wrote on last edited by
              #9

              Well, the link provided below contains an AVI file with what I am trying to accomplish. It will definately clarify things. [url=http://www1.zippyshare.com/v/64422131/file.html\]example.avi[/url] Even in the example you can see the flicker that occurs. Below is the code that i'm using for the 'animation': (PS: panel1 is the panel that contains the listview that needs to be stretched)

              public void Animate(Control Ctrl, int Steps)
              {
              Bitmap OriginalView = new Bitmap(Ctrl.Width, Ctrl.Height);
              Ctrl.DrawToBitmap(OriginalView, new Rectangle(0,0,Ctrl.Width,Ctrl.Height));
              int ParentWidth = (int)Ctrl.Parent.Width;
              int ParentHeight = (int)Ctrl.Parent.Height;
              int wGrowth = (int)(ParentWidth - Ctrl.Width)/Steps;
              int hGrowth = (int) (ParentHeight - Ctrl.Height)/Steps;
              panel1.BackgroundImage = OriginalView;
              Ctrl.Visible = false;

                      for (int i = 0; i < Steps; i++)
                      {
                          Bitmap NewRender = new Bitmap(OriginalView, new Size(OriginalView.Size.Width + i\*wGrowth, OriginalView.Size.Height + i\*hGrowth));
                          panel1.BackgroundImage = NewRender;
                          panel1.Update(); 
                      }
                      GC.Collect();
                      Ctrl.Visible = true;
                      panel1.BackgroundImage = null;
                      panel1.Update();
                      
              
                  }
              

              And to end up: The dispose method won't work. If i do it throws me all sorts of error of which I still have to find out their sources... Thanks again for your time and idea's, Zaegra

              Motivation is the key to software development.

              L L 2 Replies Last reply
              0
              • Z Zaegra

                Well, the link provided below contains an AVI file with what I am trying to accomplish. It will definately clarify things. [url=http://www1.zippyshare.com/v/64422131/file.html\]example.avi[/url] Even in the example you can see the flicker that occurs. Below is the code that i'm using for the 'animation': (PS: panel1 is the panel that contains the listview that needs to be stretched)

                public void Animate(Control Ctrl, int Steps)
                {
                Bitmap OriginalView = new Bitmap(Ctrl.Width, Ctrl.Height);
                Ctrl.DrawToBitmap(OriginalView, new Rectangle(0,0,Ctrl.Width,Ctrl.Height));
                int ParentWidth = (int)Ctrl.Parent.Width;
                int ParentHeight = (int)Ctrl.Parent.Height;
                int wGrowth = (int)(ParentWidth - Ctrl.Width)/Steps;
                int hGrowth = (int) (ParentHeight - Ctrl.Height)/Steps;
                panel1.BackgroundImage = OriginalView;
                Ctrl.Visible = false;

                        for (int i = 0; i < Steps; i++)
                        {
                            Bitmap NewRender = new Bitmap(OriginalView, new Size(OriginalView.Size.Width + i\*wGrowth, OriginalView.Size.Height + i\*hGrowth));
                            panel1.BackgroundImage = NewRender;
                            panel1.Update(); 
                        }
                        GC.Collect();
                        Ctrl.Visible = true;
                        panel1.BackgroundImage = null;
                        panel1.Update();
                        
                
                    }
                

                And to end up: The dispose method won't work. If i do it throws me all sorts of error of which I still have to find out their sources... Thanks again for your time and idea's, Zaegra

                Motivation is the key to software development.

                L Offline
                L Offline
                LimitedAtonement
                wrote on last edited by
                #10

                Dear Mr. Viagra, Take a look at the property DoubleBuffered. It is used on any control (the control that is updating over and over) to prevent flickering.

                In Christ, Aaron Laws http://ProCure.com

                1 Reply Last reply
                0
                • Z Zaegra

                  Well, the link provided below contains an AVI file with what I am trying to accomplish. It will definately clarify things. [url=http://www1.zippyshare.com/v/64422131/file.html\]example.avi[/url] Even in the example you can see the flicker that occurs. Below is the code that i'm using for the 'animation': (PS: panel1 is the panel that contains the listview that needs to be stretched)

                  public void Animate(Control Ctrl, int Steps)
                  {
                  Bitmap OriginalView = new Bitmap(Ctrl.Width, Ctrl.Height);
                  Ctrl.DrawToBitmap(OriginalView, new Rectangle(0,0,Ctrl.Width,Ctrl.Height));
                  int ParentWidth = (int)Ctrl.Parent.Width;
                  int ParentHeight = (int)Ctrl.Parent.Height;
                  int wGrowth = (int)(ParentWidth - Ctrl.Width)/Steps;
                  int hGrowth = (int) (ParentHeight - Ctrl.Height)/Steps;
                  panel1.BackgroundImage = OriginalView;
                  Ctrl.Visible = false;

                          for (int i = 0; i < Steps; i++)
                          {
                              Bitmap NewRender = new Bitmap(OriginalView, new Size(OriginalView.Size.Width + i\*wGrowth, OriginalView.Size.Height + i\*hGrowth));
                              panel1.BackgroundImage = NewRender;
                              panel1.Update(); 
                          }
                          GC.Collect();
                          Ctrl.Visible = true;
                          panel1.BackgroundImage = null;
                          panel1.Update();
                          
                  
                      }
                  

                  And to end up: The dispose method won't work. If i do it throws me all sorts of error of which I still have to find out their sources... Thanks again for your time and idea's, Zaegra

                  Motivation is the key to software development.

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #11

                  Hi, I've now created a little article[^] that explains how to animate GDI+ on WinForms. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                  Z L 2 Replies Last reply
                  0
                  • L Luc Pattyn

                    Hi, I've now created a little article[^] that explains how to animate GDI+ on WinForms. :)

                    Luc Pattyn [Forum Guidelines] [My Articles]


                    I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                    Z Offline
                    Z Offline
                    Zaegra
                    wrote on last edited by
                    #12

                    That's exactly what I need, thanks Luc! :-D

                    Motivation is the key to software development.

                    1 Reply Last reply
                    0
                    • L Luc Pattyn

                      Hi, I've now created a little article[^] that explains how to animate GDI+ on WinForms. :)

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                      L Offline
                      L Offline
                      LimitedAtonement
                      wrote on last edited by
                      #13

                      Dear Mr. Pattyn, I couldn't see a way to contact you in your `little article.' But, I thought I would cite something I saw in MSDN: " To fully enable double-buffering, you can set the OptimizedDoubleBuffer and AllPaintingInWmPaint bits to true. However the preferred method for enabling double buffering, which yields the same result, is to set the DoubleBuffered property for the control to true. " Nice article, though! [EDIT] http://msdn.microsoft.com/en-us/library/system.windows.forms.controlstyles.aspx[^] Paragraph 3 of Remarks.

                      In Christ, Aaron Laws http://ProCure.com

                      L 1 Reply Last reply
                      0
                      • L LimitedAtonement

                        Dear Mr. Pattyn, I couldn't see a way to contact you in your `little article.' But, I thought I would cite something I saw in MSDN: " To fully enable double-buffering, you can set the OptimizedDoubleBuffer and AllPaintingInWmPaint bits to true. However the preferred method for enabling double buffering, which yields the same result, is to set the DoubleBuffered property for the control to true. " Nice article, though! [EDIT] http://msdn.microsoft.com/en-us/library/system.windows.forms.controlstyles.aspx[^] Paragraph 3 of Remarks.

                        In Christ, Aaron Laws http://ProCure.com

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #14

                        Hi Aaron, thanks for your feedback. I have updated the article, so it now uses the Control.DoubleBuffered property. Cheers.

                        Luc Pattyn [Forum Guidelines] [My Articles]


                        I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups