Results 1 to 13 of 13

Thread: Flickering Text

  1. #1

    Thread Starter
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    Flickering Text

    I am using a timer control to display time but and i am using it at interval = 1. However the text always flickers. Is there anyway i can stop this.

    Any Ideas?

    Thanks

    Jenova

  2. #2

  3. #3

    Thread Starter
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    Re: Flickering Text

    I'm using a timer to display the time in a label. However the text in the label flickers rapidly when i set the timer interval to 1. I would like to keep the timer at interval 1 as well.

    If i had to guess i would more than likely need to double buffer it. But i am not sure how to do that.

  4. #4
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Flickering Text

    why do you need to display the time every millisecond? Just set the timer to 1000.

  5. #5
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Flickering Text

    Try using LockWindowUpdate to prevent flickering:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long
    4.  
    5. Private Sub Form_Load()
    6.     Timer1.Interval = 1
    7.     Timer1.Enabled = True
    8. End Sub
    9.  
    10. Private Sub Timer1_Timer()
    11.     [B]LockWindowUpdate Me.hWnd[/B]
    12.     Label1.Caption = Time
    13.     [B]LockWindowUpdate False[/B]
    14. End Sub

  6. #6
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: Flickering Text

    It has no use to set a timerinterval to 1. I don't think a refresh rate lower then 30ms can be achieved with the normal timer control.

    There is also no use in changing the label's caption, if the caption doesn't change.

    eg

    VB Code:
    1. Private Sub Timer1_Timer()
    2. Dim strTime As String
    3.     strTime = Format(Time, "hh:mm:ss")
    4.     If Label1.Caption <> strTime Then
    5.         Label1.Caption = strTime
    6.     End If
    7. End Sub
    Frans

  7. #7

    Thread Starter
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    Re: Flickering Text

    If i set the timers interval to a second it will delay the display of the time by a second that is why i have it at one. Thank you for all of your help. But i got it.

    VB Code:
    1. Private Declare Function DrawText Lib "user32" Alias "DrawTextA" ( _
    2.                          ByVal hdc As Long, _
    3.                          ByVal lpStr As String, _
    4.                          ByVal nCount As Long, _
    5.                          lpRect As RECT, _
    6.                          ByVal wFormat As Long) _
    7.                          As Long
    8.                          
    9. Private Declare Function SetRect Lib "user32" ( _
    10.                          lpRect As RECT, _
    11.                          ByVal X1 As Long, _
    12.                          ByVal Y1 As Long, _
    13.                          ByVal X2 As Long, _
    14.                          ByVal Y2 As Long) _
    15.                          As Long
    16.  
    17.  
    18. Const DC_TEXT = &H8
    19. Const BF_BOTTOM = &H8
    20. Const BF_LEFT = &H1
    21. Const BF_RIGHT = &H4
    22. Const BF_TOP = &H2
    23. Const DT_CENTER = &H1
    24.  
    25. Private Type RECT
    26.     Left As Long
    27.     Top As Long
    28.     Right As Long
    29.     Bottom As Long
    30. End Type
    31.  
    32. Private Sub Form_Paint()
    33.     Dim R As RECT
    34.     SetRect R, 0, 0, Me.ScaleWidth, 20
    35.     DrawText Me.hdc, Time, Len(Time), R, DT_CENTER
    36. End Sub
    37.  
    38. Private Sub Timer1_Timer()
    39.     Me.Refresh
    40. End Sub

    Regards

    Jenova

  8. #8
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Flickering Text

    I couldn't get your code to work, but the use of API is a little over the top here. Why not just do (with AutoRedraw set to True):
    VB Code:
    1. Private Sub Timer1_Timer()
    2.     Me.Cls
    3.     Me.Print Time
    4. End Sub

  9. #9

  10. #10

    Thread Starter
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    Re: Flickering Text

    Rhinobull:

    Yes i did . Unfortunatly it still flickered alot. But i never thought about doing it through API so i looked through the API viewer and saw draw text and tried that and it worked. Thanks

    Bush Mobile:

    Set the form scalemode to Pixels and not twips then it will work. your example works great too. i always have a habit of going over the top. Thanks alot

    The only problem now is getting it in the label as i will be formating the label caption i.e. colour and size etc. Any ideas

    Jenova

  11. #11
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Flickering Text

    like this?
    VB Code:
    1. Private Sub Timer1_Timer()
    2.     Me.Cls
    3.     Me.ForeColor = vbRed
    4.     Me.FontBold = True
    5.     Me.Print Time
    6. End Sub

  12. #12

    Thread Starter
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    Re: Flickering Text

    Yes but when i resize the form the text has to stay in the centre of the form also the text needs to be sized at font 18. I know that is easy (Font size) but the only problem that i would face would be trying to center (by center io mean in the middle from top to bottom and left to right) the text in the middle of the form when the user tries to resize it.

    That is why i chose to use a label as it may be easier. I don't know anyway apart from the API way to do this. But your example works just as good as the API example so i would rather just use that

  13. #13
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Flickering Text

    VB Code:
    1. Private Sub Timer1_Timer()
    2.     With Me
    3.         .Cls
    4.         .ForeColor = vbRed
    5.         .FontBold = True
    6.         .FontSize = 18
    7.         .CurrentX = (.ScaleWidth - .TextWidth(Time)) \ 2
    8.         .CurrentY = (.ScaleHeight - .TextHeight(Time)) \ 2
    9.         Me.Print Time
    10.     End With
    11. 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