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. ControlStyles.OptimizedDoubleBuffer increases my flicker problems

ControlStyles.OptimizedDoubleBuffer increases my flicker problems

Scheduled Pinned Locked Moved C#
csharpasp-netgraphicshardwaretesting
3 Posts 2 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.
  • P Offline
    P Offline
    poppabaggins
    wrote on last edited by
    #1

    I'm new to form programming in C#. Just for learning purposes, I tried double buffering to get rid of my flicker problems with a simple test program. The problem is, that enabling double buffering made increased my flicker exponentially. What should I do to solve this? Here's the code for reference

    public partial class DoubleBufferPractice : Form
        {
            Timer timer;
            Rectangle rect;
    
            public DoubleBufferPractice()
            {
                InitializeComponent();
                this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
                this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
                this.SetStyle(ControlStyles.UserPaint, true);
                rect = new Rectangle(0, this.Height/3, 50, 50);
                InitializeTimer();
            }
    
            private void InitializeTimer()
            {
                timer = new Timer();
                timer.Interval = 10;
                timer.Tick += new EventHandler(timer_Tick);
                timer.Start();
            }
    
            private void timer_Tick(object sender, EventArgs e) 
            {
                if(rect.X + rect.Width < this.Width)
                    rect.X += 10;
                else
                    rect.X = 0;
                this.Invalidate();
            }
    
            protected override void OnPaint(PaintEventArgs e)
            {
                using(Graphics g = this.CreateGraphics()) {
                    using(Brush brush = new SolidBrush(Color.Magenta)) {
                        g.FillRectangle(brush, rect);
                    }
                }
                base.OnPaint(e);
            }
        }
    

    Also, I'm testing this on my laptop with a 2.0ghz Dual Core Pentium, 2gigs ram, and a 256meg nonshared vram NVidia Geforce 8600 M GT, so I don't think that hardware is the problem.

    M 1 Reply Last reply
    0
    • P poppabaggins

      I'm new to form programming in C#. Just for learning purposes, I tried double buffering to get rid of my flicker problems with a simple test program. The problem is, that enabling double buffering made increased my flicker exponentially. What should I do to solve this? Here's the code for reference

      public partial class DoubleBufferPractice : Form
          {
              Timer timer;
              Rectangle rect;
      
              public DoubleBufferPractice()
              {
                  InitializeComponent();
                  this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
                  this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
                  this.SetStyle(ControlStyles.UserPaint, true);
                  rect = new Rectangle(0, this.Height/3, 50, 50);
                  InitializeTimer();
              }
      
              private void InitializeTimer()
              {
                  timer = new Timer();
                  timer.Interval = 10;
                  timer.Tick += new EventHandler(timer_Tick);
                  timer.Start();
              }
      
              private void timer_Tick(object sender, EventArgs e) 
              {
                  if(rect.X + rect.Width < this.Width)
                      rect.X += 10;
                  else
                      rect.X = 0;
                  this.Invalidate();
              }
      
              protected override void OnPaint(PaintEventArgs e)
              {
                  using(Graphics g = this.CreateGraphics()) {
                      using(Brush brush = new SolidBrush(Color.Magenta)) {
                          g.FillRectangle(brush, rect);
                      }
                  }
                  base.OnPaint(e);
              }
          }
      

      Also, I'm testing this on my laptop with a 2.0ghz Dual Core Pentium, 2gigs ram, and a 256meg nonshared vram NVidia Geforce 8600 M GT, so I don't think that hardware is the problem.

      M Offline
      M Offline
      mmikey7
      wrote on last edited by
      #2

      Hi, first of all, do NOT you this.CreateGraphics(). Just use e.Graphics available from parameter like this: protected override void OnPaint(PaintEventArgs e) { //using(Graphics g = this.CreateGraphics()) //{ using(Brush brush = new SolidBrush(Color.Magenta)) { e.Graphics.FillRectangle(brush, rect); } //} base.OnPaint(e); } second of all, interval is in miliseconds so this timer.Interval = 10; will redraw whole form 100 times per second. It may flicker even on good computer. hth

      P 1 Reply Last reply
      0
      • M mmikey7

        Hi, first of all, do NOT you this.CreateGraphics(). Just use e.Graphics available from parameter like this: protected override void OnPaint(PaintEventArgs e) { //using(Graphics g = this.CreateGraphics()) //{ using(Brush brush = new SolidBrush(Color.Magenta)) { e.Graphics.FillRectangle(brush, rect); } //} base.OnPaint(e); } second of all, interval is in miliseconds so this timer.Interval = 10; will redraw whole form 100 times per second. It may flicker even on good computer. hth

        P Offline
        P Offline
        poppabaggins
        wrote on last edited by
        #3

        Thank you so much. changing it to e.Graphics fixed everything. Also, it went from extreme flicker (practically disappearing for a full second or two) to incredibly minor flicker at a 10ms interval.

        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