m0_63133201 2022-11-05 03:09 采纳率: 87%
浏览 26
已结题

把case WM_KEYDOWN 放进去这个函数里面if (PtInRect(&ra, pt)) 要怎么做?

把case WM_KEYDOWN 放进去这个函数里面if (PtInRect(&ra, pt)) 要怎么做?
目的:选择中小箱子的位置,然后通过键盘进行移动。


#include <windows.h>        
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    PSTR szCmdLine,
    int iCmdShow)
{
    static TCHAR szAppName[] = TEXT("HelloWin");
    HWND   hwnd;
    MSG    msg;
    WNDCLASS wndclass;
    wndclass.style = 0;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
    wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = TEXT("MyClass");
    if (!RegisterClass(&wndclass))//(注册窗口类的API是RegisterClass)
    {
        MessageBox(NULL, TEXT("RegisterClass fail!"), szAppName, MB_ICONERROR);
        return 0;
    }
    //窗口起始位置(100,100),大小300*400   
    hwnd = CreateWindow(
        TEXT("MyClass"),        // window class name
        szAppName,        // window caption
        WS_OVERLAPPEDWINDOW,    // window style
        100,            // initial x position
        100,            // initial y position
        630,            // initial x size
        520,            // initial y size
        NULL,                // parent window handle
        NULL,                // window menu handle
        hInstance,        // program instance handle
        NULL);            // creation parameters
    if (!hwnd)
    {
        MessageBox(NULL, TEXT("CreateWindow fail!!"), szAppName, MB_ICONERROR);
        return 0;
    }
    ShowWindow(hwnd, SW_SHOWMAXIMIZED);
    UpdateWindow(hwnd);
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}
HWND hwnd;
HDC hdc, hdcmen;
BITMAP bm;
HBITMAP hbm;
PAINTSTRUCT ps; 

//窗口过程处理函数
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{

    HPEN hPen, hOldPen;
    srand((unsigned)time(NULL));
    int n = rand() % 10;
    int m = rand() % 10;     
       const int a =  n * 50 + 20;
       const int b =  m * 50 + 20;
       static int xMonster = a, yMonster = b;
    const int step = 50;


    switch (message)
    {
    case WM_LBUTTONDOWN:
            {
                //声明当前点变量
                POINT pt;
                //声明矩形框变量
                RECT ra;
                //矩形框变量赋值
                ra.left = xMonster;
                ra.top =yMonster;
                ra.right = bm.bmWidth;
                ra.bottom = bm.bmHeight;
                pt.x = LOWORD(lParam);   //当前鼠标位置
                pt.y = HIWORD(lParam);   //当前鼠标位置           
                if (PtInRect(&ra, pt)) {   //点击检测函数
                  
                    break;
                }
            }break;
    case WM_LBUTTONUP: // 鼠标左键,抬起时刷新
    {

    }
    case WM_KEYDOWN:
                switch (wParam) {
                case VK_UP:
                    yMonster -= step;
                    break;
                case VK_DOWN:
                    yMonster += step;
                    break;
                case VK_LEFT:
                    xMonster -= step;
                    break;
                case VK_RIGHT:
                    xMonster += step;
                    break;
                }
                InvalidateRect(hwnd, NULL, true);
                return 0;
    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        hdcmen = CreateCompatibleDC(hdc);
        hbm = (HBITMAP)LoadImage(
            NULL,
            TEXT("box1.bmp"),
            IMAGE_BITMAP,
            0,
            0,
            LR_LOADFROMFILE);
        SelectObject(hdcmen, hbm);
        GetObject(hbm, sizeof(BITMAP), &bm);
        BitBlt(hdc, xMonster, yMonster, bm.bmWidth, bm.bmHeight, hdcmen, 0, 0, SRCCOPY);
        
        hPen = CreatePen(PS_SOLID, 5, RGB(0, 0, 0));
        hOldPen = (HPEN)SelectObject(hdc, hPen);

        for (int i = 0; i < 550; i += 50) {
            MoveToEx(hdc, 20, 20+i, NULL);
            LineTo(hdc, 520, 20+i);
        }
        for (int i = 0; i < 550; i += 50) {
            MoveToEx(hdc, 20+i, 20 , NULL);
            LineTo(hdc, 20+i, 520 );
        }
        DeleteObject(hbm);
        DeleteDC(hdcmen);
        EndPaint(hwnd, &ps);
        return 0;

    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;    
    }
    
    return DefWindowProc(hwnd, message, wParam, lParam);
}

  • 写回答

1条回答 默认 最新

  • 真相重于对错 2022-11-05 07:56
    关注

    在if修改盒子坐标

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月9日
  • 创建了问题 11月5日

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。