Get current cursor position

前端 未结 2 1279
谎友^
谎友^ 2020-11-27 16:19

I want to get the current mouse position of the window, and assign it to 2 variables x and y (co-ordinates relative to the window, not to the scree

相关标签:
2条回答
  • 2020-11-27 16:20

    You get the cursor position by calling GetCursorPos.

    POINT p;
    if (GetCursorPos(&p))
    {
        //cursor position now in p.x and p.y
    }
    

    This returns the cursor position relative to screen coordinates. Call ScreenToClient to map to window coordinates.

    if (ScreenToClient(hwnd, &p))
    {
        //p.x and p.y are now relative to hwnd's client area
    }
    

    You hide and show the cursor with ShowCursor.

    ShowCursor(FALSE);//hides the cursor
    ShowCursor(TRUE);//shows it again
    

    You must ensure that every call to hide the cursor is matched by one that shows it again.

    0 讨论(0)
  • 2020-11-27 16:22

    GetCursorPos() will return to you the x/y if you pass in a pointer to a POINT structure.

    Hiding the cursor can be done with ShowCursor().

    0 讨论(0)
提交回复
热议问题