- #include <windows.h>
- #include <stdio.h>
- #include <time.h>
- #define BLOCK 100
- LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);
- int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
- {
- WNDCLASS wndclass;
- MSG msg;
- char szAppName[80] = "别踩白块儿 by 流星不死";
- HWND hwnd;
- wndclass.cbClsExtra = 0;
- wndclass.cbWndExtra = 0;
- wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
- wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
- wndclass.hIcon = LoadIcon(NULL,IDI_ERROR);
- wndclass.hInstance = hInstance;
- wndclass.lpfnWndProc = WndProc;
- wndclass.lpszClassName = szAppName;
- wndclass.lpszMenuName = NULL;
- wndclass.style = CS_HREDRAW | CS_VREDRAW;
- RegisterClass(&wndclass);
- hwnd = CreateWindow(szAppName,szAppName,WS_SYSMENU | WS_CAPTION,400,100,BLOCK*4+4,BLOCK*4+24,NULL,NULL,hInstance,NULL);
- ShowWindow(hwnd,nShowCmd);
- UpdateWindow(hwnd);
- while(GetMessage(&msg,NULL,0,0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return FALSE;
- }
- LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
- {
- static int bw[4],tm = 0,n = 0,flag = 0;
- static RECT rect;
- POINT point;
- INT i;
- HDC hdc;
- PAINTSTRUCT ps;
- char tmpstr[80];
- HBRUSH hbrush;
- switch(message)
- {
- case WM_CREATE:
- srand((unsigned)time(NULL));
- for(i=0;i<4;i++)
- bw[i] = rand()%4;
- SetTimer(hwnd,0,10,NULL);
- return 0;
- case WM_TIMER:
- tm++;
- return 0;
- case WM_CHAR:
- hdc = GetDC(hwnd);
- if(-1 == flag)
- {
- wsprintf(tmpstr,"你输了! 用时:%d.%d秒 格数:%d个",tm/100,tm-(tm/100)*100,n);
- MessageBox(NULL,tmpstr,tmpstr,0);
- exit(0);
- return 0;
- }
- if(wParam-49 != bw[3])
- {
- flag = -1,KillTimer(hwnd,0);;
- wsprintf(tmpstr,"你输了! 用时:%d.%d秒 格数:%d个",tm/100,tm-(tm/100)*100,n);
- MessageBox(NULL,tmpstr,tmpstr,0);
- exit(0);
- return 0;
- }
- for(i=3;i>=1;i--) bw[i] = bw[i-1];
- bw[0] = rand()%4,n++;;
- ScrollWindow(hwnd,0,BLOCK,NULL,NULL);
- hbrush = CreateSolidBrush(RGB(0,0,0));
- SetRect(&rect,bw[0]*BLOCK,0,bw[0]*BLOCK+BLOCK,BLOCK);
- FillRect(hdc,&rect,hbrush);
- DeleteObject(hbrush);
- ReleaseDC(hwnd,hdc);
- return 0;
- case WM_LBUTTONDOWN:
- point.x = LOWORD(lParam),point.y = HIWORD(lParam);
- SendMessage(hwnd,WM_CHAR,point.x/BLOCK+49,0);
- return 0;
- case WM_PAINT:
- hdc = BeginPaint(hwnd,&ps);
- for(i=0;i<4;i++)
- {
- SetRect(&rect,bw[i]*BLOCK,i*BLOCK,bw[i]*BLOCK+BLOCK,i*BLOCK+BLOCK);
- hbrush = CreateSolidBrush(RGB(0,0,0));
- FillRect(hdc,&rect,hbrush);
- DeleteObject(hbrush);
- }
- EndPaint(hwnd,&ps);
- return 0;
- case WM_DESTROY:PostQuitMessage(0);return 0;
- }
- return DefWindowProc(hwnd,message,wParam,lParam);
- }
- //该片段来自于http://www.codesnippet.cn/detail/1910201513831.html
来源: http://www.codesnippet.cn/detail/1910201513831.html