2011-11-21 21 views
0

我想爲許多正方形的動畫製作動畫。在這個例子中我創建了兩個正方形。當我建造並運行時,我可以看到那兩個方格,但他們沒有移動。使用GDI的Animate Squares C++

我創建了代表了一類平方

#pragma once 
#include <windows.h> 

class Square { 


private: 
    int x,y; 

    int moveWith_x, moveWith_y; 

public: 
    void DrawSquare(HDC,int,int,int,int); 
    void MoveSquare(int,int); 

    Square(void); 
    ~Square(void); 
}; 

而且實現文件(在這裏,我創建使用DrawSquare矩形功能方塊),並嘗試動畫在MoveSquares運動:

#include "Square.h" 

    void Square::DrawSquare(HDC hdc,int rx,int ry, int lx, int ly) { 

     Rectangle(hdc,rx,ry,lx,ly); 
    } 

void Square::MoveSquare(int mod_x_Size,int mod_y_Size) { 

     x=x+ moveWith_x; 
     y=y+ moveWith_y; 

      if (x >= mod_x_Size - 30) { 

       x= mod_x_Size - 30 ; 
       moveWith_x = -moveWith_x++; 
      } 


      if (y >= mod_y_Size -30) { 
        y=mod_y_Size - 30 ; 
       moveWith_y= -moveWith_y++; 
      } 

      if (x < 30) { 

      x=30; 
      moveWith_x= -moveWith_x++; 
      } 

      if (y <30) { 

       y=50; 
       moveWith_y= -moveWith_x++; 
      } 
    } 

Square::Square(void) { 
     x=0; 
     y=0; 

     moveWith_x = 0; 
     moveWith_y = 0; 
    } 

    Square::~Square(void) { 

    } 

在我main.cpp我設置了一個定時器,創建並繪製給定數量的正方形(用白色筆刷)

#include <windows.h> 
#include <windowsx.h> 

#include "Square.h" 

#define NUMARUL_DE_FIGURI 2 


LRESULT CALLBACK WndProc (HWND,UINT,WPARAM,LPARAM); 
void moveObject(int,int); 

    //Coordonatele in caz de redimensionare a ferestrei 
    static int mod_x_Size, mod_y_Size; 

    // Crearea ferestrei de lucru 
HWND hwnd; 

int WINAPI WinMain (HINSTANCE hInt, HINSTANCE,LPSTR,int) { 

char atitle[] = "Drawing with GDI"; 
char className[] = "Main Window"; 

MSG msg; 

// Crearea si initializarea ferestrei principale 
WNDCLASS wnd; 
ZeroMemory(&wnd,sizeof(wnd)); 

wnd.lpfnWndProc = WndProc; 
wnd.hInstance = hInt; 
wnd.hIcon = LoadIcon (NULL,IDI_APPLICATION); 
wnd.hCursor = LoadCursor (NULL,IDC_ARROW); 
wnd.hbrBackground = GetStockBrush (BLACK_BRUSH); 
wnd.lpszClassName = className; 

// Inregistrarea clasei in SO 
RegisterClass (&wnd); 


    // Crearea unui timer 
SetTimer (hwnd, 1, 40, NULL) ; 

hwnd = CreateWindow (className,atitle,WS_OVERLAPPEDWINDOW,200,200,600,600,HWND_DESKTOP,NULL,hInt,NULL); 

//Afisarea ferestrei in mod obisnuit 
ShowWindow (hwnd,SW_SHOWNORMAL); 

// Daca aplicatia primeste mesaj de la SO are loc transmiterea p-u prelucrare 
while (GetMessage(&msg,NULL,0,0)) DispatchMessage(&msg); 

    return 0; 
} 

// Prelucrarea mesajelor 
LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { 

    // Structura ce detine informatii despre dispozitivul de desenare 
    PAINTSTRUCT ps; 

    // Descriptorul dispozitivului de desen 
    HDC hdc; 

    Square square[NUMARUL_DE_FIGURI]; 

    // Culoarea figurii 
    HBRUSH shapeColor; 

    switch (msg) { 

// Prelucrarea mesajelor primite la modificarea dimensiunilor ferestrei 
case WM_SIZE :  

     //La fiecare redimensionare a ferestrei se reinnoiesc coordonatele speciale 
     mod_x_Size = LOWORD (lparam) ; 
     mod_y_Size = HIWORD (lparam) ; 
     break ; 

// Prelucrarea mesajelor primite la fiecare interval al timerului 
case WM_TIMER: 

    InvalidateRect (hwnd, NULL, TRUE) ; 

for (int i=0;i<NUMARUL_DE_FIGURI;i++) { 

    square[i].MoveSquare(mod_x_Size, mod_y_Size); 

} 



// Prelucrarea mesajului de desenare 
case WM_PAINT: 

    // Permite desenarea pe dispozitivul de desenare 
    hdc = BeginPaint(hwnd, &ps); 

    // Crearea patratelor 
    for (int i=0;i<NUMARUL_DE_FIGURI;i++) { 

     shapeColor = (HBRUSH)SelectObject(hdc, CreateSolidBrush(RGB(255,255,255))); 

     square[i].DrawSquare(hdc,mod_x_Size-((i*200)+50),mod_y_Size-((i*200)+50),mod_x_Size-((i*20)+100),mod_y_Size-((i*20)+100)); 
    } 

    // 
    //ReleaseDC(hwnd,hdc); 

    // Finiseaza procesul de desenare pe dispozitiv 
    EndPaint(hwnd, &ps); 

    break; 

// Distrugerea ferestrei aplicatiei 
case WM_DESTROY: 
     PostQuitMessage(0); 
     break; 

    default: 
     return DefWindowProc(hwnd, msg, wparam, lparam); 
    } 

    return 0; 
} 

我在哪裏錯了? 原諒我的代碼評論(這是我的語言),如果你想從我的問題更具體問就問。

+0

什麼不按預期工作? – pezcode

+1

我在這個例子中創建了兩個方塊。當我建造並運行時,我可以看到那兩個方格,但他們沒有移動。無論如何它不工作, – develoops

回答

3
// Crearea unui timer 
SetTimer (hwnd, 1, 40, NULL) ; 

hwnd = CreateWindow (className,atitle,WS_OVERLAPPEDWINDOW,200,200,600,600,HWND_DESKTOP,NULL,hInt,NULL); 

您指定的窗口句柄HWND之前調用SetTimer,應該CreateWindow後調用。

此外,每當主窗口調整大小時,代碼都會將它們調整到客戶區。 不知道這是你打算做什麼。

+0

我的錯,但反正沒有幫助 – develoops

+0

@pez太好了+1 – sehe

0
// Prelucrarea mesajelor primite la fiecare interval al timerului 
case WM_TIMER: 
    InvalidateRect (hwnd, NULL, TRUE); 
    for (int i=0;i<NUMARUL_DE_FIGURI;i++) { 
     square[i].MoveSquare(mod_x_Size, mod_y_Size); 
    } 

這似乎是錯誤的,這將每次收到的計時器消息時,做了-loop,並 產生的位置將每次i==NUMARUL_DE_FIGURI

你需要做一個int i(類成員?)狀態變量,只是

case WM_TIMER: 
    if (i<0) i =0; 
    if (i<NUMARUL_DE_FIGURI) 
    { 
     square[i].MoveSquare(mod_x_Size, mod_y_Size); 
     InvalidateRect (hwnd, NULL, TRUE);   
    } 
    i++; 
+0

。當你調試時仍然困惑 – develoops

+1

,你能看到定時器甚至觸發,並且我的值增加嗎? – sehe

+0

是的,價值在增加 – develoops