C# 如果光标位置不在元素边框内,则禁用输入调用
我使用鼠标坐标(x&y)在windows上获取自动化元素 我想实现一个功能,如果用户光标位置不在元素Boundingrectangle内,则使用API user32.dll阻止输入调用,为此,我在下面编写了鼠标按下操作代码C# 如果光标位置不在元素边框内,则禁用输入调用,c#,winforms,C#,Winforms,我使用鼠标坐标(x&y)在windows上获取自动化元素 我想实现一个功能,如果用户光标位置不在元素Boundingrectangle内,则使用API user32.dll阻止输入调用,为此,我在下面编写了鼠标按下操作代码 [return: MarshalAs(UnmanagedType.Bool)] [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern void
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern void BlockInput([In, MarshalAs(UnmanagedType.Bool)]bool fBlockIt);
[DllImport("User32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetCursorPos(ref POINT p);
private void HookManager_MouseDown(object sender, MouseEventArgs e)
{
POINT l_objPoint = new POINT();
bool l_bretVal = GetCursorPos(ref l_objPoint);
while (!m_bRect .Contains(l_objPoint.X, l_objPoint.Y)
{
BlockInput(true);
break;
}
}
但上面的代码并没有按预期工作,即使用户光标不在automation元素boundingrectangle内,也需要用户单击
我还尝试了另一种使用定时器勾号的方法,请检查下面的代码,但这里没有正确捕获Automationelement
private void OnHovering()
{
System.Windows.Forms.Timer _timerOnHover = new System.Windows.Forms.Timer();
_timerOnHover.Tick += new EventHandler(_timerOnHovering_Tick);
_timerOnHover.Interval = 100;
_timerOnHover.Start();
}
private void _timerOnHovering_Tick(object sender, EventArgs e)
{
POINT currentPoint = new POINT();
bool l_bretVal = GetCursorPos(ref currentPoint);
if (!l_bretVal)
{
return;
}
if (!m_bRect.Contains(currentPoint.X, currentPoint.Y && !ISmouseMove)
{
BlockInput(true);
}
else
{
BlockInput(false);
}
}
}
private void OnHovering()
{
System.Windows.Forms.Timer _timerOnHover = new System.Windows.Forms.Timer();
_timerOnHover.Tick += new EventHandler(_timerOnHovering_Tick);
_timerOnHover.Interval = 100;
_timerOnHover.Start();
}
private void _timerOnHovering_Tick(object sender, EventArgs e)
{
POINT currentPoint = new POINT();
bool l_bretVal = GetCursorPos(ref currentPoint);
if (!l_bretVal)
{
return;
}
if (!m_bRect.Contains(currentPoint.X, currentPoint.Y && !ISmouseMove)
{
BlockInput(true);
}
else
{
BlockInput(false);
}
}
}