Results 1 to 5 of 5

Thread: Shape Blinks While Moving With Timer!

  1. #1

    Thread Starter
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Question Shape Blinks While Moving With Timer!

    Hello Guys.

    I recently posted a thread on " [RESOLVED] Moving Shapes With Timers! in Visual Basic 6 and Earlier" , but there is another related question :

    Why does a shape blink while moving with timer ?! (Of course sometimes it happens!)

    Do other shapes behind the moving shape cause this problem ?!

    Thanks in advance

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,125

    Re: Shape Blinks While Moving With Timer!

    Shapes are drawn on their container, they are not their own window, as are buttons and pictureboxes, so they blink because you see them being erased and redrawn.
    Its been a while, but would setting AutoRedraw on the container, i.e. the form if the shape is drawn on the form, help?

    Edit, I did a quick test and on my machine it is hard to tell, but I don't think AutoRedraw will necessarily help with flashing shapes.
    I've never used shape controls myself. I would just draw the shape myself, and since I'm doing the drawing, then AutoRedraw will definitely keep that from flashing (other than slight tearing since we're not sync'd with the vertical refresh of the monitor).
    Last edited by passel; Jan 27th, 2015 at 04:40 PM.

  3. #3

    Thread Starter
    Addicted Member GlowingVB's Avatar
    Join Date
    Feb 2014
    Posts
    234

    Re: Shape Blinks While Moving With Timer!

    Quote Originally Posted by passel View Post
    Shapes are drawn on their container, they are not their own window, as are buttons and pictureboxes, so they blink because you see them being erased and redrawn.
    Its been a while, but would setting AutoRedraw on the container, i.e. the form if the shape is drawn on the form, help?
    Would you please explain more or give an example ?! (I think I should write Me.AutoRedraw = True in the timer , right ?)
    Last edited by GlowingVB; Jan 27th, 2015 at 04:44 PM.

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,125

    Re: Shape Blinks While Moving With Timer!

    What part needed explaining?
    Here is a quick test. Just add a timer to a form, and a shape control.
    Set the shape to the characteristics you want (color, shape, filled, etc.).
    Once you have it set, copy it and paste as many copies as you want at various locations within the form. (it will ask you when you paste the first copy if you want to make an array, and you should answer Yes).
    You can change the "AutoRedraw = True" line to False, to see if you see any difference between having it on or off.

    When you run the program, the shapes will move around the form bouncing off the walls. On my machine, I pasted 10 copies so had 11 of them running, and it didn't seem to make much difference. I couldn't see too much flashing in either case, but it was probably about the same amount, so I don't think AutoRedraw will make a difference with light controls (controls that are not themselves windows, but are drawn on the surface of their container).

    I had already edited my first post to say I've never used shape controls. I just drew things on the form (or more often, a picturebox) itself, rather than use a shape control. The AutoRedraw doubled buffering will definitely keep flashing to a minimum.
    Code:
    Option Explicit
    Dim dx As Integer, dy As Integer
      Dim dxa() As Integer
      Dim dya() As Integer
    
    Private Sub Form_Load()
      Dim i As Integer
      AutoRedraw = True
      dx = 3
      dy = 2
      ReDim dxa(Shape1.UBound)
      ReDim dya(Shape1.UBound)
      
      For i = 0 To Shape1.UBound
        dxa(i) = dx
        dya(i) = dy
      Next
      
      ScaleMode = vbPixels
      Timer1.Interval = 1
      Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
      Dim i As Integer
      For i = 0 To Shape1.UBound
      
        Shape1(i).Left = Shape1(i).Left + dxa(i)
        Shape1(i).Top = Shape1(i).Top + dya(i)
        
        If Shape1(i).Left <= 0 Then
          Shape1(i).Left = 0
          dxa(i) = -dxa(i)
        ElseIf Shape1(i).Left >= (ScaleWidth - Shape1(i).Width) Then
          Shape1(i).Left = (ScaleWidth - Shape1(i).Width)
          dxa(i) = -dxa(i)
        End If
    
        If Shape1(i).Top <= 0 Then
          Shape1(i).Top = 0
          dya(i) = -dya(i)
        ElseIf Shape1(i).Top >= (ScaleHeight - Shape1(i).Height) Then
          Shape1(i).Top = (ScaleHeight - Shape1(i).Height)
          dya(i) = -dya(i)
        End If
      Next
    End Sub

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,125

    Re: Shape Blinks While Moving With Timer!

    And, for drawing the rectangles yourself instead of using shape controls, here's code to do something similar to the above code.
    If you set AutoRedraw to False in this example, you should see a lot more flashing because it is clearing the whole form and redraing all the rectangles. When using the shapes, it is probably only clearing the area around the shapes old position and redrawing in the new position, which lessens the area that can be caught in the erased condition.
    But, for animation, you wouldn't want to run in a non-double buffered mode.
    Code:
    Option Explicit
    
    Private Type rect  'to hold information about a paricular rectangle object
      left As Single
      top As Single
      width As Single
      height As Single
      color As Long
      dx As Single
      dy As Single
    End Type
    
    Dim rects(10) As rect  'move 11 rectangles around the screen
    
    Private Sub form_load()
      Randomize
      Dim i As Integer
      ScaleMode = vbPixels
      AutoRedraw = True
      
      For i = 0 To UBound(rects)
        With rects(i)
          .dx = 3
          .dy = 2
          .color = vbRed
          .height = 32
          .width = 64
          .left = (ScaleWidth - .width) * Rnd
          .top = (ScaleHeight - .height) * Rnd
        End With
      Next
      
      Timer1.Interval = 1
      Timer1.Enabled = True
    End Sub
    
    Private Sub Timer1_Timer()
      Dim i As Integer
      Cls
      For i = 0 To UBound(rects)
        With rects(i)
          .left = .left + .dx
          .top = .top + .dy
          
          If .left <= 0 Then
            .left = 0
            .dx = -.dx
          ElseIf .left >= (ScaleWidth - .width) Then
            .left = (ScaleWidth - .width)
            .dx = -.dx
          End If
      
          If .top <= 0 Then
            .top = 0
            .dy = -.dy
          ElseIf .top >= (ScaleHeight - .height) Then
            .top = (ScaleHeight - .height)
            .dy = -.dy
          End If
          Line (.left, .top)-Step(.width, .height), .color, BF
        End With
      Next
    End Sub

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