Results 1 to 9 of 9

Thread: [RESOLVED] User control Flickering

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Resolved [RESOLVED] User control Flickering

    I have a user control that I created using GDI+, and .Net 2005. But there is a lot of flickering. I turned on DoubleBuffering and the flicker got ten times worse. The control that I created is a level meter( the meter on some audio devices that show the audio levels. Usually a bar that is green to yellow to red). So the needs to refresh about every 100 milliseconds.

    The way I designed it makes it look a bit like led lights. When the value lights up the led lights. And they are darened when not higher than the current value. I've attached what the control looks like. Anyone have an idea how I can get rid of the flickering, or reduse it? Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: User control Flickering

    I'm no guru at this stuff but I'd think that you'd want to redraw as little as possible at each stage. If you are just refreshing the control and handling the drawing on the Paint event then I'd guess that that is less efficient than it needs to be. Obviously you must redraw the whole thing on the Paint event, but I'd suggest that each time you check the level you only actually redraw the parts that need it. You would test the current level and the previous level, then only redraw the boxes that need to change colour, e.g. if last recorded level was 10 and new level is 12 just redraw boxes 11 and 12 in bright colour, or if last level was 7 and current level is 3 just redraw boxes 4, 5, 6 and 7 in dark colour.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    Re: User control Flickering

    You are not required to Paint the whole control in the Paint event, which you shouldn't use when you are creating a user control as it will occur after the underlying base control is painted, use the OnPaint override instead so you are in full control (possibly even the OnBackGroundPaint as well). You only have to redraw the invalidated region which is given to you by the PaintEventArgs class (e) (in some cases that will be the whole control)

    e.ClipRectangle gives you the rectangle that has been invalidated.

    You should invalidate only the rectangle that needs redrawing, then in the overriden OnPaint you should calculate which elements fit in that rectangle and only redraw them.

    The actual drawing takes up the most time, so it's OK to have bulk load of mathematics in order to prevent the unneeded repainting.
    Last edited by grilkip; Feb 6th, 2006 at 12:03 PM.
    "so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman

  4. #4
    Addicted Member
    Join Date
    Sep 2004
    Location
    UK
    Posts
    129

    Re: User control Flickering

    Make sure you use the graphics object supplied by the paint event and do not use CreateGraphics

    Supercharged

  5. #5
    Fanatic Member
    Join Date
    May 2005
    Posts
    898

    Re: User control Flickering

    That's right, you shouldn't use CreateGraphics, but you also shouldn't use the Paint event as it will occur after the base control has already been painted. You must override the OnPaint sub for efficiency's sake.
    "so just keep in mind that fantasy is not the same as realtiy and make sure u remember that wii sports may be fun but u cant count on it as exercise ok cool bye" - HungarianHuman

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: User control Flickering

    In addition to using the OnPaint override you can benefit tremendously by creating a backbuffer.

    Here is what I use in my controls to creat th buffer.

    VB Code:
    1. #Region " Constructors "
    2.         Public Sub New()
    3.             MyBase.New()
    4.             Me.SetStyle(ControlStyles.DoubleBuffer, True)
    5.             Me.SetStyle(ControlStyles.UserPaint, True)
    6.             Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
    7.             Me.SetStyle(ControlStyles.ResizeRedraw, True)
    8.             Me.UpdateStyles()
    9.         End Sub
    10. #End Region
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: User control Flickering

    This is c#, but I was doing something similar, but with alot more calls to FillRect than it appears you have..

    http://www.vbforums.com/showthread.php?t=370733

    What I figured out, eventually, was that I nearly eliminated the flicker by doing all my individual rectangle draws to a bitmap object, and only calling DrawImage once with the completed bitmap..

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: User control Flickering

    Thanks for all the input. Right now I have somethign that takes precedence over this now, so I'm nto going to be able to try these fixes for a couple days. But from what I've read I think this is resolved.

  9. #9
    New Member
    Join Date
    Nov 2012
    Posts
    6

    Resolved Re: User control Flickering

    Quote Originally Posted by Supercharged View Post
    Make sure you use the graphics object supplied by the paint event and do not use CreateGraphics

    Supercharged
    You are a genius. This solved my problem completely!

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