1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| #include <windows.h>
#include <cstdio>
#include <time.h>
#include <sys/timeb.h>
int get_time_ms () // juste pour avoir l'heure : pas le sujet de ce post...
{ // mais je laisse l'implémentation pour ceux qui veulent tester ce code
struct timeb t ;
ftime( &t ) ;
static unsigned first_sec = 0 ;
static unsigned first_milli ;
if (!first_sec)
{
first_sec = (unsigned)t.time ;
first_milli = (unsigned)t.millitm ;
return 0 ;
}
unsigned sec = (unsigned)t.time ;
unsigned milli = (unsigned)t.millitm ;
return (sec-first_sec)*1000 + milli - first_milli ;
}
// bonne vieille callback à la sauce Win32
LRESULT CALLBACK win_callback ( HWND hWnd , UINT message , WPARAM wParam , LPARAM lParam )
{
static int last = 0 ;
int now = get_time_ms() ;
switch ( message )
{
case WM_MOUSEMOVE :
printf("+") ; // affiche un '+' sur la console à chaque fois qu'on bouge la souris
break ;
case WM_TIMER : // affiche l'heure et son delta à chaque événement timer
printf("\n%d (%d) ",now,now-last ) ;
last = now ;
break ;
}
return DefWindowProc( hWnd,message,wParam,lParam ) ;
}
int main () // le main
{
WNDCLASSEX wcx ;
memset( &wcx,0,sizeof( wcx )) ;
wcx.cbSize = sizeof( wcx ) ;
wcx.lpfnWndProc = win_callback ;
wcx.lpszClassName = "spam" ;
RegisterClassEx( &wcx ) ;
HWND hwnd = CreateWindowEx( NULL,"spam",NULL,WS_POPUP,0,0,100,100,NULL,NULL,NULL,NULL ) ;
ShowWindow( hwnd,TRUE ) ;
SetFocus( hwnd ) ; // créé une fenêtre (on ne la voit pas, mais on sait qu'elle est dans le coin haut-gauche de l'écran)
MSG msg ;
int timer = 0 ;
for (;;) // boucle de gestion des événements
{
if (GetMessage( &msg,hwnd,0,0 ))
{
TranslateMessage( &msg ) ;
DispatchMessage( &msg ) ;
}
SetTimer( hwnd,timer,100,NULL ) ; // on "créé" un événement timer toutes les 100ms
}
return 0 ;
} |
Partager