C#如何用鼠标左键切换模拟鼠标点击

时间:2018-02-05 11:01:15

标签: c#

我希望当我按住鼠标左键时它会模拟点击次数,但这不起作用,因为它没有模拟点击次数,但如果我删除了timer1中的if语句,它会模拟点击,但我希望它只在我按住鼠标时模拟。

到目前为止,我的代码是我所做的。如果你能提供帮助那就太棒了

 public partial class Form1 : Form
{
    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
    //Mouse actions
    private const int MOUSEEVENTF_LEFTDOWN = 0x02;
    private const int MOUSEEVENTF_LEFTUP = 0x04;
    private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
    private const int MOUSEEVENTF_RIGHTUP = 0x10;




    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        timer1.Start();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        timer1.Stop();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
       if (MouseButtons == MouseButtons.Left){
            DoMouseClick();
        }
    }

    public void DoMouseClick()
    {
        //Call the imported function with the cursor's current position
        uint X = (uint)Cursor.Position.X;
        uint Y = (uint)Cursor.Position.Y;
        mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
    }
}

0 个答案:

没有答案