当LeftButton为HELD C#

时间:2018-12-06 14:29:43

标签: c# click mouse mouseleftbuttondown

注意:我对此很陌生,所以如果我犯了愚蠢的错误,我表示歉意,我正在学习! :D

所以基本上我想在按住鼠标的LeftButton的同时执行一个动作,在我的情况下,我希望程序在保持true的情况下执行LeftClick。我已完成以下操作:

  [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = 
  CallingConvention.StdCall)]

  public static extern void mouse_event(int dwFlags, int dx, int dy, int 
  cButtons, int dwExtraInfo);

  private const int MOUSEEVENTF_LEFTDOWN = 0x0002;
  private const int MOUSEEVENTF_LEFTUP = 0x0004;

  bool isDown = false;

  private void mouseMoveEventHandler(object sender, MouseEventArgs e)
  {
        if (e.Button == MouseButtons.Left)
        {
            isDown = true;
        }
        else
        {
            isDown = false;
        }
    }
    //send click
    public void send()
    {
        while (isDown == true){
            int dly = 100;
            txtDly.Text = dly.ToString();
            mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
            System.Threading.Thread.Sleep(dly);
            mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
        }
    }

这当前对我不起作用,单击事件是否仅在我在表单上时发生?还是会在我的计算机上的任何地方执行点击?

感谢您的帮助,如果这是一个愚蠢的问题,请对不起。

0 个答案:

没有答案