Trouver une ressource (Nouvelle version du moteur, plus rapide & pertinent, essayez le !)
Vous ne trouvez pas de réponse à votre problème ? Alors posez la question dans le forum.
Souvenez-vous qu'il n'y a jamais de question bête, mais rester dans l'ignorance parce que l'on n'ose pas poser une question, ça c'est une erreur !
DES YEUX (WIN32)
Information sur la source
Description
Deux yeux qui regardent en permanence la souris. Clic gauche maintenu enfoncé pour bouger les yeux sur l'ecran. Touche Echap ou clic droit + quitter pour quitter. Ce sont en fait 2 regions elliptiques combinées. Un timer recalcule la position des iris assez souvent en fonction de la position de la souris. Pour eviter que les yeux clignotent, lors du traitement du message WM_PAINT on dessine tout dans un Device Context en mémoire, puis on l'affiche d'un coup avec BitBlt() sur le DC de la fenetre. Merci a Garslouche pour sa source sur le double buffering.
Source
- #include <windows.h>
- #include <math.h>
- #include "resource.h"
-
- #define CY 81 // hauteur oeil
- #define CX 51 // largeur oeil
- #define PEN 2 // epaisseur contour noir
- #define IRIS 9 // rayon iris
- #define TIMER 1
- #define TIMER_ELAPSE 50
-
-
- //----------------------------------------------------------------------------------
- // variables globales
- //----------------------------------------------------------------------------------
- static POINT id, ig; // coordonnées des iris des yeux
- static POINT cd, cg; // centres des yeux
- static HPEN hPen; // noir, contour des yeux
- static HPEN hIris; // couleur iris
- static HBRUSH hBrush; // iris
- static HMENU hMenu; // menu
-
-
- //----------------------------------------------------------------------------------
- // InitRegion
- //----------------------------------------------------------------------------------
- int __stdcall InitRegion(HWND hWnd)
- {
- HRGN hRgn, hRgnTmp;
-
- // coordonnées des centres des yeux
- cg.y = cd.y = CY / 2;
- cg.x = cd.x = CX / 2;
- cd.x += CX;
-
- // regions eliptiques pour les yeux
- hRgn = CreateEllipticRgn(0, 0, CX, CY);
- hRgnTmp = CreateEllipticRgn(CX, 0, CX + CX, CY);
-
- // region & position de la fenetre
- CombineRgn(hRgn, hRgn, hRgnTmp, RGN_OR);
- SetWindowRgn(hWnd, hRgn, TRUE);
- SetWindowPos(hWnd, NULL, 0, 0, CX + CX, CY, SWP_NOZORDER | SWP_NOMOVE);
-
- DeleteObject(hRgnTmp);
- DeleteObject(hRgn);
- return 0;
- }
-
-
- //----------------------------------------------------------------------------------
- // EyeProc
- //----------------------------------------------------------------------------------
- void __stdcall EyeProc(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
- {
- POINT pm;
- double x, y; // coordonnées de la souris dans le repere de l'oeil
- double a = CX/2 - IRIS, b = CY/2 - IRIS; // petit rayon (horizontal) et grand rayon (vertical) de l'elipse
-
- GetCursorPos(&pm);
- ScreenToClient(hWnd, &pm);
-
- // oeil gauche
- x = (double)(pm.x - cg.x);
- y = (double)(pm.y - cg.y);
- if(((x*x)/(a*a)) + ((y*y)/(b*b)) <= 1.0f) // si <= 1 (equation d'ellipse), la souris est dans l'ellipse
- {
- ig.x = pm.x; // coordonnées souris = coordonnées iris
- ig.y = pm.y; //
- }
- else
- {
- double arg = atan2(y, x); // "argument" des coordonnées de la souris
- ig.x = (long)(cg.x + a * cos(arg)); // coordonnées de l'iris
- ig.y = (long)(cg.y + b * sin(arg)); //
- }
-
- // oeil droit
- x = (double)(pm.x - cd.x);
- y = (double)(pm.y - cd.y);
- if(((x*x)/(a*a)) + ((y*y)/(b*b)) <= 1.0f)
- {
- id.x = pm.x;
- id.y = pm.y;
- }
- else
- {
- double arg = atan2(y, x);
- id.x = (long)(cd.x + a * cos(arg));
- id.y = (long)(cd.y + b * sin(arg));
- }
- InvalidateRect(hWnd, NULL, FALSE);
- UpdateWindow(hWnd);
- }
-
-
- //----------------------------------------------------------------------------------
- // WndProc
- //----------------------------------------------------------------------------------
- LRESULT __stdcall WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- switch(uMsg)
- {
- case WM_PAINT:
- {
- HBITMAP hBmp;
- PAINTSTRUCT ps;
- HDC hDC, hMemDC;
-
- hDC = BeginPaint(hWnd, &ps);
- hMemDC = CreateCompatibleDC(hDC);
- hBmp = CreateCompatibleBitmap(hDC, CX+CX, CY);
- SelectObject(hMemDC, hBmp);
-
- // contours noirs des yeux
- SelectObject(hMemDC, hPen);
- Ellipse(hMemDC, 0, 0, CX-1, CY-1);
- Ellipse(hMemDC, CX, 0, CX+CX-1, CY-1);
-
- // les "iris"
- SelectObject(hMemDC, hBrush);
- SelectObject(hMemDC, hIris);
- Ellipse(hMemDC, ig.x - IRIS, ig.y - IRIS, ig.x + IRIS, ig.y + IRIS);
- Ellipse(hMemDC, id.x - IRIS, id.y - IRIS, id.x + IRIS, id.y + IRIS);
-
- // on affiche d'un coup le MemDC
- BitBlt(hDC, 0, 0, CX + CX, CY, hMemDC, 0, 0, SRCCOPY);
- DeleteObject(hBmp);
- DeleteDC(hMemDC);
- EndPaint(hWnd, &ps);
- }
- return 0;
- case WM_LBUTTONDOWN: // deplacement de la fenetre
- PostMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION, lParam);
- return 0;
- case WM_RBUTTONDOWN: // affichage du menu "quitter"
- {
- POINT pmenu;
-
- pmenu.x = LOWORD(lParam);
- pmenu.y = HIWORD(lParam);
- ClientToScreen(hWnd, &pmenu);
- TrackPopupMenu(hMenu, TPM_LEFTALIGN, pmenu.x, pmenu.y, 0, hWnd, 0);
- }
- return 0;
- case WM_KEYDOWN:
- if(wParam == VK_ESCAPE)
- PostMessage(hWnd, WM_DESTROY, 0, 0);
- return 0;
- case WM_COMMAND:
- if(LOWORD(wParam) == IDM_QUIT)
- PostMessage(hWnd, WM_DESTROY, 0, 0);
- return 0;
- case WM_CREATE:
- {
- HMENU hMenuTmp;
-
- InitRegion(hWnd); // regions
-
- // pen & brushs
- hPen = CreatePen(PS_SOLID, PEN+PEN, RGB(0,0,0));
- hIris = CreatePen(PS_NULL, 1, RGB(0,0,0));
- hBrush = CreateSolidBrush(RGB(0, 76, 167));
-
- // menu & timer
- hMenuTmp = LoadMenu(GetModuleHandle(NULL), MAKEINTRESOURCE(IDR_MENU));
- hMenu = GetSubMenu(hMenuTmp, 0);
- SetTimer(hWnd, TIMER, TIMER_ELAPSE, EyeProc);
- }
- return 0;
- case WM_DESTROY:
- KillTimer(hWnd, TIMER);
- DestroyMenu(hMenu);
- DeleteObject(hPen);
- DeleteObject(hIris);
- DeleteObject(hBrush);
- PostQuitMessage(0);
- return 0;
- }
- return DefWindowProc(hWnd, uMsg, wParam, lParam);
- }
-
-
- //----------------------------------------------------------------------------------
- // WinMain
- //----------------------------------------------------------------------------------
- int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
- {
- HWND hWnd;
- MSG Msg;
- WNDCLASS wndclass;
-
- wndclass.style = CS_HREDRAW | CS_VREDRAW;
- wndclass.lpfnWndProc = WndProc;
- wndclass.cbClsExtra = 0;
- wndclass.cbWndExtra = 0;
- wndclass.hInstance = hInstance ;
- wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
- wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
- wndclass.lpszMenuName = NULL;
- wndclass.lpszClassName = "yeux_Class";
- RegisterClass(&wndclass);
-
- hWnd = CreateWindow("yeux_Class", NULL, WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT, 300, 300, NULL, NULL, NULL, NULL);
-
- ShowWindow(hWnd, iCmdShow);
- UpdateWindow(hWnd);
-
- while(GetMessage(&Msg, NULL, 0, 0))
- {
- TranslateMessage(&Msg);
- DispatchMessage(&Msg);
- }
- return 0;
- }
-
-
-
#include <windows.h>
#include <math.h>
#include "resource.h"
#define CY 81 // hauteur oeil
#define CX 51 // largeur oeil
#define PEN 2 // epaisseur contour noir
#define IRIS 9 // rayon iris
#define TIMER 1
#define TIMER_ELAPSE 50
//----------------------------------------------------------------------------------
// variables globales
//----------------------------------------------------------------------------------
static POINT id, ig; // coordonnées des iris des yeux
static POINT cd, cg; // centres des yeux
static HPEN hPen; // noir, contour des yeux
static HPEN hIris; // couleur iris
static HBRUSH hBrush; // iris
static HMENU hMenu; // menu
//----------------------------------------------------------------------------------
// InitRegion
//----------------------------------------------------------------------------------
int __stdcall InitRegion(HWND hWnd)
{
HRGN hRgn, hRgnTmp;
// coordonnées des centres des yeux
cg.y = cd.y = CY / 2;
cg.x = cd.x = CX / 2;
cd.x += CX;
// regions eliptiques pour les yeux
hRgn = CreateEllipticRgn(0, 0, CX, CY);
hRgnTmp = CreateEllipticRgn(CX, 0, CX + CX, CY);
// region & position de la fenetre
CombineRgn(hRgn, hRgn, hRgnTmp, RGN_OR);
SetWindowRgn(hWnd, hRgn, TRUE);
SetWindowPos(hWnd, NULL, 0, 0, CX + CX, CY, SWP_NOZORDER | SWP_NOMOVE);
DeleteObject(hRgnTmp);
DeleteObject(hRgn);
return 0;
}
//----------------------------------------------------------------------------------
// EyeProc
//----------------------------------------------------------------------------------
void __stdcall EyeProc(HWND hWnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
POINT pm;
double x, y; // coordonnées de la souris dans le repere de l'oeil
double a = CX/2 - IRIS, b = CY/2 - IRIS; // petit rayon (horizontal) et grand rayon (vertical) de l'elipse
GetCursorPos(&pm);
ScreenToClient(hWnd, &pm);
// oeil gauche
x = (double)(pm.x - cg.x);
y = (double)(pm.y - cg.y);
if(((x*x)/(a*a)) + ((y*y)/(b*b)) <= 1.0f) // si <= 1 (equation d'ellipse), la souris est dans l'ellipse
{
ig.x = pm.x; // coordonnées souris = coordonnées iris
ig.y = pm.y; //
}
else
{
double arg = atan2(y, x); // "argument" des coordonnées de la souris
ig.x = (long)(cg.x + a * cos(arg)); // coordonnées de l'iris
ig.y = (long)(cg.y + b * sin(arg)); //
}
// oeil droit
x = (double)(pm.x - cd.x);
y = (double)(pm.y - cd.y);
if(((x*x)/(a*a)) + ((y*y)/(b*b)) <= 1.0f)
{
id.x = pm.x;
id.y = pm.y;
}
else
{
double arg = atan2(y, x);
id.x = (long)(cd.x + a * cos(arg));
id.y = (long)(cd.y + b * sin(arg));
}
InvalidateRect(hWnd, NULL, FALSE);
UpdateWindow(hWnd);
}
//----------------------------------------------------------------------------------
// WndProc
//----------------------------------------------------------------------------------
LRESULT __stdcall WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_PAINT:
{
HBITMAP hBmp;
PAINTSTRUCT ps;
HDC hDC, hMemDC;
hDC = BeginPaint(hWnd, &ps);
hMemDC = CreateCompatibleDC(hDC);
hBmp = CreateCompatibleBitmap(hDC, CX+CX, CY);
SelectObject(hMemDC, hBmp);
// contours noirs des yeux
SelectObject(hMemDC, hPen);
Ellipse(hMemDC, 0, 0, CX-1, CY-1);
Ellipse(hMemDC, CX, 0, CX+CX-1, CY-1);
// les "iris"
SelectObject(hMemDC, hBrush);
SelectObject(hMemDC, hIris);
Ellipse(hMemDC, ig.x - IRIS, ig.y - IRIS, ig.x + IRIS, ig.y + IRIS);
Ellipse(hMemDC, id.x - IRIS, id.y - IRIS, id.x + IRIS, id.y + IRIS);
// on affiche d'un coup le MemDC
BitBlt(hDC, 0, 0, CX + CX, CY, hMemDC, 0, 0, SRCCOPY);
DeleteObject(hBmp);
DeleteDC(hMemDC);
EndPaint(hWnd, &ps);
}
return 0;
case WM_LBUTTONDOWN: // deplacement de la fenetre
PostMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION, lParam);
return 0;
case WM_RBUTTONDOWN: // affichage du menu "quitter"
{
POINT pmenu;
pmenu.x = LOWORD(lParam);
pmenu.y = HIWORD(lParam);
ClientToScreen(hWnd, &pmenu);
TrackPopupMenu(hMenu, TPM_LEFTALIGN, pmenu.x, pmenu.y, 0, hWnd, 0);
}
return 0;
case WM_KEYDOWN:
if(wParam == VK_ESCAPE)
PostMessage(hWnd, WM_DESTROY, 0, 0);
return 0;
case WM_COMMAND:
if(LOWORD(wParam) == IDM_QUIT)
PostMessage(hWnd, WM_DESTROY, 0, 0);
return 0;
case WM_CREATE:
{
HMENU hMenuTmp;
InitRegion(hWnd); // regions
// pen & brushs
hPen = CreatePen(PS_SOLID, PEN+PEN, RGB(0,0,0));
hIris = CreatePen(PS_NULL, 1, RGB(0,0,0));
hBrush = CreateSolidBrush(RGB(0, 76, 167));
// menu & timer
hMenuTmp = LoadMenu(GetModuleHandle(NULL), MAKEINTRESOURCE(IDR_MENU));
hMenu = GetSubMenu(hMenuTmp, 0);
SetTimer(hWnd, TIMER, TIMER_ELAPSE, EyeProc);
}
return 0;
case WM_DESTROY:
KillTimer(hWnd, TIMER);
DestroyMenu(hMenu);
DeleteObject(hPen);
DeleteObject(hIris);
DeleteObject(hBrush);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
//----------------------------------------------------------------------------------
// WinMain
//----------------------------------------------------------------------------------
int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
HWND hWnd;
MSG Msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = "yeux_Class";
RegisterClass(&wndclass);
hWnd = CreateWindow("yeux_Class", NULL, WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT, 300, 300, NULL, NULL, NULL, NULL);
ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return 0;
}
Fichier Zip
Pour les "Membres Club", vous pouvez télécharger directement un fichier contenu dans le zip sans télécharger le zip en entier !
Télécharger le zip
Historique
- 25 septembre 2005 03:05:05 :
- mise à jour.
Sources de la même categorie
Commentaires
Discussions en rapport avec ce code source
|
Téléchargements
Logiciels à télécharger sur le même thème :
|