【发布时间】:2021-04-13 01:07:05
【问题描述】:
我正在寻找在特定窗口中的光标位置。 目前,我明白了:
/// <summary>
/// Struct representing a point.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
public static implicit operator Point(POINT point) {
return new Point(point.X, point.Y);
}
}
/// <summary>
/// Retrieves the cursor's position, in screen coordinates.
/// </summary>
/// <see>See MSDN documentation for further information.</see>
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out POINT lpPoint);
public static Point GetCursorPosition() {
POINT lpPoint;
GetCursorPos(out lpPoint);
// NOTE: If you need error handling
// bool success = GetCursorPos(out lpPoint);
// if (!success)
return lpPoint;
}
这很好用,但它并不特定于像 Spy++ 这样的窗口。
【问题讨论】:
-
您在寻找 ScreenToClient 功能吗? docs.microsoft.com/en-us/windows/win32/api/winuser/…
-
谢谢,我会读到的 :) @SimonMourier
-
这个函数返回一个布尔值:/不是位置@SimonMourier
-
当然。仔细阅读文档。你必须传递一个带有位置的指针,它会相对于窗口改变它:pinvoke.net/default.aspx/user32.screentoclient
-
我目前正在做与文档相同的操作,但我得到了一个奇怪的 X、Y 位置,类似于 X=-8,Y=-31。