贪吃蛇?
#include <windows.h> LRESULT APIENTRY WndProc(HWND, UINT, WPARAM, LPARAM); //窗口函数 BOOL ckzc(HINSTANCE); //注册窗口类函数 BOOL ckcj(HINSTANCE, int); //创建窗口函数 HINSTANCE hInst; //当前句柄 HWND hWndMain; //窗口句柄 int x=100, y=100, dx=20, dy=0, cxClient, cyClient, color=255; int A[100], B[100], xs, ys, flag, len=3, dlen=0; BOOL ckzc(HINSTANCE hInstance) { WNDCLASSEX wcexMouse; wcexMouse.cbSize = sizeof(WNDCLASSEX); wcexMouse.style = CS_DBLCLKS; //支持鼠标双击 wcexMouse.lpfnWndProc = (WNDPROC)WndProc; wcexMouse.cbClsExtra = 0; wcexMouse.cbWndExtra = 0; wcexMouse.hInstance = hInstance; wcexMouse.hIcon = LoadIcon(NULL,IDI_WINLOGO); wcexMouse.hCursor = LoadCursor(NULL,IDC_ARROW); wcexMouse.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); wcexMouse.lpszMenuName = NULL; wcexMouse.lpszClassName = "MouseWClass"; wcexMouse.hIconSm = LoadIcon(NULL,IDI_WINLOGO); //为了保证程序WIN98/NT的兼容性和可移植性,用以下对窗口进行注册 if (!RegisterClassEx (&wcexMouse)) { if (!RegisterClass((LPWNDCLASS)&wcexMouse.style)) MessageBox (NULL, TEXT ("注册窗口类失败!"), "wcexMouse", MB_ICONERROR); return (FALSE); } return (TRUE); } BOOL ckcj(HINSTANCE hInstance,int nCmdShow) { hInst = hInstance; hWndMain = CreateWindow("MouseWClass", "我的鼠标---cnscom", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT, NULL,NULL,hInstance,NULL); if(!hWndMain) return (FALSE); ShowWindow(hWndMain,nCmdShow); //显示窗口 UpdateWindow(hWndMain); //刷新窗口 return (TRUE); } int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { MSG msg; if(!ckzc(hInstance)) //注册窗口类 return (FALSE); if(!ckcj(hInstance,nCmdShow)) //创建窗口 return (FALSE); InvalidateRect(hWndMain, NULL, TRUE); //按指定区域更新区域 while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return (msg.wParam); } void DrawRectangle (HWND hwnd, int c) { HBRUSH hBrush ; HDC hdc ; RECT rect ; // if (cxClient == 0 || cyClient == 0) return ; SetRect (&rect, x, y, x+20, y+20); if (c) hBrush = CreateSolidBrush (RGB (255, 255, 255)) ; else if (color) hBrush = CreateSolidBrush (RGB (70, 0, 0)) ; else hBrush = CreateSolidBrush (RGB (200, 200, 0)) ; if (c==0)color=255-color; hdc = GetDC (hwnd) ; FillRect (hdc, &rect, hBrush) ; ReleaseDC (hwnd, hdc) ; DeleteObject (hBrush) ; if (++dlen>10) { dlen=0; len++; } } LRESULT APIENTRY WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { HDC hDc, hMemDc; PAINTSTRUCT ps; DWORD fwKeys; POINT ptMouse; char szTitle[100]; static RECT rect1, rect2; static HCURSOR hCursorPen; switch(message) { case WM_CREATE: rect1.left = 90; rect1.right = 170; rect1.top = 8; rect1.bottom = 68; rect2.left = 90; rect2.right = 170; rect2.top = 78; rect2.bottom = 138; hCursorPen = LoadCursor(hInst, "RCCURSOR"); SetTimer (hWnd, 1, 100, NULL) ; // break; case WM_SIZE: cxClient = LOWORD (lParam)/20*20 ; if (cxClient<20) cxClient=20; cyClient = HIWORD (lParam)/20*20 ; if (cyClient<20) cyClient=20; break; case WM_TIMER: xs=x+dx; ys=y+dy; if (xs<0||xs>cxClient||ys<0||ys>cyClient) break; flag=0; for (int i=0; i<=len; i++) { if (xs==A[i]&&ys==B[i]){ flag=1; len=i-1; break; } } // if (flag) break; x=A[len]; y=B[len]; DrawRectangle(hWnd,1); x=xs; y=ys; for (int i=len; i>0; i--) { A[i]=A[i-1]; B[i]=B[i-1]; } A[0]=x; B[0]=y; DrawRectangle(hWnd,0); break; case WM_LBUTTONDOWN: case WM_RBUTTONDOWN: case WM_MOUSEMOVE: ptMouse.x = LOWORD(lParam); //取lParam低字节为横坐标x ptMouse.y = HIWORD(lParam); //取lParam高字节为横坐标y fwKeys = wParam; //将鼠标按钮和键盘组合赋给32位无符号整数fwKeys //很喜欢各个游戏中很有特色的鼠标吧。下面的代码就可以帮助你实现 //判断是否位于窗口之中否则,采用普通的箭头鼠标。 if (PtInRect(&rect1, ptMouse) || PtInRect(&rect2, ptMouse)) //判断指定点是否在指定窗口内 SetCursor(hCursorPen); //在标题栏上显示鼠标的当前位置和按键和按键信息 //把输出格式和输出变量送到szTitle数组 wsprintf(szTitle, "鼠标测试程序 ---- 鼠标位置:[%d,%d]", ptMouse.x, ptMouse.y); if (fwKeys!=0) { //把指定的内容输出到szTitle数组中 lstrcat(szTitle, " ---- 您按下了"); if(fwKeys & MK_CONTROL) lstrcat(szTitle, "Ctrl "); if(fwKeys & MK_SHIFT) lstrcat(szTitle, "Shift "); if(fwKeys & MK_LBUTTON) lstrcat(szTitle, "鼠标左按钮"); if(fwKeys & MK_RBUTTON) lstrcat(szTitle, "鼠标右按钮"); lstrcat(szTitle, "键"); } SetWindowText(hWnd, szTitle); break; case WM_LBUTTONDBLCLK: case WM_RBUTTONDBLCLK: //如果鼠标双击了窗口客户区,那么就显示坐标信息 hDc = GetDC(hWnd); //设置输出字体颜色 SetTextColor(hDc, (message == WM_LBUTTONDBLCLK) ? RGB(255, 0, 0) : RGB(0, 0, 255)); wsprintf(szTitle, "(%d,%d)", LOWORD(lParam), HIWORD(lParam)); TextOut(hDc, LOWORD(lParam), HIWORD(lParam), szTitle, lstrlen(szTitle)); //在指定位置输出文本 ReleaseDC(hWnd, hDc); break; case WM_KEYUP: // SetWindowText(hWnd, "KEYUP"); break; case WM_KEYDOWN: wsprintf(szTitle, "键盘测试程序 ----"); switch(wParam){ case VK_UP: lstrcat(szTitle, "UP"); dy=-20; dx=0; break; case VK_DOWN: lstrcat(szTitle, "DOWN"); dy=20; dx=0; break; case VK_LEFT: lstrcat(szTitle, "LEFT"); dx=-20; dy=0; break; case VK_RIGHT: lstrcat(szTitle, "RIGHT"); dx=20; dy=0; break; } SetWindowText(hWnd, szTitle); break; case WM_PAINT: hDc = BeginPaint(hWnd, &ps); hMemDc = CreateCompatibleDC(hDc); // SelectObject(hMemDc, hSun); BitBlt(hDc, 90, 8, 80, 60, hMemDc, 0, 0, SRCCOPY); DeleteDC(hMemDc); hMemDc = CreateCompatibleDC(hDc); // SelectObject(hMemDc, hMoon); BitBlt(hDc, 90, 78, 80, 60, hMemDc, 0, 0, SRCCOPY); DeleteDC(hMemDc); EndPaint(hWnd, &ps); break; case WM_DESTROY: // DeleteObject(hSun); // DeleteObject(hMoon); PostQuitMessage(0); break; default: return (DefWindowProc(hWnd, message, wParam, lParam)); } return (0); }