[cpp]#include <Windows.h>
// Stdio est nécessaire pour sprintf()
#include <stdio.h>
#define WM_SI_CA_MARCHE_MIEUX_COMME_CA WM_USER+1
// Rien de spécial par ici (cf. projet02)
LRESULT CALLBACK MainProc(HWND Dlg,UINT message,WPARAM wParam,LPARAM lParam);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX principale;
principale.cbSize=sizeof(WNDCLASSEX);
principale.style=CS_HREDRAW|CS_VREDRAW;
principale.lpfnWndProc=MainProc;
principale.cbClsExtra=0;
principale.cbWndExtra=0;
principale.hInstance=hInstance;
principale.hIcon=LoadIcon(NULL,IDI_APPLICATION);
principale.hCursor=LoadCursor(NULL,IDC_ARROW);
principale.hbrBackground=reinterpret_cast<HBRUSH>(COLOR_WINDOW+1);
principale.lpszMenuName=NULL;
principale.lpszClassName="std";
principale.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
RegisterClassEx(&principale);
HWND hWnd;
USHORT *status = new USHORT;
hWnd=CreateWindowEx(
WS_EX_CLIENTEDGE,
"std",
"Notre fenêtre",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
);
// On affiche pas la fenêtre
ShowWindow(hWnd,SW_HIDE);
/*TOUTES LES FONCTIONS COMMENCANT PAR "SH" SONT PROPRES A UNE DLL QUI CREE
LE THREAD AUTOMATIQUEMENT EN LUI PASSANT LE HANDLE DE LA FENETRE*/
//INTIALISATION DU THREAD
SH_Init_Library(1,hWnd);
SH_TAG_ScanOnOff(0x0001, status);
//LE THREAD EST A L'ETAT "SUSPEND"
//THREAD ACTIF A PARTIR D'ICI :
SH_Resume_Thread();
MSG msg;
while(GetMessage(&msg,NULL,0,0)==TRUE)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
LRESULT CALLBACK MainProc(HWND hWnd, UINT mes, WPARAM wParam, LPARAM lParam)
{
// Quelques variables utiles
HDC hDC;
PAINTSTRUCT paintst;
POINT pt,pt1;
char buf[256];
switch (mes)
{
case WM_CREATE:
SetTimer(hWnd,NULL,2000,NULL);
return 0;
case WM_LBUTTONDOWN:
// Le bouron gauche est pressé
// On récupère la position écran
GetCursorPos(&pt);
// On la convertit en coordonnées dans la zone client
pt1=pt;
ScreenToClient(hWnd,&pt1);
sprintf(buf,"Clique gauche @ (%d ; %d) [SCR]\nClique gauche @ (%d ; %d) [WND]",pt.x,pt.y,pt1.x,pt1.y);
// On affiche une boite de dialogue prédéfinie
MessageBox(hWnd,buf,"Info",MB_OK);
return 0;
case WM_RBUTTONDOWN:
// Le bouron droit est pressé
// Même traitement
GetCursorPos(&pt);
pt1=pt;
ScreenToClient(hWnd,&pt1);
sprintf(buf,"Clique droit @ (%d ; %d) [SCR]\nClique droit @ (%d ; %d) [WND]",pt.x,pt.y,pt1.x,pt1.y);
MessageBox(hWnd,buf,"Info",MB_OK);
return 0;
case WM_CHAR:
// Une touche est pressée
sprintf(buf,"Caractère '%c' pressé!",(char)wParam); // On récupère le caractère ASCII correspondant
MessageBox(hWnd,buf,"Info",MB_OK);
return 0;
case WM_TIMER:
// Il est temps d'afficher la fenêtre
ShowWindow(hWnd,SW_SHOW);
// Pour ne pas réafficher la fenêtre toutes les 2s
KillTimer(hWnd,NULL);
return 0;
case WM_PAINT:
hDC=BeginPaint(hWnd,&paintst);
EndPaint(hWnd,&paintst);
return 0;
case WM_DESTROY:
// On ferme!
PostQuitMessage(0);
return 0;
case WM_SI_CA_MARCHE_MIEUX_COMME_CA:
//ON SUSPEND LE THREAD POUR EVITER DE RECEVOIR D'AUTRZ MESSAGES !
SH_Suspend_Thread();
sprintf(buf,"THREAD!",(char)wParam); // On récupère le caractère ASCII correspondant
MessageBox(hWnd,buf,"Info",MB_OK);
SH_Resume_Thread();
return 0;
default:
return DefWindowProc(hWnd, mes, wParam, lParam);
}
}[/cpp]