Date: 02-16-2022
Return to Index
created by gbSnippets
'When performing several actions, or actions on text that is not currently
'in the visible window, or when changing selections repeatedly - an edit
'control may flicker. Disabling a control with WM_SetReDraw before taking
'actions, then turning enabling it after the actions have been taken can
'significantly improve performance.
'Primary Code:
SendMessage(hEdit, %WM_SETREDRAW, 0, 0) 'Turn off redraw for faster and smoother action
SendMessage hEdit, %WM_SETREDRAW, 1, 0 'Turn on redraw again and refresh
'Compilable Example: (Jose Includes)
'This shows the impact of the flicker control statements. Press the first
'button and see the flicker. Press the second button and see the improved
'performance - no flicker and much faster!
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg As DWord, hEdit1 As DWord, hEdit2 As DWord
Function PBMain() As Long
Local style&, buf$
style& = %WS_TabStop Or %WS_Border Or %ES_Left Or %ES_AutoHScroll Or %ES_MultiLine Or %ES_NoHideSel Or %ES_WantReturn
buf$ = "This is sample" + $CrLf + "text for the" + $CrLf + "edit control."
buf$ = buf$ + $CrLf + buf$ + $CrLf + buf$ + $CrLf + buf$ + $CrLf + buf$ + $CrLf + buf$
Dialog New Pixels, 0, "TextBox Test",300,300,200,200, %WS_SysMenu, 0 To hDlg
Control Add Button, hDlg, 100,"Flicker", 10,20,80,20
Control Add Button, hDlg, 150,"Non-Flicker", 110,20,80,20
Control Add TextBox, hDlg, 200,buf$, 10,50, 80,100, style&
Control Add TextBox, hDlg, 250,buf$, 110,50,80,100, style&
Control Handle hDlg, 200 To hEdit1
Control Handle hDlg, 250 To hEdit2
Control Add Label, hDlg, 300,"Flicker", 20,160,80,20
Control Add Label, hDlg, 350,"Non-Flicker", 110,160,80,20
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local i As Long, m&, n&, iResult&
Dim iStart As Long, iEnd As Long, Result As String
If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
iStart = GetTickCount
For i = 1 To 5000
m& = Rnd(0,240)
n& = Rnd(m&,240)
iResult& = SendMessage(hEdit1, %EM_SetSel, m&, n&+1)
Next i
iResult& = SendMessage(hEdit1, %EM_SetSel, 1,2)
iEnd = GetTickCount
Control Set Text hDlg, 300, Format$((iEnd - iStart)/1000,3) & " seconds"
End If
If CB.Msg = %WM_Command AND CB.Ctl = 150 AND CB.Ctlmsg = %BN_Clicked Then
iStart = GetTickCount
SendMessage(hEdit2, %WM_SETREDRAW, 0, 0) 'Turn off redraw for faster and smoother action
For i = 1 To 5000
m& = Rnd(0,240)
n& = Rnd(m&,240)
iResult& = SendMessage(hEdit2, %EM_SetSel, m&, n&+1)
Next i
SendMessage hEdit2, %WM_SETREDRAW, 1, 0 'Turn on redraw again and refresh - may itself cause flicker in RichEdit..
iResult& = SendMessage(hEdit2, %EM_SetSel, 1,2)
iEnd = GetTickCount
Control Set Text hDlg, 350, Format$((iEnd - iStart)/1000,3) & " seconds"
End If
End Function
'gbs_00131
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm