获取当前光标位置 [英] Get current cursor position

查看:128
本文介绍了获取当前光标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取窗口的当前鼠标位置,并将其分配给2个变量 x y (相对于窗口的坐标,而不是整个屏幕)。

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 screen as a whole).

我使用Win32和C ++。

I'm using Win32 and C++.

还有一个快速的奖励问题:你如何隐藏光标/取消隐藏光标?

And a quick bonus question: how would you go about hiding the cursor/unhiding it?

推荐答案

通过调用 GetCursorPos

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

这将返回相对于屏幕坐标的光标位置。致电 ScreenToClient 以映射到窗口坐标。

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
}






您可以使用 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.

这篇关于获取当前光标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆