如何检查鼠标点击位置是否在所需的应用程序上?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何检查鼠标点击位置是否在所需的应用程序上?相关的知识,希望对你有一定的参考价值。

我知道使用API​​ GetCursorPos和应用程序句柄(HWND)的鼠标单击位置。

如何检查此应用程序上的鼠标点击位置?

我的看法:

  • 从其句柄中获取应用程序的边界框。 (GetWindowRect(hWnd, &rect);
  • 检查光标位置是否在此边界框中。 (PtInRect(&rect, p)

如果窗口重叠,这不起作用。

答案

我们知道目标屏幕句柄的手柄和点击光标位置:

// hWnd : Already known windows handle

GetCursorPos(&p);
HWND hWndFromPoint = WindowFromPoint(p);

// If the handle got from click point is child of the desire window means it is clicked on the desire window itself.
if (IsChild(hWnd, hWndFromPoint))
{
    // Do something on Mouse click 
}

以上是关于如何检查鼠标点击位置是否在所需的应用程序上?的主要内容,如果未能解决你的问题,请参考以下文章