【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 2529|回复: 3

简单win32,想按WSAD使一个圆移动,但是不移动怎么回事?

[复制链接]
发表于 2014-1-8 17:04:04 | 显示全部楼层 |阅读模式
本帖最后由 shenchiyuan 于 2014-1-8 17:10 编辑

#include "stdafx.h"
#include "resource.h"
DWORD dwThreadID;
HANDLE h_Handle;
#define MAX_LOADSTRING 100
int startx=400,starty=400;//起始位置
int oldx,oldy;//旧X Y 坐标
int x,y;
HDC hdc;
int r=20;//圆半径
int step=3;//移动一步的距离
RECT rt;
// Global Variables:
HINSTANCE hInst;                                                                // current instance
TCHAR szTitle[MAX_LOADSTRING];                                                                // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];                                                                // The title bar text
int lastmove=0;//是否持续移动
void draw(int x,int y);//话圆并消去旧圆
DWORD WINAPI thread(LPVOID lpParameter)
{
        while(1)
        {
        if(lastmove)//判断是否持续运动
        {
                x=x+x-oldx;
                y=y+y-oldy;
        }
        if(x!=oldx||y!=oldy)//当x,y中有一个改变,重画并消去旧圆
        {
     draw(x,y);
         oldx=x;
         oldy=y;
        }
        }
}
// Foward declarations of functions included in this code module:
ATOM                                MyRegisterClass(HINSTANCE hInstance);
BOOL                                InitInstance(HINSTANCE, int);
LRESULT CALLBACK        WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK        About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
         // TODO: Place code here.
        MSG msg;
        HACCEL hAccelTable;

        // Initialize global strings
        LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
        LoadString(hInstance, IDC_MY, szWindowClass, MAX_LOADSTRING);
        MyRegisterClass(hInstance);

        // Perform application initialization:
        if (!InitInstance (hInstance, nCmdShow))
        {
                return FALSE;
        }

        hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_MY);

        // Main message loop:
        while (GetMessage(&msg, NULL, 0, 0))
        {
                if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
                {
                        TranslateMessage(&msg);
                        DispatchMessage(&msg);
                }
        }

        return msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
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_MY);
        wcex.hCursor                = LoadCursor(NULL, IDC_ARROW);
        wcex.hbrBackground        = (HBRUSH)(COLOR_WINDOW+1);
        wcex.lpszMenuName        = (LPCSTR)IDC_MY;
        wcex.lpszClassName        = szWindowClass;
        wcex.hIconSm                = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

        return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND        - process the application menu
//  WM_PAINT        - Paint the main window
//  WM_DESTROY        - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
        int wmId, wmEvent;
        PAINTSTRUCT ps;

        switch (message)
        {
        case WM_KEYDOWN:
                {
                        switch(wParam)
                        {
                        case 87://W键,按下开始开始持续运动
                                lastmove=0;
                                lastmove=1;
                y=y-step;//y坐标减少
                                break;
                        case 83://S
                                lastmove=0;
                                lastmove=1;
                                y=y+step;
                                break;
                        case 65://A
                                lastmove=0;
                                lastmove=1;
                                x=x-step;
                                break;
                        case 68://D
                                lastmove=0;
                                lastmove=1;
                                x=x+step;
                                break;
                                default:break;
                        }
                        break;
                }
    case WM_KEYUP:
                {
            switch(wParam)
                        {
                                switch(wParam)
                        {
                        case 87://W
                                lastmove=0;
                                break;
                        case 83://S
                                lastmove=0;
                                break;
                        case 65://A
                                lastmove=0;
                                break;
                        case 68://D
                                lastmove=0;
                                break;
                                default:break;
                        }
                        }
                        break;
                }
                case WM_COMMAND:
                        wmId    = LOWORD(wParam);
                        wmEvent = HIWORD(wParam);
                        // Parse the menu selections:
                        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);
                        // TODO: Add any drawing code here...
                        x=startx;
                        y=starty;
                        oldx=startx;
                        oldy=starty;
                        CreateThread(NULL,0,thread,NULL,0,&dwThreadID);//判断线程
                        GetClientRect(hWnd, &rt);
                        Ellipse(hdc,x-r,y-r,x+r,y+r);//先画开始的圆
                        EndPaint(hWnd, &ps);
                        break;
                case WM_DESTROY:
                        CloseHandle(h_Handle);
                        PostQuitMessage(0);
                        break;
                default:
                        return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

// Mesage handler for about box.
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;
}
void draw(int x,int y)
{
   HGDIOBJ old=SelectObject(hdc,CreatePen(PS_SOLID,1,RGB(255,255,255)));//把画笔弄成白色,消除之前的圆
   Ellipse(hdc,oldx-r,oldy-r,oldx+r,oldy+r);
   SelectObject(hdc,old);
   Ellipse(hdc,x-r,y-r,x+r,y+r);
}
发表于 2014-1-8 22:45:15 | 显示全部楼层
  还写得多线程啊~~
你在画圆的地方设断点,坐标变了吗?
 楼主| 发表于 2014-1-9 14:06:54 | 显示全部楼层
本帖最后由 shenchiyuan 于 2014-1-9 14:28 编辑
夜行的猫仔 发表于 2014-1-8 22:45
还写得多线程啊~~
你在画圆的地方设断点,坐标变了吗?

变             了                 啊
发表于 2014-1-13 22:10:38 | 显示全部楼层
你的QQ叫什么?找不到你的Q了~
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2025-2-6 07:05

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表