使用GDI C++制作正方形动画
Animate Squares using GDI 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中的Rectangle函数创建正方形(,并尝试在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中,我设置了一个计时器,创建并绘制给定数量的od正方形(用白色画笔(
#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;
}
我哪里错了?请原谅我的代码注释(这是我的语言(,如果你想让我在这个问题上更具体,请问。
// 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
之后调用。
此外,每当调整主窗口的大小时,您的代码都会将它们调整到客户端区域。不确定你是否打算这么做。
// 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);
}
这似乎是错误的,每次收到定时器消息时都会执行for-loop,并且所得到的位置将每次针对CCD_ 3。
您需要将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++;
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 试试完美的正方形,你能给点小费吗
- 使用 GLUT 使用键停止动画?
- 使用对象数组对 SFML 进行动画处理
- (SFML)按下键时,播放器构造函数未使用正确的动画进行更新
- 如何减慢从 BVH 文件读取的 opengl 动画?
- 如何检测是否在缓冲绘画动画中绘制最后一帧?
- 从精灵表在 sfml 中为精灵制作动画
- OpenGL绘制三角形而不是正方形
- Qt3D 骨骼动画
- ASCII 旋转光标 (TUI) 动画出现问题
- 带有 std::cout 的多线程控制台文本动画
- 有没有办法像动画一样移动控制台窗口?
- 围绕特定轴对 qt3d 旋转进行动画处理
- 如何在Qt中设置围绕固定点的线的动画
- 试图用c++制作一个动画加载圈
- 精灵动画查看器应该使用哪些Qt小部件
- 在 OpenGL 中旋转一个正方形
- 骨骼动画:变换矩阵(collada)之间的插值
- 使用GDI C++制作正方形动画