| 
 | 
 
 本帖最后由 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); 
} |   
 
 
 
 |