m0_63133201 2022-11-03 12:17 采纳率: 87%
浏览 36
已结题

判断鼠标是否点击矩形怎么实现

判断鼠标是否点击矩形,点击到矩形就变色,要怎么改?help

img


#include <windows.h>        

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
BOOL PtInRect(
    _In_ const RECT* lprc,  //矩形区域
    _In_       POINT pt   //当前点
);
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, iCmdShow);
    UpdateWindow(hwnd);
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;
    HPEN hOldPen, hPen1;
    static HBRUSH hBrush, hBrushR;
    switch (message)
    {
    case WM_CREATE:
        hBrush = (HBRUSH)GetStockObject(BLACK_BRUSH);
        hBrushR = CreateSolidBrush(RGB(255, 0, 0));
        return 0;



case WM_LBUTTONDOWN:
    {

        pt.x = LOWORD(lParam);   //当前鼠标位置
        pt.y = HIWORD(lParam);   //当前鼠标位置
        if (PtInRect(&hdc, pt)) {   //点击检测函数
            hBrush = hBrushR;
            break;
        }
 
    }break;


    case WM_LBUTTONUP: // 鼠标左键,抬起时刷新
        InvalidateRect(hwnd, NULL, FALSE);
        return 0;

    case WM_PAINT:

        hdc = BeginPaint(hwnd, &ps);
        hPen1 = CreatePen(HS_CROSS, 5, RGB(0, 0, 225));

        SelectObject(hdc, hBrush);

        //将hPen1选入设备环境
        hOldPen = (HPEN)SelectObject(hdc, hPen1);

        Rectangle(hdc, 50, 50, 150, 150);


        //删除创建的画刷
        DeleteObject(hBrush);

        //删除创建的画笔
        DeleteObject(hPen1);


        //释放设备环境
        EndPaint(hwnd, &ps);
        return 0;

        //恢复设备环境,将默认画笔选回
        SelectObject(hdc, hOldPen);

    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;

    case WM_KEYDOWN:
        hdc = GetDC(hwnd);
        ReleaseDC(hwnd, hdc);
    }
    return DefWindowProc(hwnd, message, wParam, lParam);
}

img

  • 写回答

1条回答 默认 最新

  • 来灵 2022-11-03 13:20
    关注

    代码稍微修改了下,可以编译运行了,看注释

    #include <windows.h>        
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    BOOL PtInRect(
        _In_ const RECT* lprc,  //矩形区域
        _In_       POINT pt   //当前点
    );
    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, iCmdShow);
        UpdateWindow(hwnd);
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        return msg.wParam;
    }
    LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        HDC hdc;
        PAINTSTRUCT ps;
        HPEN hOldPen, hPen1;
        static HBRUSH hBrush, hBrushR;
        switch (message)
        {
        case WM_CREATE:
            hBrush = (HBRUSH)GetStockObject(BLACK_BRUSH);
            hBrushR = CreateSolidBrush(RGB(255, 0, 0));
            return 0;
    
    
    
        case WM_LBUTTONDOWN:
        {
            //声明当前点变量
            POINT pt;
            //声明矩形框变量
            RECT rc;
            //矩形框变量赋值
            rc.left = 50;
            rc.top = 50;
            rc.right = 150;
            rc.bottom = 150;
            pt.x = LOWORD(lParam);   //当前鼠标位置
            pt.y = HIWORD(lParam);   //当前鼠标位置
            if (PtInRect(&rc, pt)) {   //点击检测函数
                hBrush = hBrushR;
                break;
            }
    
        }break;
    
    
        case WM_LBUTTONUP: // 鼠标左键,抬起时刷新
            InvalidateRect(hwnd, NULL, FALSE);
            return 0;
    
        case WM_PAINT:
    
            hdc = BeginPaint(hwnd, &ps);
            hPen1 = CreatePen(HS_CROSS, 5, RGB(0, 0, 225));
    
            SelectObject(hdc, hBrush);
    
            //将hPen1选入设备环境
            hOldPen = (HPEN)SelectObject(hdc, hPen1);
    
            Rectangle(hdc, 50, 50, 150, 150);
    
    
            //删除创建的画刷
            DeleteObject(hBrush);
    
            //删除创建的画笔
            DeleteObject(hPen1);
    
    
            //释放设备环境
            EndPaint(hwnd, &ps);
            return 0;
    
            //恢复设备环境,将默认画笔选回
            SelectObject(hdc, hOldPen);
    
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    
        case WM_KEYDOWN:
            hdc = GetDC(hwnd);
            ReleaseDC(hwnd, hdc);
        }
        return DefWindowProc(hwnd, message, wParam, lParam);
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 11月11日
  • 已采纳回答 11月3日
  • 修改了问题 11月3日
  • 创建了问题 11月3日

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证