ljsgxka 2021-10-19 20:22 采纳率: 0%
浏览 16
已结题

C++键盘控制移动松开键盘按键就退出窗口是为什么?

就是按下能走动但是一松开就关闭整个窗口了,不太清楚是哪儿出了问题
源代码如下:

#include "framework.h"
#include "girl.h"
#include <stdio.h>




//全局变量声明
HINSTANCE hInst;
HBITMAP girl[4], bg,bmp;
HDC        hdc, mdc, bufdc;
HWND    hWnd;
DWORD    tPre, tNow;
int        num, dir, x, y;       //x,y变量为人物贴图坐标,dir为人物移动方向,num为连续贴图中的小图编号
bool    archKeyStatus[4];

//全局函数声明
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
void                CartoonPaint(HDC hdc);

//****WinMain函数,程序入口点函数***********************
int APIENTRY WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR     lpCmdLine,
    int       nCmdShow)
{
    MSG msg;
    ZeroMemory(archKeyStatus, 0);
    MyRegisterClass(hInstance);

    //初始化
    if (!InitInstance(hInstance, nCmdShow))
    {
        return FALSE;
    }

    //贴图初始位置和移动方向
    x = 300;
    y = 250;
    dir = 0;
    num = 0;

    //载入连续移动位图、背景图
    girl[0] = (HBITMAP)LoadImage(NULL, "girl0.bmp", IMAGE_BITMAP, 440, 148, LR_LOADFROMFILE);
    girl[1] = (HBITMAP)LoadImage(NULL, "girl1.bmp", IMAGE_BITMAP, 440, 148, LR_LOADFROMFILE);
    girl[2] = (HBITMAP)LoadImage(NULL, "girl2.bmp", IMAGE_BITMAP, 440, 148, LR_LOADFROMFILE);
    girl[3] = (HBITMAP)LoadImage(NULL, "girl3.bmp", IMAGE_BITMAP, 440, 148, LR_LOADFROMFILE);
    bg = (HBITMAP)LoadImage(NULL, "bg.bmp", IMAGE_BITMAP, 640, 480, LR_LOADFROMFILE);
    hdc = GetDC(hWnd);
    mdc = CreateCompatibleDC(hdc);
    bufdc = CreateCompatibleDC(hdc);

    //建立空的位图并置入mdc中
    bmp = CreateCompatibleBitmap(hdc, 640, 480);
    SelectObject(mdc, bmp);

    CartoonPaint(hdc);



    PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);            
   //消息循环
    while (msg.message != WM_QUIT)
    {
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else
        {
            tNow = GetTickCount();
            if (tNow - tPre >= 40)
                CartoonPaint(hdc);
        }
    }

    return msg.wParam;
}

//设计窗口类
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 = NULL;
    wcex.hCursor = NULL;
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wcex.lpszMenuName = NULL;
    wcex.lpszClassName = "canvas";
    wcex.hIconSm = NULL;

    return RegisterClassEx(&wcex);
}

//****初始化函数*************************************
// 加载位图并设定各种初始值
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    hInst = hInstance;

    hWnd = CreateWindow("canvas", "girl", WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

    if (!hWnd)
    {
        return FALSE;
    }

    MoveWindow(hWnd, 10, 10, 640, 480, true);
    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);


    return TRUE;
}



LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_KEYDOWN:         //按下键盘消息
        //判断按键的虚拟键码
        switch (wParam)
        {
        case VK_ESCAPE:           //按下Esc键
            PostQuitMessage(0);  //结束程序
            break;
        case VK_UP:                  //按下↑键
            archKeyStatus[0] = true;
            dir = 0;
            break;
        case VK_DOWN:              //按下↓键
            archKeyStatus[1] = true;
            dir = 1;
            break;

        case VK_LEFT:              //按下←键
            archKeyStatus[2] = true;
            dir = 2;
            break;

        case VK_RIGHT:               //按下→键
            archKeyStatus[3] = true;
            dir = 3;
            break;
        }
        break;
    case WM_KEYUP: //按键释放的消息
        switch (wParam) {
        case VK_UP:                                                      //向上               
            archKeyStatus[0]=false;
            break;
        case VK_DOWN:                                              //向下       
            archKeyStatus[1] = false;
            break;
        case VK_LEFT:                                                 //向左
            archKeyStatus[2] = false;
            break;
        case VK_RIGHT:                                              //向右
            archKeyStatus[3] = false;
            break;
        }
        break;
    case WM_DESTROY:                    //窗口结束消息
        int i;
        DeleteDC(mdc);
        DeleteDC(bufdc);
        for (i = 0; i < 4; i++)
            DeleteObject(girl[i]);
        DeleteObject(bg);
        ReleaseDC(hWnd, hdc);
        PostQuitMessage(0);
        break;
    default:                            
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}


void CartoonPaint(HDC hdc)
{
    int w, h;
    SelectObject(bufdc, bg);
    BitBlt(mdc, 0, 0, 640, 480, bufdc, 0, 0, SRCCOPY);
    SelectObject(bufdc, girl[dir]);
    w = 55;
    h = 74;
    if (archKeyStatus[0]) {
        y -= 10;
        if (y < 0)     y = 0;
    }
    if (archKeyStatus[1]) {
        y += 10;
        if (y > 375)     y = 375;
    }
    if (archKeyStatus[2]) {
        x -= 10;
        if (x < 0)
            x = 0;
    }
    if (archKeyStatus[3]) {
        x += 10;
        if (x > 575)
            x = 575;
    }
    BitBlt(mdc, x, y, w, h, bufdc, num*w, h, SRCAND);
    BitBlt(mdc, x, y, w, h, bufdc, num*w, 0, SRCPAINT);
    BitBlt(hdc, 0, 0, 640, 480, mdc, 0, 0, SRCCOPY);
    tPre = GetTickCount();
    num++;
    if (num == 8)     num = 0;
}



  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 10月27日
    • 创建了问题 10月19日

    悬赏问题

    • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
    • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
    • ¥50 如何将脑的图像投影到颅骨上
    • ¥15 提问一个关于vscode相关的环境配置问题,就是输入中文但是显示不出来,代码在idea可以显示中文,但在vscode不行,不知道怎么配置环境
    • ¥15 netcore使用PuppeteerSharp截图
    • ¥20 这张图页头,页脚具体代码该怎么写?
    • ¥15 关于#sql#的问题,请各位专家解答!
    • ¥20 WPF MVVM模式 handycontrol 框架, hc:SearchBar 控件 Text="{Binding NavMenusKeyWords}" 绑定取不到值
    • ¥15 需要手写数字信号处理Dsp三个简单题 不用太复杂
    • ¥15 数字信号处理考试111