Pacman_desktop

Description

Voici un petit programme qui crée une icone sur le bureau en forme de pacman et qui cherche à manger ça pièce
je me suis inspiré de la source : http://www.cppfrance.com/codes/ANIMEZ-VOTRE-BUREAU-WIN32_16843.aspx

Source / Exemple :


#include <windows.h>

#define DTime 10
#define DTimeMouve 100
#define TaileIcon 32
HINSTANCE hInst;
HWND hWndDesktop;

HICON     hIcon[5][4];
HICON     hPiece;
POINT     ptIcon= { 0,  0};
POINT     ptPiece={200,200};
int*      iTableau;
HDC       hDC;
int       iPoseIcon=0;
bool      bMouve=0;
int       iMouve=0;
bool      bClikG=0,bTour=0;
HWND FindDesktopWindow(void)
{
    HWND hWnd = FindWindow("Progman", "Program Manager");

    if(GetParent(hWnd) == NULL)
    { 
        HWND hWndEnfant = GetWindow(hWnd, GW_CHILD);
        char szBuf[32];

        while(hWndEnfant != NULL)
        {
            GetClassName(hWndEnfant, szBuf, sizeof(szBuf));
            if(lstrcmp(szBuf, "SHELLDLL_DefView") == 0) break;
            hWndEnfant = GetWindow(hWndEnfant, GW_HWNDNEXT);
        }
        if(hWndEnfant != NULL)
        {
            hWndEnfant = GetWindow(hWndEnfant, GW_CHILD);
            while(hWndEnfant != NULL)
            {
                GetClassName(hWndEnfant, szBuf, sizeof(szBuf));
                if(lstrcmp(szBuf, "SysListView32") == 0) break;
                hWndEnfant = GetWindow(hWndEnfant, GW_HWNDNEXT);
            }
            if(hWndEnfant != NULL) return hWndEnfant; 
        }
    }
    return GetDesktopWindow(); 
}
void EffaceIcon(HWND hWnd,POINT &p)
{
    RECT r;

    r.right  = (r.left = p.x) + TaileIcon;
    r.bottom = (r.top  = p.y) + TaileIcon;
    InvalidateRect(hWnd, &r, true);
    UpdateWindow(hWnd);
}
void DessineIcon(HDC hDC,POINT p,HICON hIcone)
{
    
    DrawIcon(hDC,  p.x,  p.y, hIcone);
    DestroyIcon(hIcone);
}

void DeplaceIcon()
{
    if(ptIcon.x!=ptPiece.x)
    {
        if(ptIcon.x<ptPiece.x)
        {
            iPoseIcon=0;
            ptIcon.x++;
        }
        else
        {
            iPoseIcon=1;
            ptIcon.x--;
        }
    }
    else
    {
        if(ptIcon.y!=ptPiece.y)
        {
            if(ptIcon.y<ptPiece.y)
            {
                iPoseIcon=3;
                ptIcon.y++;
            }
            else
            {
                iPoseIcon=2;
                ptIcon.y--;
            }
        }
    }
    if(ptIcon.y==ptPiece.y&&ptIcon.x==ptPiece.x)
    {
        EffaceIcon(hWndDesktop,ptPiece);
        ptPiece.x=rand()%(GetSystemMetrics(SM_CXSCREEN) - 32);
        ptPiece.y=rand()%(GetSystemMetrics(SM_CYSCREEN) - 64);
        
    }
}

void CALLBACK TimerProc(HWND hWnd, UINT uMsg, UINT uTimerId, DWORD dwTime)
{
   
   DeplaceIcon();
   EffaceIcon(hWndDesktop,ptIcon);
   if(HIBYTE(GetAsyncKeyState(VK_ESCAPE)))
    {
      if(hWndDesktop == GetFocus())
        {
             PostQuitMessage (0);
        }
    }
     
     if(HIBYTE(GetAsyncKeyState(VK_LBUTTON)))
    {
      if(hWndDesktop == GetFocus())
      {
        bClikG=1;
        bTour=1;
        EffaceIcon(hWndDesktop,ptPiece);
        GetCursorPos(&ptPiece);
        ptPiece.x -= 16;
        ptPiece.y -= 32;
        
      }
    }  
    if(bClikG&&!bTour)
    {
        bClikG=0;
        InvalidateRect(hWndDesktop,0,TRUE);
    }
    bTour=0;
     EffaceIcon(hWndDesktop,ptIcon);
    if(hDC = GetDC(hWndDesktop))
    {
      DessineIcon(hDC,ptIcon ,hIcon[iPoseIcon][iMouve]);
      DessineIcon(hDC,ptPiece,hPiece);
      ReleaseDC(hWndDesktop, hDC);
    }   
       
}
void CALLBACK TimerMove(HWND hWnd, UINT uMsg, UINT uTimerId, DWORD dwTime)
{
    if(!bMouve)
    {
        if(iMouve++==2)
        bMouve=true;
    }
    else
    {
        if(iMouve--==0)
        bMouve=false;
    }
}
    
int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    MSG messages;
    DWORD dwPid;
    
    char cBuffer[7];
    hInst = hThisInstance;
    hWndDesktop=FindDesktopWindow(); 
    
    dwPid = GetWindowThreadProcessId(hWndDesktop, NULL);    
    if(!AttachThreadInput(dwPid, GetCurrentThreadId(), TRUE))
    return 0; 
    
   if(!(hPiece = LoadIcon(hInst, "Icon12")))
           return 0;
    
    for(int j=0;j<=3;j++)
    {
        for(int i = 0; i <= 2; i++)
        {
            wsprintf(cBuffer, "Icon%d",i+(j*3));
            if(!(hIcon[j][i] = LoadIcon(hInst, cBuffer)))
            return 0;
        }
    }
        
    
    srand((UINT)GetTickCount());     
    if(hDC = GetDC(hWndDesktop))
        {
            DessineIcon(hDC,ptPiece,hPiece);
            ReleaseDC(hWndDesktop, hDC);
        } 
    SetTimer(NULL, 1, DTime, TimerProc);
    SetTimer(NULL, 2, DTimeMouve, TimerMove);
    while (GetMessage (&messages, NULL, 0, 0))
    {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }
    
    AttachThreadInput(dwPid, GetCurrentThreadId(), FALSE);    
    KillTimer(NULL, 1);
    KillTimer(NULL, 2);
    InvalidateRect(hWndDesktop, 0, TRUE);
    return messages.wParam;
}

Conclusion :


Voila j'espère qu'il vous aura plus :)

Codes Sources

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.