建一个简单的Windows32 Application,然后将代码copy过去
#define DIVI 16 #define DMEM (DIVI*DIVI) #include <windows.h> #include<stdlib.h> typedef struct { double x,y,mx,my,dx,dy; HBITMAP hbitmap; }piece; int WINAPI WinMain(HINSTANCE hinsta nce,HINSTANCE hprevinstance,PSTR szcmdline,int icmdshow) { HDC hdc,xdc,mdc; HWND hwnd; int cx,cy,counter; piece particle[DMEM]; HBITMAP hbitmap; // For frame locking HPEN hpen = CreatePen(PS_SOLID,0,RGB(0,0,0)); HBRUSH hbrush = (HBRUSH)GetStockObject(BLACK_BRUSH); if(LockWindowUpdate(hwnd = GetDesktopWindow())) { hdc = GetDCEx(hwnd,NULL,DCX_CACHE | DCX_LOCKWINDOWUPDATE); xdc = CreateCompatibleDC(hdc); mdc = CreateCompatibleDC(hdc); // Now get the size of each rectangle cx = GetSystemMetrics(SM_CXSCREEN)/DIVI; cy = GetSystemMetrics(SM_CYSCREEN)/DIVI; // Now create all the pieces... srand((int)GetCurrentTime()); // Initialize random seed hbitmap = CreateCompatibleBitmap(hdc,cx*DIVI,cy*DIVI); // The size of the whole screen for(int index = 0;index < DMEM;index++) { particle[index].hbitmap = CreateCompatibleBitmap(hdc,cx,cy); // The size of a piece is (cx,cy) particle[index].mx = (double)((rand()%16)-8); particle[index].my = (double)((rand()%16)-8); } // Now the pieces need to be grabbed counter = 0; // Reset counter for(int x = 0;x < cx*DIVI;x += cx) { for(int y = 0;y < cy*DIVI;y += cy) { SelectObject(xdc,particle[counter].hbitmap); particle[counter].x = (double)x; particle[counter].y = (double)y; particle[counter].dx = x; particle[counter].dy = y; BitBlt(xdc,0,0,cx,cy,hdc,x,y,SRCCOPY); counter++; } } // Run for 8000 frames for(int frame = 0;frame < 325;frame++) { SelectObject(xdc,hbitmap); SelectObject(xdc,hpen); SelectObject(xdc,hbrush); // Select an empty shader for the result bitmap Rectangle(xdc,0,0,cx*DIVI,cy*DIVI); // Fill with zero value for(int index = 0;index < DMEM;index++) { SelectObject(mdc,particle[index].hbitmap); // Get that bitmap! BitBlt(xdc,(int)particle[index].x,(int)particle[index].y,cx,cy,mdc,0,0,SRCCOPY); // Draw the particle particle[index].x += particle[index].mx; particle[index].y += particle[index].my; particle[index].my += 0.1; } BitBlt(hdc,0,0,cx*DIVI,cy*DIVI,xdc,0,0,SRCCOPY); } Sleep(1000); // Sleep for a second // Now run using a tracking method for recombine! for(int dframe = 0;dframe < 200;dframe++) { SelectObject(xdc,hbitmap); SelectObject(xdc,hpen); SelectObject(xdc,hbrush); // Select an empty shader for the result bitmap Rectangle(xdc,0,0,cx*DIVI,cy*DIVI); // Fill with zero value for(int index = 0;index < DMEM;index++) { if(dframe == 180) { particle[index].x = particle[index].dx; particle[index].y = particle[index].dy; } SelectObject(mdc,particle[index].hbitmap); // Get that bitmap! BitBlt(xdc,(int)particle[index].x,(int)particle[index].y,cx,cy,mdc,0,0,SRCCOPY); // Draw the particle double dist_x = particle[index].x-particle[index].dx; double dist_y = particle[index].y-particle[index].dy; dist_x /= 16; dist_y /= 16; particle[index].x -= dist_x; particle[index].y -= dist_y; } BitBlt(hdc,0,0,cx*DIVI,cy*DIVI,xdc,0,0,SRCCOPY); } // Now pack everything away DeleteObject(hbitmap); for(int dindex = 0;dindex < DMEM;dindex++) DeleteObject(particle[dindex].hbitmap); ReleaseDC(hwnd,hdc); DeleteDC(hdc); DeleteDC(xdc); DeleteDC(mdc); LockWindowUpdate(hwnd); } DeleteObject(hpen); DeleteObject(hbrush); //MessageBox(hwnd,"That was cool!","Cool",MB_ICONEXCLAMATION); return 1; }