如何在C ++中输入鼠标按钮

时间:2019-04-09 11:20:58

标签: c++ winapi

我出于娱乐目的正在用c ++制作一个井字游戏项目,我已经完成了制作游戏板的工作。

我想输入用户单击的位置,以便可以正确显示“标记”。为了测试我的代码,我尝试显示单元格的编号(1,2,3 ...)。每当我单击面板外时,它应显示-1

但是INSTEAD却在任何地方(内外)显示-1

下面给出的是我编写的代码的一部分。完整的代码可以在https://pastebin.com/dEQvRdhz

上找到
const int Cell_Size = 100;

BOOL GetBoard(HWND hwnd, RECT *pRect) {

    RECT rc;

    if (GetClientRect(hwnd, &rc)) {
        int *ptr;

        int width = rc.right - rc.left;
        int height = rc.bottom - rc.top;

        rc.left = (width - Cell_Size * 3) / 2;
        rc.top = (height - Cell_Size * 3) / 2;
        rc.right = (rc.left + Cell_Size * 3);
        rc.bottom = (rc.top + Cell_Size * 3);


        return TRUE;
    } SetRectEmpty(pRect);
    return FALSE;


}
int GetMousePoint(HWND hwnd,int x, int y)
{
    POINT pt = { x, y };


    RECT rc;
    if (GetBoard(hwnd,&rc )) 
    {
        if (PtInRect(&rc, pt)) 
        {
            x = pt.x - rc.left;
            y = pt.y - rc.top;

            int column = x / Cell_Size;
            int row = y / Cell_Size;

            return column+row;
        }   

    }return -1;
}

case WM_LBUTTONDOWN:

        {  
          int xPos = GET_X_LPARAM(lParam);
            int yPos = GET_Y_LPARAM(lParam);    
            int index = GetMousePoint(hWnd,xPos, yPos);

            HDC hdc = GetDC(hWnd); 
            if (NULL != hdc) {

                WCHAR temp[100];
                wsprintf(temp,L"Index = %d",index );
                TextOut(hdc, xPos, yPos, temp, lstrlen(temp));
                ReleaseDC(hWnd, hdc);

            } 



        }

        break;

    case WM_GETMINMAXINFO:

        {
            MINMAXINFO * pMinMax = (MINMAXINFO*)lParam ;
            pMinMax->ptMinTrackSize.x = Cell_Size * 5;
            pMinMax->ptMinTrackSize.y = Cell_Size * 5;
                                                }
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);



            RECT rc;

            if (GetClientRect(hWnd, &rc)) {
                int *ptr;

                int width = rc.right - rc.left;
                int height = rc.bottom - rc.top;

                rc.left = (width - Cell_Size * 3) / 2;
                rc.top = (height - Cell_Size * 3) / 2;
                rc.right = (rc.left + Cell_Size * 3);
                rc.bottom = (rc.top + Cell_Size * 3);

                Rectangle(hdc, rc.left, rc.top, rc.right, rc.bottom);
                Rectangle(hdc, rc.left, rc.top+Cell_Size, rc.right, rc.top+Cell_Size*2);
                Rectangle(hdc, rc.left+Cell_Size, rc.top , rc.left+Cell_Size*2, rc.bottom);
                Rectangle(hdc, rc.left + Cell_Size, rc.top + Cell_Size, rc.left + Cell_Size * 2, rc.top + Cell_Size * 2);

            }



            EndPaint(hWnd, &ps);
        }
        break;

1 个答案:

答案 0 :(得分:0)

我已经尝试过您的代码。

问题在这里:

problem

最简单的方法是定义全局变量。

const int Cell_Size = 100;
RECT rc;
BOOL GetBoard(HWND hwnd, RECT *pRect)
{   
//  RECT rc;
if (GetClientRect(hwnd, &rc)) {
    int *ptr;

    int width = rc.right - rc.left;
    int height = rc.bottom - rc.top;

    rc.left = (width - Cell_Size * 3) / 2;
    rc.top = (height - Cell_Size * 3) / 2;
    rc.right = (rc.left + Cell_Size * 3);
    rc.bottom = (rc.top + Cell_Size * 3);


    return TRUE;
} SetRectEmpty(pRect);
    return FALSE;

}

int GetMousePoint(HWND hwnd, int x, int y)
{
POINT pt = { x, y };

//  RECT rc;
if (GetBoard(hwnd, &rc))
{
    if (PtInRect(&rc, pt))
    {
        x = pt.x - rc.left;
        y = pt.y - rc.top;

        int column = x / Cell_Size;
        int row = y / Cell_Size;

        return column + row;
    }

}return -1;
}

最后,您将获得想要的结果: debug