检查项目的 hwnd 是否为零。这对我来说经常出现。
您仍然可以通过将光标移动到元素的中间并发送鼠标单击事件来单击。
您可以根据边界矩形计算位置。根据您的情况,您可能需要将客户端转换为屏幕坐标。
边界矩形部分类似于:
x = left + (right - left) / 2
y = top + (bottom - top) / 2
要转换为屏幕坐标,您必须使用具有 hwnd 的元素并应用必要的偏移量。
编辑
要将光标放在矩形上,我使用 PInvoke:
这是我使用的东西(C#):
public static void Click()
{
User32.mouse_event(WindowsConstants.MouseEventLeftDown, 0, 0, 0, IntPtr.Zero);
User32.mouse_event(WindowsConstants.MouseEventLeftUp, 0, 0, 0, IntPtr.Zero);
}
public static void RightClick()
{
User32.mouse_event(WindowsConstants.MouseEventRightDown, 0, 0, 0, IntPtr.Zero);
User32.mouse_event(WindowsConstants.MouseEventRightUp, 0, 0, 0, IntPtr.Zero);
}
public static void DoubleClick()
{
User32.mouse_event(WindowsConstants.MouseEventLeftDown, 0, 0, 0, IntPtr.Zero);
User32.mouse_event(WindowsConstants.MouseEventLeftUp, 0, 0, 0, IntPtr.Zero);
Thread.Sleep(150);
User32.mouse_event(WindowsConstants.MouseEventLeftDown, 0, 0, 0, IntPtr.Zero);
User32.mouse_event(WindowsConstants.MouseEventLeftUp, 0, 0, 0, IntPtr.Zero);
}
public const UInt32 MouseEventLeftDown = 0x0002;
public const UInt32 MouseEventLeftUp = 0x0004;
public const UInt32 MouseEventRightDown = 0x0008;
public const UInt32 MouseEventRightUp = 0x00010;
[DllImport("user32.dll")]
public static extern void mouse_event(UInt32 dwFlags, UInt32 dx, UInt32 dy, UInt32 dwData, IntPtr dwExtraInfo);