Goodmorning :)
I need in one of my projects to use the DoEvents statement. Many programmers said me that is not a good way use it for vary reasons, but as I need it into a function, I thought that there might be another better solution. With a Google query, I found this code:
It seems work well and faster than the DoEvents statement. But can I really trust in this code lines? Can I be sure that if I replace all the DoEvents calls with MyDoEvents any problems won't be raised?
Thank you for all your opinions and suggestions,
:wave:
I need in one of my projects to use the DoEvents statement. Many programmers said me that is not a good way use it for vary reasons, but as I need it into a function, I thought that there might be another better solution. With a Google query, I found this code:
Code:
Option Explicit
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type MSG
hwnd As Long 'hwnd della finestra di destinazione del messaggio
message As Long 'numero che identifica il tipo di messaggio
wParam As Long 'parametro del messaggio (tipo e valore cambiano in funzione del tipo di messaggio inviato)
lParam As Long 'altro parametro del messaggio che varia sempre in funzione del messaggio
time As Long 'l'ora in cui è stato inviato il messaggio
pt As POINTAPI 'posizione del cursore al momento dell'invio del messaggio
End Type
'La funzione PeekMessage cerca e recupera un messaggio in coda e lascia immediatamente il controllo al programma
'Se il messaggio viene recuperato la funzione restituisce TRUE, in caso contrario restituisce FALSE.
'La funzione prevede un parametro (wRemoveMsg) che indica se rimuovere il messaggio recuperato dalla coda o di lasciarlo dove si trova.
Private Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" ( _
lpMsg As MSG, _
ByVal hwnd As Long, _
ByVal wMsgFilterMin As Long, _
ByVal wMsgFilterMax As Long, _
ByVal wRemoveMsg As Long) As Long
Private Const PM_REMOVE = &H1 'indica alla funzione PeekMessage di rimuovere il messaggio in coda
'La funzione TranslateMessage codifica alcuni messaggi arrivati dalla tastiera in modo da renderli opportunamente eleborabili.
Private Declare Function TranslateMessage Lib "user32" (lpMsg As MSG) As Long
'La funzione DispatchMessage richiama la corretta procedura che deve elaborare il messaggio in base al valore del campo hwnd della struttura MSG
'N.B.: l'omissione di questa funzione porterebbe alla mancata elaborazione dei messaggi e, quindi, al probabile blocco del sistema.
Private Declare Function DispatchMessage Lib "user32" Alias "DispatchMessageA" (lpMsg As MSG) As Long
Private Const lMaxCounter As Long = 100000
Private Sub MyDoEvents() 'funzione alternativa alla funzione di VB "DoEvents"
Dim CurrMsg As MSG
'Questo loop recupera (e cancella) tutti i messaggi dalla coda e li invia alla finestra di destinazione
Do While PeekMessage(CurrMsg, 0, 0, 0, PM_REMOVE) <> 0
TranslateMessage CurrMsg
DispatchMessage CurrMsg
Loop
End Sub
Thank you for all your opinions and suggestions,
:wave: