newTest.cpp
资源名称:newTest.rar [点击查看]
上传用户:mugkiss
上传日期:2022-08-05
资源大小:1360k
文件大小:7k
源码类别:
其他小程序
开发平台:
Visual C++
- // newTest.cpp : 定义应用程序的入口点。
- //
- #include "stdafx.h"
- #include "newTest.h"
- #define MAX_LOADSTRING 100
- #include <math.h>
- // 全局变量:
- HINSTANCE hInst; // 当前实例
- TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
- TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
- // 此代码模块中包含的函数的前向声明:
- ATOM MyRegisterClass(HINSTANCE hInstance);
- BOOL InitInstance(HINSTANCE, int);
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
- int APIENTRY _tWinMain(HINSTANCE hInstance,
- HINSTANCE hPrevInstance,
- LPTSTR lpCmdLine,
- int nCmdShow)
- {
- // TODO: 在此放置代码。
- MSG msg;
- HACCEL hAccelTable;
- // 初始化全局字符串
- LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
- LoadString(hInstance, IDC_NEWTEST, szWindowClass, MAX_LOADSTRING);
- MyRegisterClass(hInstance);
- // 执行应用程序初始化:
- if (!InitInstance (hInstance, nCmdShow))
- {
- return FALSE;
- }
- hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_NEWTEST);
- // 主消息循环:
- while (GetMessage(&msg, NULL, 0, 0))
- {
- if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- return (int) msg.wParam;
- }
- //
- // 函数: MyRegisterClass()
- //
- // 目的: 注册窗口类。
- //
- // 注释:
- //
- // 仅当希望在已添加到 Windows 95 的
- // “RegisterClassEx”函数之前此代码与 Win32 系统兼容时,
- // 才需要此函数及其用法。调用此函数
- // 十分重要,这样应用程序就可以获得关联的
- // “格式正确的”小图标。
- //
- ATOM MyRegisterClass(HINSTANCE hInstance)
- {
- WNDCLASSEX wcex;
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_HREDRAW | CS_VREDRAW;
- wcex.lpfnWndProc = (WNDPROC)WndProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = hInstance;
- wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_NEWTEST);
- wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
- wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wcex.lpszMenuName = (LPCTSTR)IDC_NEWTEST;
- wcex.lpszClassName = szWindowClass;
- wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
- return RegisterClassEx(&wcex);
- }
- //
- // 函数: InitInstance(HANDLE, int)
- //
- // 目的: 保存实例句柄并创建主窗口
- //
- // 注释:
- //
- // 在此函数中,我们在全局变量中保存实例句柄并
- // 创建和显示主程序窗口。
- //
- BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
- {
- HWND hWnd;
- hInst = hInstance; // 将实例句柄存储在全局变量中
- hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, 0, 640, 480, NULL, NULL, hInstance, NULL);
- if (!hWnd)
- {
- return FALSE;
- }
- ShowWindow(hWnd, nCmdShow);
- UpdateWindow(hWnd);
- return TRUE;
- }
- //
- // 函数: WndProc(HWND, unsigned, WORD, LONG)
- //
- // 目的: 处理主窗口的消息。
- //
- // WM_COMMAND - 处理应用程序菜单
- // WM_PAINT - 绘制主窗口
- // WM_DESTROY - 发送退出消息并返回
- //
- //
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- int wmId, wmEvent;
- PAINTSTRUCT ps;
- HDC hdc;
- static HBITMAP bgmap;
- static HBITMAP NpcMap;
- static HDC memBgDc;
- static BITMAP bmpinfo;
- static int action;
- static HDC npcmemDc;
- static POINT npcPoint;
- static int speed;
- static POINT targetPoint;//使用鼠标控制时人物要移动到的目标位置
- static double distance;
- static HDC memDc;
- static HBITMAP membit;
- static RECT clientRect;
- switch (message)
- {
- case WM_CREATE:
- bgmap=(HBITMAP)LoadImage(hInst,"city.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
- NpcMap=(HBITMAP)LoadImage(hInst,"npc00.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
- memDc=::CreateCompatibleDC(GetDC(hWnd));
- memBgDc=CreateCompatibleDC(GetDC(hWnd));
- npcmemDc=CreateCompatibleDC(GetDC(hWnd));
- SetTimer(hWnd,1,90,NULL);
- npcPoint.x = 10;
- npcPoint.y =10;
- speed=5;
- GetClientRect(hWnd,&clientRect);
- membit=CreateCompatibleBitmap(GetDC(hWnd),clientRect.right-clientRect.left,clientRect.bottom-clientRect.top);
- break;
- case WM_TIMER:
- {
- action=((action+1)>1)?0:action+1;
- //人物状态刷新
- double xdistance = targetPoint.x - npcPoint.x;
- double ydistance = targetPoint.y - npcPoint.y;
- distance= _hypot(xdistance,ydistance);
- /*npcPoint.x = targetPoint.x;
- npcPoint.y = targetPoint.y;*/
- if (distance > 5)
- {
- npcPoint.x += speed*xdistance/distance;
- npcPoint.y += speed*ydistance/distance;
- }
- InvalidateRect(hWnd,NULL,FALSE);//flase 擦除前次绘画
- }
- break;
- case WM_LBUTTONDOWN:
- targetPoint.x=LOWORD(lParam);
- targetPoint.y=HIWORD(lParam);
- case WM_KEYDOWN:
- switch (wParam)
- {
- case VK_DOWN:
- if (GetKeyState((VK_SHIFT))&0x80)
- {
- npcPoint.y+=speed*2;
- }
- npcPoint.y+=speed;
- break;
- case VK_UP:
- if (GetKeyState((VK_SHIFT))&0x80)
- {
- npcPoint.y-=speed*2;
- }
- npcPoint.y-=speed;
- break;
- case VK_LEFT:
- if (GetKeyState((VK_SHIFT))&0x80)
- {
- npcPoint.x-=speed*2;
- }
- npcPoint.x-=speed;
- break;
- case VK_RIGHT:
- if (GetKeyState((VK_SHIFT))&0x80)
- {
- npcPoint.x+=speed*2;
- }
- npcPoint.x+=speed;
- break;
- }
- break;
- case WM_COMMAND:
- wmId = LOWORD(wParam);
- wmEvent = HIWORD(wParam);
- // 分析菜单选择:
- switch (wmId)
- {
- case IDM_ABOUT:
- DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
- break;
- case IDM_EXIT:
- DestroyWindow(hWnd);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- break;
- case WM_PAINT:
- hdc = BeginPaint(hWnd, &ps);
- SelectObject(memDc,membit);
- GetObject(bgmap,sizeof(BITMAP),&bmpinfo);
- SelectObject(memBgDc,bgmap);
- BitBlt(memDc,0,0,clientRect.right,clientRect.bottom,memBgDc,0,0,SRCCOPY);
- //人物资源
- GetObject(NpcMap,sizeof(BITMAP),&bmpinfo);
- SelectObject(npcmemDc,NpcMap);
- TransparentBlt(memDc,
- npcPoint.x,npcPoint.y //npc初始位置
- ,bmpinfo.bmWidth/2,bmpinfo.bmHeight/4,//绘制到设备环境时的人物尺寸
- npcmemDc,
- action*bmpinfo.bmWidth/2,0//兼容设备环境上的截取位置
- ,bmpinfo.bmWidth/2,bmpinfo.bmHeight/4,//截取尺寸
- RGB(0,255,0));
- BitBlt(hdc,0,0,640,480,memDc,0,0,SRCCOPY);
- // TODO: 在此添加任意绘图代码...
- EndPaint(hWnd, &ps);
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- // “关于”框的消息处理程序。
- LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_INITDIALOG:
- return TRUE;
- case WM_COMMAND:
- if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
- {
- EndDialog(hDlg, LOWORD(wParam));
- return TRUE;
- }
- break;
- }
- return FALSE;
- }