On-Form drawing flickering

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • asalaheddin
    New Member
    • Jul 2008
    • 2

    #1

    On-Form drawing flickering

    Well, to start, I would like to say that I read many of things available online to overcome the notorious flickering problem that all programmers face when trying to draw, resize, or even drag somethings on any form.

    I started my project trying to dissect the screen into 4 quadrant using the mouse position to draw the dissection lines, however it turned out to be a very challenging job and I can get through with the thing by using a form and draw the dividing lines on that form.

    My code snippet is (on the OnPaint event):
    [code=c#]
    Pen p = new Pen(Color.Red, 5);

    Graphics g = this.CreateGrap hics();

    g.DrawLine(p, MousePosition.X , 0, MousePosition.X ,
    SystemInformati on.PrimaryMonit orSize.Height);

    g.DrawLine(p, 0, MousePosition.Y ,
    SystemInformati on.PrimaryMonit orSize.Width,
    MousePosition.Y );
    Refresh();


    g.Dispose();
    [/code]
    Of course the last code will do the job with flickering red lines! so I tried using one of the available double buffering algorithms but it didn't work with me!

    The double buffering code snippet is (on the OnPaint event):
    [code=c#]
    Pen p = new Pen(Color.Red, 5);
    Bitmap offScreenBmp;
    Graphics offScreenDC;
    offScreenBmp = new Bitmap(this.Wid th, this.Height);
    offScreenDC = Graphics.FromIm age(offScreenBm p);


    Graphics clientDC = this.CreateGrap hics();

    offScreenDC.Dra wLine(p, MousePosition.X , 0, MousePosition.X , SystemInformati on.PrimaryMonit orSize.Height);
    offScreenDC.Dra wLine(p, 0, MousePosition.Y , SystemInformati on.PrimaryMonit orSize.Width, MousePosition.Y );

    clientDC.DrawIm age(offScreenBm p, 0, 0);
    Refresh();
    [/code]

    I really hope that someone will have a solution for this problem.

    Note: removing the (Refresh()) will not update the drawing!
    Last edited by Plater; Jul 18 '08, 04:40 PM. Reason: code tags
  • IanWright
    New Member
    • Jan 2008
    • 179

    #2
    Have you tried something simple like this?

    [CODE=c#]
    public partial class Form1 : Form
    {
    public Form1()
    {
    this.SetStyle(C ontrolStyles.Us erPaint, true);
    this.SetStyle(C ontrolStyles.Al lPaintingInWmPa int, true);
    }
    }
    [/CODE]

    I've got this in one of my sample app's that I created for myself some time ago that had crazy flickering, but this prevented it...

    Comment

    • IanWright
      New Member
      • Jan 2008
      • 179

      #3
      In addition from the MSDN website, they include ControlSyles.Do ubleBuffer

      ... "The following code example enables double-buffering on a Form and updates the styles to reflect the changes."

      [CODE=cs]
      public void EnableDoubleBuf fering()
      {
      // Set the value of the double-buffering style bits to true.
      this.SetStyle(C ontrolStyles.Do ubleBuffer |
      ControlStyles.U serPaint |
      ControlStyles.A llPaintingInWmP aint,
      true);
      this.UpdateStyl es();
      }[/CODE]

      Comment

      • asalaheddin
        New Member
        • Jul 2008
        • 2

        #4
        This is the solution with the least flickering so far!

        The code works for about one second more or less! then the flickering starts again!

        I've also tried the code from MSDN which gave me a different problem! The lines that I drew got stuck on the screen!

        ------------------------------------------------------------------------------------------
        Originally posted by IanWright
        In addition from the MSDN website, they include ControlSyles.Do ubleBuffer

        ... "The following code example enables double-buffering on a Form and updates the styles to reflect the changes."

        [CODE=cs]
        public void EnableDoubleBuf fering()
        {
        // Set the value of the double-buffering style bits to true.
        this.SetStyle(C ontrolStyles.Do ubleBuffer |
        ControlStyles.U serPaint |
        ControlStyles.A llPaintingInWmP aint,
        true);
        this.UpdateStyl es();
        }[/CODE]

        This is the solution with the least flickering so far!

        The code works for about one second more or less! then the flickering starts again!

        I've also tried the code from MSDN which gave me a different problem! The lines that I drew got stuck on the screen!

        ------------------------------------------------------------------------------------------

        Comment

        • IanWright
          New Member
          • Jan 2008
          • 179

          #5
          asalaheddin,

          I've just tried reading your original post in a little more detail. I'm not sure exactly what you are doing with your segments, but do you really need to creating a new Graphics object?

          Normally I would do the following;

          [CODE=c#]
          private void Form_Paint(obje ct sender, PaintEventArgs e)
          {
          Graphics g = e.Graphics;
          g.DrawLine( ... );
          Refresh();
          }[/CODE]

          Apart from that, I'm not sure if there is much I can suggest, maybe someone else could follow up if you're still having problems after that...

          Comment

          Working...