如果光标位置不在元素BoundingRectangle内,则禁用输入调用

时间:2017-01-18 04:54:06

标签: c# winforms

我使用鼠标坐标(x& y)

在Windows上获取自动化元素
while (myfile >> word) {

        volt = "";
        i = 2;

        //printing out words (remove)
        cout << word << endl;

        //checking for U in words
            if (word[0] == 'U') {

                //declaring variables for checking for certain letters
                char v = 'V';
                char m = 'm';
                char M = 'M';

                //taking in integer for voltage
                while (word[i] != v)  {
                    volt = volt + word[i];
                    i++;
                    //cout << volt << endl;
                }
                cout << volt << endl;

我想实现一个功能,如果用户光标位置不在元素Boundingrectangle内,则使用API​​ user32.dll阻止输入调用,因为我已经在下面的代码中写下了鼠标按下操作。

static Rect m_bRect = new Rect(0, 0, 0, 0);

AutomationElement element = AutomationElement.FromPoint(new Point((double)Cursor.Position.X, (double)Cursor.Position.Y));
m_bRect = element.Current.BoundingRectangle;

但是上面的代码没有按预期工作,即使用户光标不在自动化元素boundingrectangle内,也需要用户点击。

我还尝试了另一种使用计时器刻度的方法,请检查下面的代码,但这里没有正确捕获Automationelement。

[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;
          }
       }

0 个答案:

没有答案