Results 1 to 18 of 18

Thread: How do I get rid of flicker?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2001
    Location
    Wales (UK)
    Posts
    17

    How do I get rid of flicker?

    Hello,

    Please excuse a dumb, self taught (poorly) VB programmer who devises applications for educational use.

    Here's my problem: I have a form on which I have drawn a graphic, at design time, using the line tool.

    By using various buttons + slider the user is able to change the gradient of one of the lines and/or shift it to the right or the left. I've no problem with any of this.

    But as the line is shifted to right/left and crosses other lines I get some very annoying and distracting flickering. It's as if each time the line moves incrementally all the other lines are redrawn. Is there some coding that I can use to prevent this?
    Peter Luffrum

  2. #2
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    LockWindowUpdate API ?
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Nov 2001
    Location
    Wales (UK)
    Posts
    17
    Thanks for this James. But ......................

    would be grateful if you could point me just that little bit further.
    Peter Luffrum

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Nov 2001
    Location
    Wales (UK)
    Posts
    17
    I've added

    Public Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long

    to the module for the application

    and called it up by LockWindowUpdate (1) at the beginning of the loop that moves the line

    but this does nothing for me.

    Any further help would be much appreciated.
    Peter Luffrum

  5. #5
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    wELL

    Call LockWindowUpdate (me.hwnd)

    To release pass the zero - LockWindowUpdate(0).

    Sorry for now being more specific before..
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Nov 2001
    Location
    Wales (UK)
    Posts
    17
    James,

    I'm making progress (with your help) but:

    where do I put the code LockWindowUpdate (me.hwnd) ?

    Putting it into Form Load locked my whole screen.

    Putting it before the loop to move the line does nothing.

    The code that I have is (you can probably pick holes in this)

    Private Sub Timer1_Timer()

    If ShiftLeft = True Then

    If Step < 400 And Line1.X1 > 1500 Then

    Select Case Step

    Case Step
    Y1 = Y1 + 2 * XCoefficient
    X2 = X2 - 8
    Line1.Y1 = Y1
    Line1.Y2 = Y2
    Line1.X1 = X1
    Line1.X2 = X2

    End Select

    Step = Step + 1

    End If

    Else

    If Step < 400 And Line1.X2 < 11000 Then

    Select Case Step

    Case Step
    Y1 = Y1 - 2 * XCoefficient
    X2 = X2 + 8
    Line1.Y1 = Y1
    Line1.X2 = X2

    End Select

    Step = Step + 1

    End If

    End If

    End Sub


    At what point do I lock the window?
    Peter Luffrum

  7. #7
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    I've never tries this without locking the whole form

    2 suggestions :

    1) Try locking form at the point you set the Line x y values in you select case areas.

    2) If you are drwaing in a picturebox or other container, try, in the same areas, LOCKWINDOWUPDATE(YOURPICTUREBOX or DRAWING IMAGE.hwnd) Do not know if this will work.

    I am now one to pick apart code unless asked to do so...
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Nov 2001
    Location
    Wales (UK)
    Posts
    17
    James,

    Have tried your suggestion (1).

    LockWindowUpdate (me.hwnd) certainly locks the form but when used at this point does not stop the flicker.

    The graphic is drawn on the form and not within a container.

    Cannot continue conversation - am now off home for dinner plus watch some rugby.

    Maybe speak to you over the weekend.

    Thanks for your interest.
    Peter Luffrum

  9. #9
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    We will keep trying at your liesure.

    Enjoy your weekend.
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Nov 2001
    Location
    Wales (UK)
    Posts
    17
    I've taken the liberty of attaching a file to show the problem and action taken (as yet unsuccessful)
    Peter Luffrum

  11. #11
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    The problem is that labels and lines are drawn directly on the form and so each time u move a line over these they have to be redrawn. You have lots of lines and labels and so u get flicker.

    A better way is to use a picture box and directly draw your labels and lines. This greatly reduces resources also.

    I have thrown together a quick sample that doesnt include all the functionality that u wanted but will be a good base for your additions.

    Regards
    Stuart

    VB Code:
    1. 'Form: 1 Picture box (large), 3 command buttons (1 to 3) and
    2. '1 timer - Timer1
    3.  
    4. Option Explicit
    5.  
    6. Dim fCounter As Long
    7. Dim fString As String
    8. Dim fStrHeight As Single
    9. Dim fStrWidth As Single
    10. Dim fGradient As Double
    11. Dim fB As Double
    12. Dim fStep As Double
    13.  
    14. Private Sub Form_Load()
    15.     Command1.Caption = "Up"
    16.     Command2.Caption = "Down"
    17.     Command3.Caption = "Stop"
    18.    
    19.     Timer1.Interval = 100
    20.     '1000 for chart and 200 for borders
    21.     Picture1.Scale (-200, -200)-(1200, 1200)
    22.     With Picture1
    23.         'X & Y axis line
    24.         .ForeColor = vbRed
    25.         .DrawWidth = 2
    26.         Picture1.Line (0, 0)-(0, 1000)
    27.         Picture1.Line (0, 1000)-(1000, 1000)
    28.        
    29.         'Tick marks & labels
    30.         For fCounter = 0 To 1000 Step 100
    31.             Picture1.Line (-20, fCounter)-(0, fCounter) 'Y Tick
    32.             Picture1.Line (fCounter, 1000)-(fCounter, 1020) 'X Tick
    33.            
    34.             fString = CStr(1000 - fCounter)
    35.             fStrHeight = .TextHeight(fString) 'height of label
    36.             fStrWidth = .TextWidth(fString) 'width of label
    37.            
    38.             'Y labels
    39.             .CurrentX = -40 - fStrWidth
    40.             .CurrentY = fCounter - fStrHeight \ 2
    41.             Picture1.Print fString;
    42.        
    43.             'Y labels
    44.             .CurrentX = 1000 - fCounter - fStrWidth \ 2
    45.             .CurrentY = 1030
    46.             Picture1.Print fString;
    47.         Next
    48.        
    49.        
    50.         'Bordering Lines
    51.         'base these on ur formulae nb (1000 - value)
    52.         .ForeColor = vbBlack
    53.         .DrawWidth = 1
    54.         Picture1.Line (0, 200)-(600, 1000)
    55.         Picture1.Line (0, 500)-(900, 1000)
    56.     End With
    57.        
    58.     'Movable Line
    59.     With Line1
    60.         .X1 = 0
    61.         .Y1 = 700
    62.         .X2 = 400
    63.         .Y2 = 1000
    64.         fGradient = -(1000 - .Y1) / .X2
    65.         fB = 1000 - .Y1
    66.     End With
    67. End Sub
    68.  
    69. Private Sub Command1_Click()
    70.     'Move up
    71.     fStep = 20
    72.     Timer1.Enabled = True
    73. End Sub
    74.  
    75. Private Sub Command2_Click()
    76.     'Move down
    77.     fStep = -20
    78.     Timer1.Enabled = True
    79. End Sub
    80.  
    81. Private Sub Timer1_Timer()
    82.     With Line1
    83.         .X2 = .X2 + fStep
    84.         .Y1 = 1000 + fGradient * .X2
    85.     End With
    86. End Sub
    87.  
    88. Private Sub Command3_Click()
    89.     Timer1.Enabled = False
    90. End Sub
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Nov 2001
    Location
    Wales (UK)
    Posts
    17
    Thanks for your help.

    I appreciate that you now expect me to fill in the gaps, but there is one problem. On executing the code it stumbles at

    'Movable Line

    With Line1

    with error message “variable not defined”

    which I suppose is only reasonable given that it’s not.

    Can I declare a line? I clearly need some way of doing this, so that the line has a handle and can then be moved. This was no problem when drawing the line using the line control which is why I went straight to this on my first attempt.

    On declaring

    Dim Line1 as ………..

    I’m offered a set of alternatives including Line. Can I do this?

    When I tried it, the code execution no longer stumbled at

    With Line 1

    but instead at the next line.

    I’m not sure what this is telling me. Any help would be greatly appreciated.

    There’s another point on which I can’t find an answer in my VB books. Suppose I want to draw/colour a polygon. Say I know its corners relative to the scales in the picture box. It seems that I can’t write what I would like – i.e.

    Picture1.polgon ….. and give the corners.

    Is there a way of doing this?

    Peter
    Peter Luffrum

  13. #13
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    Sorry Peter, Line1 was a line control that you put on the form inside the picture box at design time. Is the same line control that u had used in ur version of the program.

    Re: the polygon u can do a search on polygon fill and see API code to do that
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  14. #14
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    Did you get it fuctioning?
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Nov 2001
    Location
    Wales (UK)
    Posts
    17
    Thanks to everyone. It was only after I posted the message this morning that I realised that Line1 meant what it usually means. Mea maxima maxima culpa - too many drinks last night!!!

    I now have the application working as I want it.

    I'll look at the API re polygons.
    Peter Luffrum

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Nov 2001
    Location
    Wales (UK)
    Posts
    17
    Me again I'm afraid. Have searched the API and the following seem to be relevant:

    Public Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As POINTAPI, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long

    Public Declare Function Polygon Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI, ByVal nCount As Long) As Long

    Public Declare Function FillRgn Lib "gdi32" (ByVal hdc As Long, ByVal hRgn As Long, ByVal hBrush As Long) As Long

    Public Type POINTAPI
    x As Long
    y As Long
    End Type

    For the function Polygon, I assume that ByVal nCount As Long simply wants the number of corners, lpPoint As POINTAPI wants a list of the points and ByVal hdc As Long wants an identifier?

    So I tried the following code:

    Dim A As POINTAPI
    Dim B As POINTAPI
    Dim C As POINTAPI
    Dim Polygon1 As Long

    Private Sub Form_Load()

    Picture1.Scale (-200, -200)-(1200, 1200)

    With Picture1

    A.x = 0
    A.y = 0
    B.x = 0
    B.y = 1000
    C.x = 1000
    C.y = 0

    Polygon1 = Polygon(Me.hdc, A - B - C, 3)

    End With

    End Sub

    This stumbled on the A - B - C. Type mismatch error.

    So I tried

    Polygon1 = Polygon(1, A, B, C, 3)

    as the function call, with the same result.

    So how do I separate the points or am I completely up a gum tree?

    Are there books that cover the API, since Win32API.txt gives no help other than listing the functions etc available?

    Another point worries me. Applications that I write need to run on all platforms from Win98 Second Edition (I rule out the first) up to Win XP. So whatever functions I call up from the API must be accessible from all these platforms.
    Peter Luffrum

  17. #17
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi again
    I see u have had no luck on other threads with the polygon api. I had to stretch my little brain to remember all of that algebra / geometry stuff about lines etc and so u can forgive me if i duffed it up. Also it is 12 midnight!!

    Anyway, this code works and u can adjust to your heart's content
    Regards
    Stuart

    VB Code:
    1. Option Explicit
    2.  
    3. Dim fCounter As Long
    4. Dim fString As String
    5. Dim fStrHeight As Single
    6. Dim fStrWidth As Single
    7. Dim fGradient As Double
    8. Dim fB As Double
    9. Dim fStep As Double
    10.  
    11. 'Coordinates of lines to work out intersecting points
    12. Private Type LineType
    13.     X1(1) As Double
    14.     Y1(1) As Double
    15.     X2(1) As Double
    16.     Y2(1) As Double
    17. End Type
    18. Dim MyLine As LineType
    19.  
    20. 'Intersecting points
    21. Dim XInt As Double
    22. Dim YInt As Double
    23.  
    24. 'Polygon api coords
    25. Private Type apiCoords
    26.     x As Long
    27.     y As Long
    28. End Type
    29. Dim PolyPoints(1 To 4) As POINTAPI
    30. Dim lPolyRgn As Long
    31. Dim lBrush As Long
    32. Dim lRet As Long
    33.  
    34. Private Declare Function Polygon Lib "gdi32" (ByVal hdc As Long, lpPoint As apiCoords, ByVal nCount As Long) As Long
    35.  
    36.  
    37. Private Sub Form_Load()
    38.     Command1.Caption = "Up"
    39.     Command2.Caption = "Down"
    40.     Command3.Caption = "Stop"
    41.    
    42.     Timer1.Interval = 100
    43.     '1000 for chart and 200 for borders
    44.     Picture1.Scale (-200, -200)-(1200, 1200)
    45.     With Picture1
    46.         .ForeColor = vbRed
    47.         .DrawWidth = 2
    48.        
    49.         'Tick marks & labels
    50.         '------------------------------------------------------------
    51.         For fCounter = 0 To 1000 Step 100
    52.             Picture1.Line (-20, fCounter)-(0, fCounter) 'Y Tick
    53.             Picture1.Line (fCounter, 1000)-(fCounter, 1020) 'X Tick
    54.            
    55.             fString = CStr(1000 - fCounter)
    56.             fStrHeight = .TextHeight(fString) 'height of label
    57.             fStrWidth = .TextWidth(fString) 'width of label
    58.            
    59.             'Y labels
    60.             .CurrentX = -40 - fStrWidth
    61.             .CurrentY = fCounter - fStrHeight \ 2
    62.             Picture1.Print fString;
    63.        
    64.             'Y labels
    65.             .CurrentX = 1000 - fCounter - fStrWidth \ 2
    66.             .CurrentY = 1030
    67.             Picture1.Print fString;
    68.         Next
    69.    
    70.         .ForeColor = vbBlack
    71.         .DrawWidth = 1
    72.     End With
    73.        
    74.     'Draw lines and find intersecting points
    75.     '------------------------------------------------------------
    76.     With MyLine
    77.         .X1(0) = 0: .Y1(0) = 700: .X2(0) = 600: .Y2(0) = 0
    78.         Picture1.Line (.X1(0), 1000 - .Y1(0))-(.X2(0), 1000 - .Y2(0))
    79.    
    80.         .X1(1) = 0: .Y1(1) = 500: .X2(1) = 900: .Y2(1) = 0
    81.         Picture1.Line (.X1(1), 1000 - .Y1(1))-(.X2(1), 1000 - .Y2(1))
    82.    
    83.         XInt = (.Y1(0) - .Y1(1)) / ((.Y1(0) / .X2(0)) - (.Y1(1) / .X2(1)))
    84.         YInt = -.Y1(0) / .X2(0) * XInt + .Y1(0)
    85.     End With
    86.        
    87.     'Polygon dimensions
    88.     '------------------------------------------------------------
    89.     PolyPoints(1) = Rescale(0, MyLine.Y1(1))
    90.     PolyPoints(2) = Rescale(0, 0)
    91.     PolyPoints(3) = Rescale(MyLine.X2(0), 0)
    92.     PolyPoints(4) = Rescale(XInt, YInt)
    93.    
    94.     'API calls to fill polygon
    95.     '------------------------------------------------------------
    96.     lPolyRgn = CreatePolygonRgn(PolyPoints(1), 4, ALTERNATE)
    97.     lBrush = CreateSolidBrush(RGB(230, 230, 230)) 'Pick any colour
    98.         If Not lBrush = 0 Then
    99.             lRet = FillRgn(Picture1.hdc, lPolyRgn, lBrush)
    100.             lRet = DeleteObject(lBrush)
    101.         End If
    102.     lRet = DeleteObject(lPolyRgn)
    103.    
    104.     'Moved axis drawing here to make sure shows up as thick line
    105.     '------------------------------------------------------------
    106.     With Picture1
    107.         .ForeColor = vbRed
    108.         .DrawWidth = 2
    109.         Picture1.Line (0, 0)-(0, 1000)
    110.         Picture1.Line (0, 1000)-(1000, 1000)
    111.     End With
    112.    
    113.     'Movable Line
    114.     '------------------------------------------------------------
    115.     With Line1
    116.         .X1 = 0
    117.         .Y1 = 700
    118.         .X2 = 400
    119.         .Y2 = 1000
    120.         fGradient = -(1000 - .Y1) / .X2
    121.         fB = 1000 - .Y1
    122.     End With
    123. End Sub
    124.  
    125. Private Function Rescale(ByVal XCoord As Long, ByVal YCoord As Long) As POINTAPI
    126. 'Function to rescale coordinates to suit scaling
    127.     With Picture1
    128.         Rescale.x = (XCoord + 200) / Screen.TwipsPerPixelX * .Width / .ScaleWidth
    129.         Rescale.y = (1200 - YCoord) / Screen.TwipsPerPixelY * .Height / .ScaleHeight
    130.     End With
    131. End Function
    132.  
    133. Private Sub Command1_Click()
    134.     'Move up
    135.     fStep = 20
    136.     Timer1.Enabled = True
    137. End Sub
    138.  
    139. Private Sub Command2_Click()
    140.     'Move down
    141.     fStep = -20
    142.     Timer1.Enabled = True
    143. End Sub
    144.  
    145. Private Sub Timer1_Timer()
    146.     With Line1
    147.         .X2 = .X2 + fStep
    148.         .Y1 = 1000 + fGradient * .X2
    149.     End With
    150. End Sub
    151.  
    152. Private Sub Command3_Click()
    153.     Timer1.Enabled = False
    154. End Sub
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Nov 2001
    Location
    Wales (UK)
    Posts
    17
    Many thanks for your help
    Peter Luffrum

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