Hello all . I used to problems with my program shaking when i mioved it off screen. I found code thats supposed to fix it . It came from some program i was tinkering with. Its had many complex textures and all added to its form and the form didnt shake when i move it off screen. I cant seem to find any good explanations or working code for double buffering anywhere i was wondering if somone could help me with what i have found so far ont he topic.

This is the code i dont udnerstand but seems to work for a program.

protected override void OnPaint(PaintEventArgs e)

      Create an offscreen graphics object for double buffering  

         Bitmap offScreenBmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height);  

         using (System.Drawing.Graphics g = Graphics.FromImage(offScreenBmp)) {  

             g.SmoothingMode = SmoothingMode.HighQuality;  

             // Draw the control  

         //  drawControl(g);                                           

            // Draw the image to the screen 

            e.Graphics.DrawImageUnscaled(offScreenBmp, 0, 0);  
         }  

     } 

Its in c but my main project is cli, so ill rewrite it in a minute if need be. The function draw control draws an led light with the g->drawline and other draw functions. In my main form in the InitializeComponent() function at the end i wrote this too.

this->DoubleBuffered = true;

Now that first chunk of code is supposed to work with this->DoubleBuffered = true; it says in the code. I can understanding how to draw an offscreen shape but i dont understand how i would apply this to somthing like a panel instead of drawing that led. Would i create an offscreen image of the texture i want to use in my form then add it to the panel?

I didnt add the code correctly to this post because i cant figure that out, I wish it was like it used to be .thanks for any advice this topic is confusing and undocumented, it required another level of forms understand that i dont have yet, thanks for any tips.

but i dont understand how i would apply this to somthing like a panel

So you know that a Windows::Form::Panel does not support double buffering. ie no DoubledBuffered property.
There are serveral way you can do this and the way I prefer is to create my own doubled buffer panel control and it's quiet simple. The advantage of creating a DoubleBufferedPanel control is that is only a click away when ever you have the need for one.
The code below should get you started.
Hope this helps you
Milton

#pragma once

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;


namespace DBPanel {

    /// <summary>
    /// Summary for DoubleBufferPanelControl
    /// </summary>
    ///
    /// WARNING: If you change the name of this class, you will need to change the
    ///          'Resource File Name' property for the managed resource compiler tool
    ///          associated with all .resx files this class depends on.  Otherwise,
    ///          the designers will not be able to interact properly with localized
    ///          resources associated with this form.
    [ToolboxBitmap("dbPanel.bmp")]
    public ref class DoubleBufferPanel : public Windows::Forms::Panel
    {
    public:
        DoubleBufferPanel(void) 
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
            SetStyle(ControlStyles::UserPaint, true);
            SetStyle(ControlStyles::DoubleBuffer, true);
            SetStyle(ControlStyles::AllPaintingInWmPaint, true);
            UpdateStyles();
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~DoubleBufferPanel()
        {
            if (components)
            {
                delete components;
            }
        }

    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>
        System::ComponentModel::Container^ components;

#pragma region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        void InitializeComponent(void)
        {
            this->SuspendLayout();
            // 
            // DoubleBufferPanel
            // 
            this->Name = L"DoubleBufferPanel";
            this->Size = System::Drawing::Size(247, 100);
            this->ResumeLayout(false);

        }
#pragma endregion
    };
}

Edited 4 Years Ago by Milton Neal

This article has been dead for over six months. Start a new discussion instead.