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
Printable View
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
What txt and where? Also, what happens when timer fires?
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.
why do you need to display the time every millisecond? Just set the timer to 1000.
Try using LockWindowUpdate to prevent flickering:
VB Code:
Option Explicit Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock As Long) As Long Private Sub Form_Load() Timer1.Interval = 1 Timer1.Enabled = True End Sub Private Sub Timer1_Timer() [B]LockWindowUpdate Me.hWnd[/B] Label1.Caption = Time [B]LockWindowUpdate False[/B] End Sub
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:
Private Sub Timer1_Timer() Dim strTime As String strTime = Format(Time, "hh:mm:ss") If Label1.Caption <> strTime Then Label1.Caption = strTime End If End Sub
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:
Private Declare Function DrawText Lib "user32" Alias "DrawTextA" ( _ ByVal hdc As Long, _ ByVal lpStr As String, _ ByVal nCount As Long, _ lpRect As RECT, _ ByVal wFormat As Long) _ As Long Private Declare Function SetRect Lib "user32" ( _ lpRect As RECT, _ ByVal X1 As Long, _ ByVal Y1 As Long, _ ByVal X2 As Long, _ ByVal Y2 As Long) _ As Long Const DC_TEXT = &H8 Const BF_BOTTOM = &H8 Const BF_LEFT = &H1 Const BF_RIGHT = &H4 Const BF_TOP = &H2 Const DT_CENTER = &H1 Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Sub Form_Paint() Dim R As RECT SetRect R, 0, 0, Me.ScaleWidth, 20 DrawText Me.hdc, Time, Len(Time), R, DT_CENTER End Sub Private Sub Timer1_Timer() Me.Refresh End Sub
Regards
Jenova
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:
Private Sub Timer1_Timer() Me.Cls Me.Print Time End Sub
AutoRedraw causes flickering more often then never... Did you try using LockWindowUpdate as I suggested yet?
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
like this?VB Code:
Private Sub Timer1_Timer() Me.Cls Me.ForeColor = vbRed Me.FontBold = True Me.Print Time End Sub
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 :)
VB Code:
Private Sub Timer1_Timer() With Me .Cls .ForeColor = vbRed .FontBold = True .FontSize = 18 .CurrentX = (.ScaleWidth - .TextWidth(Time)) \ 2 .CurrentY = (.ScaleHeight - .TextHeight(Time)) \ 2 Me.Print Time End With End Sub